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

1.1       root        1: /*
                      2:        MS-DOS Player for Win32 console
                      3: 
                      4:        Author : Takeda.Toshiya
                      5:        Date   : 2009.11.09-
                      6: */
                      7: 
                      8: #include "msdos.h"
                      9: 
1.1.1.28  root       10: void exit_handler();
                     11: 
1.1       root       12: #define fatalerror(...) { \
                     13:        fprintf(stderr, __VA_ARGS__); \
1.1.1.28  root       14:        exit_handler(); \
1.1       root       15:        exit(1); \
                     16: }
                     17: #define error(...) fprintf(stderr, "error: " __VA_ARGS__)
1.1.1.22  root       18: #define nolog(...)
                     19: 
1.1.1.33  root       20: //#define ENABLE_DEBUG_LOG
                     21: #ifdef ENABLE_DEBUG_LOG
1.1.1.22  root       22:        #define EXPORT_DEBUG_TO_FILE
                     23:        #define ENABLE_DEBUG_SYSCALL
                     24:        #define ENABLE_DEBUG_UNIMPLEMENTED
1.1.1.25  root       25:        #define ENABLE_DEBUG_IOPORT
1.1.1.22  root       26:        
                     27:        #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root       28:                FILE* fp_debug_log = NULL;
1.1.1.22  root       29:        #else
1.1.1.33  root       30:                #define fp_debug_log stderr
1.1.1.22  root       31:        #endif
                     32:        #ifdef ENABLE_DEBUG_UNIMPLEMENTED
                     33:                #define unimplemented_10h fatalerror
1.1.1.42  root       34:                #define unimplemented_13h fatalerror
1.1.1.25  root       35:                #define unimplemented_14h fatalerror
1.1.1.22  root       36:                #define unimplemented_15h fatalerror
                     37:                #define unimplemented_16h fatalerror
1.1.1.37  root       38:                #define unimplemented_17h fatalerror
1.1.1.22  root       39:                #define unimplemented_1ah fatalerror
                     40:                #define unimplemented_21h fatalerror
                     41:                #define unimplemented_2fh fatalerror
1.1.1.24  root       42:                #define unimplemented_33h fatalerror
1.1.1.22  root       43:                #define unimplemented_67h fatalerror
                     44:                #define unimplemented_xms fatalerror
                     45:        #endif
                     46: #endif
                     47: #ifndef unimplemented_10h
                     48:        #define unimplemented_10h nolog
                     49: #endif
1.1.1.42  root       50: #ifndef unimplemented_13h
                     51:        #define unimplemented_13h nolog
                     52: #endif
1.1.1.25  root       53: #ifndef unimplemented_14h
                     54:        #define unimplemented_14h nolog
                     55: #endif
1.1.1.22  root       56: #ifndef unimplemented_15h
                     57:        #define unimplemented_15h nolog
                     58: #endif
                     59: #ifndef unimplemented_16h
                     60:        #define unimplemented_16h nolog
                     61: #endif
1.1.1.37  root       62: #ifndef unimplemented_17h
                     63:        #define unimplemented_17h nolog
                     64: #endif
1.1.1.22  root       65: #ifndef unimplemented_1ah
                     66:        #define unimplemented_1ah nolog
                     67: #endif
                     68: #ifndef unimplemented_21h
                     69:        #define unimplemented_21h nolog
                     70: #endif
                     71: #ifndef unimplemented_2fh
                     72:        #define unimplemented_2fh nolog
                     73: #endif
1.1.1.24  root       74: #ifndef unimplemented_33h
                     75:        #define unimplemented_33h nolog
                     76: #endif
1.1.1.22  root       77: #ifndef unimplemented_67h
                     78:        #define unimplemented_67h nolog
                     79: #endif
                     80: #ifndef unimplemented_xms
                     81:        #define unimplemented_xms nolog
                     82: #endif
                     83: 
1.1.1.32  root       84: #define array_length(array) (sizeof(array) / sizeof(array[0]))
1.1.1.22  root       85: #define my_strchr(str, chr) (char *)_mbschr((unsigned char *)(str), (unsigned int)(chr))
                     86: #define my_strtok(tok, del) (char *)_mbstok((unsigned char *)(tok), (const unsigned char *)(del))
                     87: #define my_strupr(str) (char *)_mbsupr((unsigned char *)(str))
1.1       root       88: 
1.1.1.12  root       89: #if defined(__MINGW32__)
                     90: extern "C" int _CRT_glob = 0;
                     91: #endif
                     92: 
                     93: /*
                     94:        kludge for "more-standardized" C++
                     95: */
                     96: #if !defined(_MSC_VER)
                     97: inline int kludge_min(int a, int b) { return (a<b ? a:b); }
                     98: inline int kludge_max(int a, int b) { return (a>b ? a:b); }
                     99: #define min(a,b) kludge_min(a,b)
                    100: #define max(a,b) kludge_max(a,b)
1.1.1.14  root      101: #elif _MSC_VER >= 1400
                    102: void ignore_invalid_parameters(const wchar_t *, const wchar_t *, const wchar_t *, unsigned int, uintptr_t)
                    103: {
                    104: }
                    105: #endif
                    106: 
1.1.1.35  root      107: #define USE_VRAM_THREAD
1.1.1.14  root      108: 
1.1.1.35  root      109: #ifdef USE_VRAM_THREAD
1.1.1.14  root      110: static CRITICAL_SECTION vram_crit_sect;
                    111: #else
                    112: #define vram_flush()
1.1.1.12  root      113: #endif
                    114: 
1.1.1.14  root      115: #define VIDEO_REGEN *(UINT16 *)(mem + 0x44c)
                    116: #define SCR_BUF(y,x) scr_buf[(y) * scr_buf_size.X + (x)]
                    117: 
                    118: void change_console_size(int width, int height);
                    119: void clear_scr_buffer(WORD attr);
                    120: 
                    121: static UINT32 vram_length_char = 0, vram_length_attr = 0;
                    122: static UINT32 vram_last_length_char = 0, vram_last_length_attr = 0;
                    123: static COORD vram_coord_char, vram_coord_attr;
                    124: 
1.1.1.28  root      125: char temp_file_path[MAX_PATH];
                    126: bool temp_file_created = false;
                    127: 
1.1.1.14  root      128: bool ignore_illegal_insn = false;
                    129: bool limit_max_memory = false;
                    130: bool no_windows = false;
                    131: bool stay_busy = false;
1.1.1.19  root      132: bool support_ems = false;
                    133: #ifdef SUPPORT_XMS
                    134: bool support_xms = false;
                    135: #endif
1.1.1.29  root      136: int sio_port_number[4] = {0, 0, 0, 0};
1.1.1.14  root      137: 
                    138: BOOL is_vista_or_later;
                    139: 
1.1.1.35  root      140: #define UPDATE_OPS 16384
                    141: #define REQUEST_HARDWRE_UPDATE() { \
                    142:        update_ops = UPDATE_OPS - 1; \
                    143: }
                    144: UINT32 update_ops = 0;
                    145: UINT32 idle_ops = 0;
                    146: 
1.1.1.14  root      147: inline void maybe_idle()
                    148: {
                    149:        // if it appears to be in a tight loop, assume waiting for input
                    150:        // allow for one updated video character, for a spinning cursor
1.1.1.35  root      151:        if(!stay_busy && idle_ops < 1024 && vram_length_char <= 1 && vram_length_attr <= 1) {
1.1.1.14  root      152:                Sleep(10);
1.1.1.35  root      153:                REQUEST_HARDWRE_UPDATE();
1.1.1.14  root      154:        }
1.1.1.35  root      155:        idle_ops = 0;
1.1.1.14  root      156: }
1.1.1.12  root      157: 
1.1       root      158: /* ----------------------------------------------------------------------------
1.1.1.3   root      159:        MAME i86/i386
1.1       root      160: ---------------------------------------------------------------------------- */
                    161: 
1.1.1.10  root      162: #ifndef __BIG_ENDIAN__
1.1       root      163: #define LSB_FIRST
1.1.1.10  root      164: #endif
1.1       root      165: 
                    166: #ifndef INLINE
                    167: #define INLINE inline
                    168: #endif
                    169: #define U64(v) UINT64(v)
                    170: 
                    171: //#define logerror(...) fprintf(stderr, __VA_ARGS__)
                    172: #define logerror(...)
                    173: //#define popmessage(...) fprintf(stderr, __VA_ARGS__)
                    174: #define popmessage(...)
                    175: 
                    176: /*****************************************************************************/
1.1.1.10  root      177: /* src/emu/devcpu.h */
                    178: 
                    179: // CPU interface functions
                    180: #define CPU_INIT_NAME(name)                    cpu_init_##name
                    181: #define CPU_INIT(name)                         void CPU_INIT_NAME(name)()
                    182: #define CPU_INIT_CALL(name)                    CPU_INIT_NAME(name)()
                    183: 
                    184: #define CPU_RESET_NAME(name)                   cpu_reset_##name
                    185: #define CPU_RESET(name)                                void CPU_RESET_NAME(name)()
                    186: #define CPU_RESET_CALL(name)                   CPU_RESET_NAME(name)()
                    187: 
                    188: #define CPU_EXECUTE_NAME(name)                 cpu_execute_##name
                    189: #define CPU_EXECUTE(name)                      void CPU_EXECUTE_NAME(name)()
                    190: #define CPU_EXECUTE_CALL(name)                 CPU_EXECUTE_NAME(name)()
                    191: 
                    192: #define CPU_TRANSLATE_NAME(name)               cpu_translate_##name
                    193: #define CPU_TRANSLATE(name)                    int CPU_TRANSLATE_NAME(name)(address_spacenum space, int intention, offs_t *address)
                    194: #define CPU_TRANSLATE_CALL(name)               CPU_TRANSLATE_NAME(name)(space, intention, address)
                    195: 
                    196: #define CPU_DISASSEMBLE_NAME(name)             cpu_disassemble_##name
                    197: #define CPU_DISASSEMBLE(name)                  int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
                    198: #define CPU_DISASSEMBLE_CALL(name)             CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
                    199: 
1.1.1.14  root      200: #define CPU_MODEL_STR(name)                    #name
                    201: #define CPU_MODEL_NAME(name)                   CPU_MODEL_STR(name)
                    202: 
1.1.1.10  root      203: /*****************************************************************************/
                    204: /* src/emu/didisasm.h */
                    205: 
                    206: // Disassembler constants
                    207: const UINT32 DASMFLAG_SUPPORTED     = 0x80000000;   // are disassembly flags supported?
                    208: const UINT32 DASMFLAG_STEP_OUT      = 0x40000000;   // this instruction should be the end of a step out sequence
                    209: const UINT32 DASMFLAG_STEP_OVER     = 0x20000000;   // this instruction should be stepped over by setting a breakpoint afterwards
                    210: const UINT32 DASMFLAG_OVERINSTMASK  = 0x18000000;   // number of extra instructions to skip when stepping over
                    211: const UINT32 DASMFLAG_OVERINSTSHIFT = 27;           // bits to shift after masking to get the value
                    212: const UINT32 DASMFLAG_LENGTHMASK    = 0x0000ffff;   // the low 16-bits contain the actual length
                    213: 
                    214: /*****************************************************************************/
1.1       root      215: /* src/emu/diexec.h */
                    216: 
                    217: // I/O line states
                    218: enum line_state
                    219: {
                    220:        CLEAR_LINE = 0,                         // clear (a fired or held) line
                    221:        ASSERT_LINE,                            // assert an interrupt immediately
                    222:        HOLD_LINE,                              // hold interrupt line until acknowledged
                    223:        PULSE_LINE                              // pulse interrupt line instantaneously (only for NMI, RESET)
                    224: };
                    225: 
                    226: // I/O line definitions
                    227: enum
                    228: {
                    229:        INPUT_LINE_IRQ = 0,
                    230:        INPUT_LINE_NMI
                    231: };
                    232: 
                    233: /*****************************************************************************/
1.1.1.10  root      234: /* src/emu/dimemory.h */
1.1       root      235: 
1.1.1.10  root      236: // Translation intentions
                    237: const int TRANSLATE_TYPE_MASK       = 0x03;     // read write or fetch
                    238: const int TRANSLATE_USER_MASK       = 0x04;     // user mode or fully privileged
                    239: const int TRANSLATE_DEBUG_MASK      = 0x08;     // debug mode (no side effects)
                    240: 
                    241: const int TRANSLATE_READ            = 0;        // translate for read
                    242: const int TRANSLATE_WRITE           = 1;        // translate for write
                    243: const int TRANSLATE_FETCH           = 2;        // translate for instruction fetch
                    244: const int TRANSLATE_READ_USER       = (TRANSLATE_READ | TRANSLATE_USER_MASK);
                    245: const int TRANSLATE_WRITE_USER      = (TRANSLATE_WRITE | TRANSLATE_USER_MASK);
                    246: const int TRANSLATE_FETCH_USER      = (TRANSLATE_FETCH | TRANSLATE_USER_MASK);
                    247: const int TRANSLATE_READ_DEBUG      = (TRANSLATE_READ | TRANSLATE_DEBUG_MASK);
                    248: const int TRANSLATE_WRITE_DEBUG     = (TRANSLATE_WRITE | TRANSLATE_DEBUG_MASK);
                    249: const int TRANSLATE_FETCH_DEBUG     = (TRANSLATE_FETCH | TRANSLATE_DEBUG_MASK);
1.1       root      250: 
1.1.1.10  root      251: /*****************************************************************************/
                    252: /* src/emu/emucore.h */
1.1       root      253: 
1.1.1.10  root      254: // constants for expression endianness
                    255: enum endianness_t
                    256: {
                    257:        ENDIANNESS_LITTLE,
                    258:        ENDIANNESS_BIG
                    259: };
1.1       root      260: 
1.1.1.10  root      261: // declare native endianness to be one or the other
                    262: #ifdef LSB_FIRST
                    263: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
                    264: #else
                    265: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
                    266: #endif
                    267: 
                    268: // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
                    269: #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
                    270: 
                    271: // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
                    272: #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
                    273: 
                    274: // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
                    275: #define ENDIAN_VALUE_NE_NNE(endian,leval,beval)        (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
1.1       root      276: 
                    277: /*****************************************************************************/
                    278: /* src/emu/memory.h */
                    279: 
1.1.1.10  root      280: // address spaces
                    281: enum address_spacenum
                    282: {
                    283:        AS_0,                           // first address space
                    284:        AS_1,                           // second address space
                    285:        AS_2,                           // third address space
                    286:        AS_3,                           // fourth address space
                    287:        ADDRESS_SPACES,                 // maximum number of address spaces
                    288: 
                    289:        // alternate address space names for common use
                    290:        AS_PROGRAM = AS_0,              // program address space
                    291:        AS_DATA = AS_1,                 // data address space
                    292:        AS_IO = AS_2                    // I/O address space
                    293: };
                    294: 
1.1       root      295: // offsets and addresses are 32-bit (for now...)
1.1.1.30  root      296: //typedef UINT32       offs_t;
1.1       root      297: 
                    298: // read accessors
                    299: UINT8 read_byte(offs_t byteaddress)
1.1.1.33  root      300: #ifdef USE_DEBUGGER
                    301: {
                    302:        if(now_debugging) {
                    303:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    304:                        if(rd_break_point.table[i].status == 1) {
                    305:                                if(byteaddress == rd_break_point.table[i].addr) {
                    306:                                        rd_break_point.hit = i + 1;
                    307:                                        now_suspended = true;
                    308:                                        break;
                    309:                                }
                    310:                        }
                    311:                }
                    312:        }
                    313:        return(debugger_read_byte(byteaddress));
                    314: }
                    315: UINT8 debugger_read_byte(offs_t byteaddress)
                    316: #endif
1.1       root      317: {
1.1.1.4   root      318: #if defined(HAS_I386)
1.1       root      319:        if(byteaddress < MAX_MEM) {
                    320:                return mem[byteaddress];
1.1.1.3   root      321: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    322: //             return read_byte(byteaddress & 0xfffff);
1.1       root      323:        }
                    324:        return 0;
1.1.1.4   root      325: #else
                    326:        return mem[byteaddress];
                    327: #endif
1.1       root      328: }
                    329: 
                    330: UINT16 read_word(offs_t byteaddress)
1.1.1.33  root      331: #ifdef USE_DEBUGGER
                    332: {
                    333:        if(now_debugging) {
                    334:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    335:                        if(rd_break_point.table[i].status == 1) {
                    336:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 2) {
                    337:                                        rd_break_point.hit = i + 1;
                    338:                                        now_suspended = true;
                    339:                                        break;
                    340:                                }
                    341:                        }
                    342:                }
                    343:        }
                    344:        return(debugger_read_word(byteaddress));
                    345: }
                    346: UINT16 debugger_read_word(offs_t byteaddress)
                    347: #endif
1.1       root      348: {
1.1.1.14  root      349:        if(byteaddress == 0x41c) {
                    350:                // pointer to first free slot in keyboard buffer
                    351:                // XXX: the buffer itself doesn't actually exist in DOS memory
1.1.1.35  root      352:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                    353: #ifdef USE_SERVICE_THREAD
                    354:                        EnterCriticalSection(&key_buf_crit_sect);
                    355: #endif
1.1.1.41  root      356:                        int count = min(key_buf_char->count(), 15);
                    357:                        // write to top of key buffer
                    358:                        for(int i = 0; i < count; i++) {
                    359:                                mem[0x41e + 2 * i + 0] = key_buf_char->read_not_remove(i);
                    360:                                mem[0x41e + 2 * i + 1] = key_buf_scan->read_not_remove(i);
                    361:                        }
1.1.1.35  root      362: #ifdef USE_SERVICE_THREAD
                    363:                        LeaveCriticalSection(&key_buf_crit_sect);
                    364: #endif
                    365:                        if(count == 0) {
1.1.1.32  root      366:                                maybe_idle();
                    367:                        }
1.1.1.41  root      368:                        return (UINT16)(0x1e + 2 * count);
1.1.1.14  root      369:                }
1.1.1.41  root      370:                return 0x1e;
1.1.1.14  root      371:        }
1.1.1.4   root      372: #if defined(HAS_I386)
1.1       root      373:        if(byteaddress < MAX_MEM - 1) {
                    374:                return *(UINT16 *)(mem + byteaddress);
1.1.1.3   root      375: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    376: //             return read_word(byteaddress & 0xfffff);
1.1       root      377:        }
                    378:        return 0;
1.1.1.4   root      379: #else
                    380:        return *(UINT16 *)(mem + byteaddress);
                    381: #endif
1.1       root      382: }
                    383: 
                    384: UINT32 read_dword(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 + 4) {
                    391:                                        rd_break_point.hit = i + 1;
                    392:                                        now_suspended = true;
                    393:                                        break;
                    394:                                }
                    395:                        }
                    396:                }
                    397:        }
                    398:        return(debugger_read_dword(byteaddress));
                    399: }
                    400: UINT32 debugger_read_dword(offs_t byteaddress)
                    401: #endif
1.1       root      402: {
1.1.1.4   root      403: #if defined(HAS_I386)
1.1       root      404:        if(byteaddress < MAX_MEM - 3) {
                    405:                return *(UINT32 *)(mem + byteaddress);
1.1.1.3   root      406: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    407: //             return read_dword(byteaddress & 0xfffff);
1.1       root      408:        }
                    409:        return 0;
1.1.1.4   root      410: #else
                    411:        return *(UINT32 *)(mem + byteaddress);
                    412: #endif
1.1       root      413: }
                    414: 
                    415: // write accessors
1.1.1.35  root      416: #ifdef USE_VRAM_THREAD
1.1.1.14  root      417: void vram_flush_char()
                    418: {
                    419:        if(vram_length_char != 0) {
                    420:                DWORD num;
1.1.1.23  root      421:                WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, vram_length_char, vram_coord_char, &num);
1.1.1.14  root      422:                vram_length_char = vram_last_length_char = 0;
                    423:        }
                    424: }
                    425: 
                    426: void vram_flush_attr()
                    427: {
                    428:        if(vram_length_attr != 0) {
                    429:                DWORD num;
1.1.1.23  root      430:                WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, vram_length_attr, vram_coord_attr, &num);
1.1.1.14  root      431:                vram_length_attr = vram_last_length_attr = 0;
                    432:        }
                    433: }
                    434: 
                    435: void vram_flush()
                    436: {
                    437:        if(vram_length_char != 0 || vram_length_attr != 0) {
                    438:                EnterCriticalSection(&vram_crit_sect);
                    439:                vram_flush_char();
                    440:                vram_flush_attr();
                    441:                LeaveCriticalSection(&vram_crit_sect);
                    442:        }
                    443: }
                    444: #endif
                    445: 
                    446: void write_text_vram_char(offs_t offset, UINT8 data)
1.1.1.8   root      447: {
1.1.1.35  root      448: #ifdef USE_VRAM_THREAD
1.1.1.14  root      449:        static offs_t first_offset_char, last_offset_char;
                    450:        
                    451:        if(vram_length_char != 0) {
                    452:                if(offset <= last_offset_char && offset >= first_offset_char) {
                    453:                        scr_char[(offset - first_offset_char) >> 1] = data;
                    454:                        return;
                    455:                }
                    456:                if(offset != last_offset_char + 2) {
                    457:                        vram_flush_char();
                    458:                }
                    459:        }
                    460:        if(vram_length_char == 0) {
                    461:                first_offset_char = offset;
                    462:                vram_coord_char.X = (offset >> 1) % scr_width;
                    463:                vram_coord_char.Y = (offset >> 1) / scr_width + scr_top;
                    464:        }
                    465:        scr_char[vram_length_char++] = data;
                    466:        last_offset_char = offset;
                    467: #else
1.1.1.8   root      468:        COORD co;
                    469:        DWORD num;
                    470:        
1.1.1.14  root      471:        co.X = (offset >> 1) % scr_width;
                    472:        co.Y = (offset >> 1) / scr_width;
                    473:        scr_char[0] = data;
1.1.1.23  root      474:        WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, 1, co, &num);
1.1.1.14  root      475: #endif
                    476: }
                    477: 
                    478: void write_text_vram_attr(offs_t offset, UINT8 data)
                    479: {
1.1.1.35  root      480: #ifdef USE_VRAM_THREAD
1.1.1.14  root      481:        static offs_t first_offset_attr, last_offset_attr;
                    482:        
                    483:        if(vram_length_attr != 0) {
                    484:                if(offset <= last_offset_attr && offset >= first_offset_attr) {
                    485:                        scr_attr[(offset - first_offset_attr) >> 1] = data;
                    486:                        return;
                    487:                }
                    488:                if(offset != last_offset_attr + 2) {
                    489:                        vram_flush_attr();
                    490:                }
                    491:        }
                    492:        if(vram_length_attr == 0) {
                    493:                first_offset_attr = offset;
                    494:                vram_coord_attr.X = (offset >> 1) % scr_width;
                    495:                vram_coord_attr.Y = (offset >> 1) / scr_width + scr_top;
                    496:        }
                    497:        scr_attr[vram_length_attr++] = data;
                    498:        last_offset_attr = offset;
                    499: #else
                    500:        COORD co;
                    501:        DWORD num;
1.1.1.8   root      502:        
1.1.1.14  root      503:        co.X = (offset >> 1) % scr_width;
                    504:        co.Y = (offset >> 1) / scr_width;
                    505:        scr_attr[0] = data;
1.1.1.23  root      506:        WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, 1, co, &num);
1.1.1.14  root      507: #endif
                    508: }
                    509: 
                    510: void write_text_vram_byte(offs_t offset, UINT8 data)
                    511: {
1.1.1.35  root      512: #ifdef USE_VRAM_THREAD
1.1.1.14  root      513:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      514: #endif
1.1.1.8   root      515:        if(offset & 1) {
1.1.1.14  root      516:                write_text_vram_attr(offset, data);
1.1.1.8   root      517:        } else {
1.1.1.14  root      518:                write_text_vram_char(offset, data);
1.1.1.8   root      519:        }
1.1.1.35  root      520: #ifdef USE_VRAM_THREAD
1.1.1.14  root      521:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      522: #endif
1.1.1.8   root      523: }
                    524: 
                    525: void write_text_vram_word(offs_t offset, UINT16 data)
                    526: {
1.1.1.35  root      527: #ifdef USE_VRAM_THREAD
1.1.1.14  root      528:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      529: #endif
1.1.1.8   root      530:        if(offset & 1) {
1.1.1.14  root      531:                write_text_vram_attr(offset    , (data     ) & 0xff);
                    532:                write_text_vram_char(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      533:        } else {
1.1.1.14  root      534:                write_text_vram_char(offset    , (data     ) & 0xff);
                    535:                write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      536:        }
1.1.1.35  root      537: #ifdef USE_VRAM_THREAD
1.1.1.14  root      538:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      539: #endif
1.1.1.8   root      540: }
                    541: 
                    542: void write_text_vram_dword(offs_t offset, UINT32 data)
                    543: {
1.1.1.35  root      544: #ifdef USE_VRAM_THREAD
1.1.1.14  root      545:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      546: #endif
1.1.1.8   root      547:        if(offset & 1) {
1.1.1.14  root      548:                write_text_vram_attr(offset    , (data      ) & 0xff);
                    549:                write_text_vram_char(offset + 1, (data >>  8) & 0xff);
                    550:                write_text_vram_attr(offset + 2, (data >> 16) & 0xff);
                    551:                write_text_vram_char(offset + 3, (data >> 24) & 0xff);
                    552:        } else {
                    553:                write_text_vram_char(offset    , (data      ) & 0xff);
                    554:                write_text_vram_attr(offset + 1, (data >>  8) & 0xff);
                    555:                write_text_vram_char(offset + 2, (data >> 16) & 0xff);
                    556:                write_text_vram_attr(offset + 3, (data >> 24) & 0xff);
1.1.1.8   root      557:        }
1.1.1.35  root      558: #ifdef USE_VRAM_THREAD
1.1.1.14  root      559:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      560: #endif
1.1.1.8   root      561: }
                    562: 
1.1       root      563: void write_byte(offs_t byteaddress, UINT8 data)
1.1.1.33  root      564: #ifdef USE_DEBUGGER
                    565: {
                    566:        if(now_debugging) {
                    567:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    568:                        if(wr_break_point.table[i].status == 1) {
                    569:                                if(byteaddress == wr_break_point.table[i].addr) {
                    570:                                        wr_break_point.hit = i + 1;
                    571:                                        now_suspended = true;
                    572:                                        break;
                    573:                                }
                    574:                        }
                    575:                }
                    576:        }
                    577:        debugger_write_byte(byteaddress, data);
                    578: }
                    579: void debugger_write_byte(offs_t byteaddress, UINT8 data)
                    580: #endif
1.1       root      581: {
1.1.1.8   root      582:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      583:                mem[byteaddress] = data;
1.1.1.8   root      584:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      585:                if(!restore_console_on_exit) {
                    586:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      587:                }
1.1.1.8   root      588:                write_text_vram_byte(byteaddress - text_vram_top_address, data);
                    589:                mem[byteaddress] = data;
                    590:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    591:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    592:                        write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1       root      593:                }
                    594:                mem[byteaddress] = data;
1.1.1.4   root      595: #if defined(HAS_I386)
1.1.1.3   root      596:        } else if(byteaddress < MAX_MEM) {
1.1.1.4   root      597: #else
                    598:        } else {
                    599: #endif
1.1.1.3   root      600:                mem[byteaddress] = data;
1.1       root      601:        }
                    602: }
                    603: 
                    604: void write_word(offs_t byteaddress, UINT16 data)
1.1.1.33  root      605: #ifdef USE_DEBUGGER
                    606: {
                    607:        if(now_debugging) {
                    608:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    609:                        if(wr_break_point.table[i].status == 1) {
                    610:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 2) {
                    611:                                        wr_break_point.hit = i + 1;
                    612:                                        now_suspended = true;
                    613:                                        break;
                    614:                                }
                    615:                        }
                    616:                }
                    617:        }
                    618:        debugger_write_word(byteaddress, data);
                    619: }
                    620: void debugger_write_word(offs_t byteaddress, UINT16 data)
                    621: #endif
1.1       root      622: {
1.1.1.8   root      623:        if(byteaddress < MEMORY_END) {
1.1.1.14  root      624:                if(byteaddress == 0x450 + mem[0x462] * 2) {
                    625:                        COORD co;
                    626:                        co.X = data & 0xff;
                    627:                        co.Y = (data >> 8) + scr_top;
1.1.1.23  root      628:                        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), co);
1.1.1.14  root      629:                }
1.1.1.3   root      630:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8   root      631:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      632:                if(!restore_console_on_exit) {
                    633:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      634:                }
1.1.1.8   root      635:                write_text_vram_word(byteaddress - text_vram_top_address, data);
                    636:                *(UINT16 *)(mem + byteaddress) = data;
                    637:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    638:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    639:                        write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1       root      640:                }
                    641:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4   root      642: #if defined(HAS_I386)
1.1.1.3   root      643:        } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4   root      644: #else
                    645:        } else {
                    646: #endif
1.1.1.3   root      647:                *(UINT16 *)(mem + byteaddress) = data;
1.1       root      648:        }
                    649: }
                    650: 
                    651: void write_dword(offs_t byteaddress, UINT32 data)
1.1.1.33  root      652: #ifdef USE_DEBUGGER
                    653: {
                    654:        if(now_debugging) {
                    655:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    656:                        if(wr_break_point.table[i].status == 1) {
                    657:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 4) {
                    658:                                        wr_break_point.hit = i + 1;
                    659:                                        now_suspended = true;
                    660:                                        break;
                    661:                                }
                    662:                        }
                    663:                }
                    664:        }
                    665:        debugger_write_dword(byteaddress, data);
                    666: }
                    667: void debugger_write_dword(offs_t byteaddress, UINT32 data)
                    668: #endif
1.1       root      669: {
1.1.1.8   root      670:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      671:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8   root      672:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      673:                if(!restore_console_on_exit) {
                    674:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      675:                }
1.1.1.8   root      676:                write_text_vram_dword(byteaddress - text_vram_top_address, data);
                    677:                *(UINT32 *)(mem + byteaddress) = data;
                    678:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    679:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    680:                        write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1       root      681:                }
                    682:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4   root      683: #if defined(HAS_I386)
1.1.1.3   root      684:        } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4   root      685: #else
                    686:        } else {
                    687: #endif
1.1.1.3   root      688:                *(UINT32 *)(mem + byteaddress) = data;
1.1       root      689:        }
                    690: }
                    691: 
                    692: #define read_decrypted_byte read_byte
                    693: #define read_decrypted_word read_word
                    694: #define read_decrypted_dword read_dword
                    695: 
1.1.1.3   root      696: #define read_raw_byte read_byte
                    697: #define write_raw_byte write_byte
                    698: 
                    699: #define read_word_unaligned read_word
                    700: #define write_word_unaligned write_word
                    701: 
                    702: #define read_io_word_unaligned read_io_word
                    703: #define write_io_word_unaligned write_io_word
                    704: 
1.1       root      705: UINT8 read_io_byte(offs_t byteaddress);
                    706: UINT16 read_io_word(offs_t byteaddress);
                    707: UINT32 read_io_dword(offs_t byteaddress);
                    708: 
                    709: void write_io_byte(offs_t byteaddress, UINT8 data);
                    710: void write_io_word(offs_t byteaddress, UINT16 data);
                    711: void write_io_dword(offs_t byteaddress, UINT32 data);
                    712: 
                    713: /*****************************************************************************/
                    714: /* src/osd/osdcomm.h */
                    715: 
                    716: /* Highly useful macro for compile-time knowledge of an array size */
                    717: #define ARRAY_LENGTH(x)     (sizeof(x) / sizeof(x[0]))
                    718: 
1.1.1.3   root      719: #if defined(HAS_I386)
1.1.1.10  root      720:        static CPU_TRANSLATE(i386);
                    721:        #include "mame/lib/softfloat/softfloat.c"
                    722:        #include "mame/emu/cpu/i386/i386.c"
1.1.1.12  root      723:        #include "mame/emu/cpu/vtlb.c"
1.1.1.3   root      724: #elif defined(HAS_I286)
1.1.1.10  root      725:        #include "mame/emu/cpu/i86/i286.c"
1.1.1.3   root      726: #else
1.1.1.10  root      727:        #include "mame/emu/cpu/i86/i86.c"
1.1.1.3   root      728: #endif
1.1.1.33  root      729: #ifdef USE_DEBUGGER
1.1.1.10  root      730:        #include "mame/emu/cpu/i386/i386dasm.c"
1.1       root      731: #endif
                    732: 
1.1.1.3   root      733: #if defined(HAS_I386)
                    734:        #define SREG(x)                         m_sreg[x].selector
                    735:        #define SREG_BASE(x)                    m_sreg[x].base
                    736:        int cpu_type, cpu_step;
                    737: #else
                    738:        #define REG8(x)                         m_regs.b[x]
                    739:        #define REG16(x)                        m_regs.w[x]
                    740:        #define SREG(x)                         m_sregs[x]
                    741:        #define SREG_BASE(x)                    m_base[x]
1.1.1.33  root      742:        #define m_eip                           (m_pc - m_base[CS])
1.1.1.3   root      743:        #define m_CF                            m_CarryVal
                    744:        #define m_a20_mask                      AMASK
                    745:        #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
                    746:        #if defined(HAS_I286)
                    747:                #define i386_set_a20_line(x)    i80286_set_a20_line(x)
                    748:        #else
                    749:                #define i386_set_a20_line(x)
                    750:        #endif
                    751:        #define i386_set_irq_line(x, y)         set_irq_line(x, y)
                    752: #endif
1.1       root      753: 
                    754: void i386_jmp_far(UINT16 selector, UINT32 address)
                    755: {
1.1.1.3   root      756: #if defined(HAS_I386)
1.1       root      757:        if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3   root      758:                i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1       root      759:        } else {
1.1.1.3   root      760:                SREG(CS) = selector;
                    761:                m_performed_intersegment_jump = 1;
                    762:                i386_load_segment_descriptor(CS);
                    763:                m_eip = address;
                    764:                CHANGE_PC(m_eip);
1.1       root      765:        }
1.1.1.3   root      766: #elif defined(HAS_I286)
                    767:        i80286_code_descriptor(selector, address, 1);
                    768: #else
                    769:        SREG(CS) = selector;
                    770:        i386_load_segment_descriptor(CS);
                    771:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    772: #endif
1.1       root      773: }
                    774: 
1.1.1.24  root      775: void i386_call_far(UINT16 selector, UINT32 address)
                    776: {
                    777: #if defined(HAS_I386)
                    778:        if(PROTECTED_MODE && !V8086_MODE) {
                    779:                i386_protected_mode_call(selector, address, 1, m_operand_size);
                    780:        } else {
                    781:                PUSH16(SREG(CS));
                    782:                PUSH16(m_eip);
                    783:                SREG(CS) = selector;
                    784:                m_performed_intersegment_jump = 1;
                    785:                i386_load_segment_descriptor(CS);
                    786:                m_eip = address;
                    787:                CHANGE_PC(m_eip);
                    788:        }
                    789: #else
                    790:        UINT16 ip = m_pc - SREG_BASE(CS);
                    791:        UINT16 cs = SREG(CS);
                    792: #if defined(HAS_I286)
                    793:        i80286_code_descriptor(selector, address, 2);
                    794: #else
                    795:        SREG(CS) = selector;
                    796:        i386_load_segment_descriptor(CS);
                    797:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    798: #endif
                    799:        PUSH(cs);
                    800:        PUSH(ip);
                    801:        CHANGE_PC(m_pc);
                    802: #endif
                    803: }
1.1.1.49  root      804: 
                    805: void i386_push16(UINT16 value)
                    806: {
                    807: #if defined(HAS_I386)
                    808:        PUSH16(value);
                    809: #else
                    810:        PUSH(value);
1.1.1.35  root      811: #endif
1.1.1.49  root      812: }
                    813: 
                    814: UINT16 i386_pop16()
                    815: {
                    816: #if defined(HAS_I386)
                    817:        return POP16();
                    818: #else
                    819:        UINT16 value;
                    820:        POP(value);
                    821:        return value;
                    822: #endif
                    823: }
1.1.1.24  root      824: 
1.1.1.29  root      825: UINT16 i386_read_stack()
                    826: {
                    827: #if defined(HAS_I386)
                    828:        UINT32 ea, new_esp;
                    829:        if( STACK_32BIT ) {
                    830:                new_esp = REG32(ESP) + 2;
                    831:                ea = i386_translate(SS, new_esp - 2, 0);
                    832:        } else {
                    833:                new_esp = REG16(SP) + 2;
                    834:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    835:        }
                    836:        return READ16(ea);
                    837: #else
                    838:        UINT16 sp = m_regs.w[SP] + 2;
                    839:        return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
                    840: #endif
                    841: }
                    842: 
1.1       root      843: /* ----------------------------------------------------------------------------
1.1.1.33  root      844:        debugger
                    845: ---------------------------------------------------------------------------- */
                    846: 
                    847: #ifdef USE_DEBUGGER
                    848: #define TELNET_BLUE      0x0004 // text color contains blue.
                    849: #define TELNET_GREEN     0x0002 // text color contains green.
                    850: #define TELNET_RED       0x0001 // text color contains red.
                    851: #define TELNET_INTENSITY 0x0008 // text color is intensified.
                    852: 
                    853: int svr_socket = 0;
                    854: int cli_socket = 0;
                    855: 
                    856: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error);
                    857: 
                    858: void debugger_init()
                    859: {
                    860:        now_debugging = false;
                    861:        now_going = false;
                    862:        now_suspended = false;
                    863:        force_suspend = false;
                    864:        
                    865:        memset(&break_point, 0, sizeof(break_point_t));
                    866:        memset(&rd_break_point, 0, sizeof(break_point_t));
                    867:        memset(&wr_break_point, 0, sizeof(break_point_t));
                    868:        memset(&in_break_point, 0, sizeof(break_point_t));
                    869:        memset(&out_break_point, 0, sizeof(break_point_t));
                    870:        memset(&int_break_point, 0, sizeof(int_break_point_t));
                    871: }
                    872: 
1.1.1.45  root      873: void telnet_send(const char *string)
1.1.1.33  root      874: {
                    875:        char buffer[8192], *ptr;
                    876:        strcpy(buffer, string);
                    877:        while((ptr = strstr(buffer, "\n")) != NULL) {
                    878:                char tmp[8192];
                    879:                *ptr = '\0';
                    880:                sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
                    881:                strcpy(buffer, tmp);
                    882:        }
                    883:        
                    884:        int len = strlen(buffer), res;
                    885:        ptr = buffer;
                    886:        while(len > 0) {
                    887:                if((res = send(cli_socket, ptr, len, 0)) > 0) {
                    888:                        len -= res;
                    889:                        ptr += res;
                    890:                }
                    891:        }
                    892: }
                    893: 
                    894: void telnet_command(const char *format, ...)
                    895: {
                    896:        char buffer[1024];
                    897:        va_list ap;
                    898:        va_start(ap, format);
                    899:        vsprintf(buffer, format, ap);
                    900:        va_end(ap);
                    901:        
                    902:        telnet_send(buffer);
                    903: }
                    904: 
                    905: void telnet_printf(const char *format, ...)
                    906: {
                    907:        char buffer[1024];
                    908:        va_list ap;
                    909:        va_start(ap, format);
                    910:        vsprintf(buffer, format, ap);
                    911:        va_end(ap);
                    912:        
                    913:        if(fp_debugger != NULL) {
                    914:                fprintf(fp_debugger, "%s", buffer);
                    915:        }
                    916:        telnet_send(buffer);
                    917: }
                    918: 
                    919: bool telnet_gets(char *str, int n)
                    920: {
                    921:        char buffer[1024];
                    922:        int ptr = 0;
                    923:        
                    924:        telnet_command("\033[12l"); // local echo on
                    925:        telnet_command("\033[2l");  // key unlock
                    926:        
                    927:        while(!m_halted) {
                    928:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    929:                
                    930:                if(len > 0 && buffer[0] != 0xff) {
                    931:                        for(int i = 0; i < len; i++) {
                    932:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                    933:                                        str[ptr] = 0;
                    934:                                        telnet_command("\033[2h");  // key lock
                    935:                                        telnet_command("\033[12h"); // local echo off
                    936:                                        return(!m_halted);
                    937:                                } else if(buffer[i] == 0x08) {
                    938:                                        if(ptr > 0) {
                    939:                                                telnet_command("\033[0K"); // erase from cursor position
                    940:                                                ptr--;
                    941:                                        } else {
                    942:                                                telnet_command("\033[1C"); // move cursor forward
                    943:                                        }
                    944:                                } else if(ptr < n - 1) {
1.1.1.37  root      945:                                        if(buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
1.1.1.33  root      946:                                                str[ptr++] = buffer[i];
                    947:                                        }
                    948:                                } else {
                    949:                                        telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
                    950:                                }
                    951:                        }
                    952:                } else if(len == -1) {
                    953:                        if(WSAGetLastError() != WSAEWOULDBLOCK) {
                    954:                                return(false);
                    955:                        }
                    956:                } else if(len == 0) {
                    957:                        return(false);
                    958:                }
                    959:                Sleep(10);
                    960:        }
                    961:        return(!m_halted);
                    962: }
                    963: 
                    964: bool telnet_kbhit()
                    965: {
                    966:        char buffer[1024];
                    967:        
                    968:        if(!m_halted) {
                    969:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    970:                
                    971:                if(len > 0) {
                    972:                        for(int i = 0; i < len; i++) {
                    973:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                    974:                                        return(true);
                    975:                                }
                    976:                        }
                    977:                } else if(len == 0) {
                    978:                        return(true); // disconnected
                    979:                }
                    980:        }
                    981:        return(false);
                    982: }
                    983: 
                    984: bool telnet_disconnected()
                    985: {
                    986:        char buffer[1024];
                    987:        int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    988:        
                    989:        if(len == 0) {
                    990:                return(true);
                    991:        } else if(len == -1) {
                    992:                if(WSAGetLastError() != WSAEWOULDBLOCK) {
                    993:                        return(true);
                    994:                }
                    995:        }
                    996:        return(false);
                    997: }
                    998: 
                    999: void telnet_set_color(int color)
                   1000: {
                   1001:        telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
                   1002: }
                   1003: 
                   1004: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
                   1005: {
                   1006: //     UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
                   1007:        UINT8 ops[16];
                   1008:        for(int i = 0; i < 16; i++) {
                   1009:                ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
                   1010:        }
                   1011:        UINT8 *oprom = ops;
                   1012:        
                   1013: #if defined(HAS_I386)
                   1014:        if(m_operand_size) {
                   1015:                return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
                   1016:        } else
                   1017: #endif
                   1018:        return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
                   1019: }
                   1020: 
                   1021: void debugger_regs_info(char *buffer)
                   1022: {
                   1023: #if defined(HAS_I386)
                   1024:        UINT32 flags = get_flags();
                   1025: #else
                   1026:        UINT32 flags = CompressFlags();
                   1027: #endif
                   1028: #if defined(HAS_I386)
                   1029:        if(m_operand_size) {
                   1030:                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",
                   1031:                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),
                   1032:                PROTECTED_MODE ? "PE" : "--",
                   1033:                (flags & 0x40000) ? 'A' : '-',
                   1034:                (flags & 0x20000) ? 'V' : '-',
                   1035:                (flags & 0x10000) ? 'R' : '-',
                   1036:                (flags & 0x04000) ? 'N' : '-',
                   1037:                (flags & 0x02000) ? '1' : '0',
                   1038:                (flags & 0x01000) ? '1' : '0',
                   1039:                (flags & 0x00800) ? 'O' : '-',
                   1040:                (flags & 0x00400) ? 'D' : '-',
                   1041:                (flags & 0x00200) ? 'I' : '-',
                   1042:                (flags & 0x00100) ? 'T' : '-',
                   1043:                (flags & 0x00080) ? 'S' : '-',
                   1044:                (flags & 0x00040) ? 'Z' : '-',
                   1045:                (flags & 0x00010) ? 'A' : '-',
                   1046:                (flags & 0x00004) ? 'P' : '-',
                   1047:                (flags & 0x00001) ? 'C' : '-');
                   1048:        } else {
                   1049: #endif
                   1050:                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",
                   1051:                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),
                   1052: #if defined(HAS_I386)
                   1053:                PROTECTED_MODE ? "PE" : "--",
                   1054: #else
                   1055:                "--",
                   1056: #endif
                   1057:                (flags & 0x40000) ? 'A' : '-',
                   1058:                (flags & 0x20000) ? 'V' : '-',
                   1059:                (flags & 0x10000) ? 'R' : '-',
                   1060:                (flags & 0x04000) ? 'N' : '-',
                   1061:                (flags & 0x02000) ? '1' : '0',
                   1062:                (flags & 0x01000) ? '1' : '0',
                   1063:                (flags & 0x00800) ? 'O' : '-',
                   1064:                (flags & 0x00400) ? 'D' : '-',
                   1065:                (flags & 0x00200) ? 'I' : '-',
                   1066:                (flags & 0x00100) ? 'T' : '-',
                   1067:                (flags & 0x00080) ? 'S' : '-',
                   1068:                (flags & 0x00040) ? 'Z' : '-',
                   1069:                (flags & 0x00010) ? 'A' : '-',
                   1070:                (flags & 0x00004) ? 'P' : '-',
                   1071:                (flags & 0x00001) ? 'C' : '-');
                   1072: #if defined(HAS_I386)
                   1073:        }
                   1074: #endif
                   1075: }
                   1076: 
                   1077: void debugger_process_info(char *buffer)
                   1078: {
                   1079:        UINT16 psp_seg = current_psp;
                   1080:        process_t *process;
                   1081:        bool check[0x10000] = {0};
                   1082:        
                   1083:        buffer[0] = '\0';
                   1084:        
                   1085:        while(!check[psp_seg] && (process  = msdos_process_info_get(psp_seg, false)) != NULL) {
                   1086:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1087:                char *file = process->module_path, *s;
                   1088:                char tmp[8192];
                   1089:                
                   1090:                while((s = strstr(file, "\\")) != NULL) {
                   1091:                        file = s + 1;
                   1092:                }
                   1093:                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));
                   1094:                strcat(tmp, buffer);
                   1095:                strcpy(buffer, tmp);
                   1096:                
                   1097:                check[psp_seg] = true;
                   1098:                psp_seg = psp->parent_psp;
                   1099:        }
                   1100: }
                   1101: 
                   1102: UINT32 debugger_get_val(const char *str)
                   1103: {
                   1104:        char tmp[1024];
                   1105:        
                   1106:        if(str == NULL || strlen(str) == 0) {
                   1107:                return(0);
                   1108:        }
                   1109:        strcpy(tmp, str);
                   1110:        
                   1111:        if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
                   1112:                // ank
                   1113:                return(tmp[1] & 0xff);
                   1114:        } else if(tmp[0] == '%') {
                   1115:                // decimal
                   1116:                return(strtoul(tmp + 1, NULL, 10));
                   1117:        }
                   1118:        return(strtoul(tmp, NULL, 16));
                   1119: }
                   1120: 
                   1121: UINT32 debugger_get_seg(const char *str, UINT32 val)
                   1122: {
                   1123:        char tmp[1024], *s;
                   1124:        
                   1125:        if(str == NULL || strlen(str) == 0) {
                   1126:                return(val);
                   1127:        }
                   1128:        strcpy(tmp, str);
                   1129:        
                   1130:        if((s = strstr(tmp, ":")) != NULL) {
                   1131:                // 0000:0000
                   1132:                *s = '\0';
                   1133:                return(debugger_get_val(tmp));
                   1134:        }
                   1135:        return(val);
                   1136: }
                   1137: 
                   1138: UINT32 debugger_get_ofs(const char *str)
                   1139: {
                   1140:        char tmp[1024], *s;
                   1141:        
                   1142:        if(str == NULL || strlen(str) == 0) {
                   1143:                return(0);
                   1144:        }
                   1145:        strcpy(tmp, str);
                   1146:        
                   1147:        if((s = strstr(tmp, ":")) != NULL) {
                   1148:                // 0000:0000
                   1149:                return(debugger_get_val(s + 1));
                   1150:        }
                   1151:        return(debugger_get_val(tmp));
                   1152: }
                   1153: 
                   1154: void debugger_main()
                   1155: {
                   1156:        telnet_command("\033[20h"); // cr-lf
                   1157:        
                   1158:        force_suspend = true;
                   1159:        now_going = false;
                   1160:        now_debugging = true;
                   1161:        Sleep(100);
                   1162:        
                   1163:        if(!m_halted && !now_suspended) {
                   1164:                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1165:                telnet_printf("waiting until cpu is suspended...\n");
                   1166:        }
                   1167:        while(!m_halted && !now_suspended) {
                   1168:                if(telnet_disconnected()) {
                   1169:                        break;
                   1170:                }
                   1171:                Sleep(10);
                   1172:        }
                   1173:        
                   1174:        char buffer[8192];
                   1175:        
                   1176:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1177:        debugger_process_info(buffer);
                   1178:        telnet_printf("%s", buffer);
                   1179:        debugger_regs_info(buffer);
                   1180:        telnet_printf("%s", buffer);
                   1181:        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1182:        telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
                   1183:        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1184:        debugger_dasm(buffer, SREG(CS), m_eip);
                   1185:        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1186:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1187:        
                   1188:        #define MAX_COMMAND_LEN 64
                   1189:        
                   1190:        char command[MAX_COMMAND_LEN + 1];
                   1191:        char prev_command[MAX_COMMAND_LEN + 1] = {0};
                   1192:        
                   1193:        UINT32 data_seg = SREG(DS);
                   1194:        UINT32 data_ofs = 0;
                   1195:        UINT32 dasm_seg = SREG(CS);
                   1196:        UINT32 dasm_ofs = m_eip;
                   1197:        
                   1198:        while(!m_halted) {
                   1199:                telnet_printf("- ");
                   1200:                command[0] = '\0';
                   1201:                
                   1202:                if(fi_debugger != NULL) {
                   1203:                        while(command[0] == '\0') {
                   1204:                                if(fgets(command, sizeof(command), fi_debugger) == NULL) {
                   1205:                                        break;
                   1206:                                }
                   1207:                                while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
                   1208:                                        command[strlen(command) - 1] = '\0';
                   1209:                                }
                   1210:                        }
                   1211:                        if(command[0] != '\0') {
                   1212:                                telnet_command("%s\n", command);
                   1213:                        }
                   1214:                }
                   1215:                if(command[0] == '\0') {
                   1216:                        if(!telnet_gets(command, sizeof(command))) {
                   1217:                                break;
                   1218:                        }
                   1219:                }
                   1220:                if(command[0] == '\0') {
                   1221:                        strcpy(command, prev_command);
                   1222:                } else {
                   1223:                        strcpy(prev_command, command);
                   1224:                }
                   1225:                if(fp_debugger != NULL) {
                   1226:                        fprintf(fp_debugger, "%s\n", command);
                   1227:                }
                   1228:                
                   1229:                if(!m_halted && command[0] != 0) {
                   1230:                        char *params[32], *token = NULL;
                   1231:                        int num = 0;
                   1232:                        
                   1233:                        if((token = strtok(command, " ")) != NULL) {
                   1234:                                params[num++] = token;
                   1235:                                while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
                   1236:                                        params[num++] = token;
                   1237:                                }
                   1238:                        }
                   1239:                        if(stricmp(params[0], "D") == 0) {
                   1240:                                if(num <= 3) {
                   1241:                                        if(num >= 2) {
                   1242:                                                data_seg = debugger_get_seg(params[1], data_seg);
                   1243:                                                data_ofs = debugger_get_ofs(params[1]);
                   1244:                                        }
                   1245:                                        UINT32 end_seg = data_seg;
                   1246:                                        UINT32 end_ofs = data_ofs + 8 * 16 - 1;
                   1247:                                        if(num == 3) {
                   1248:                                                end_seg = debugger_get_seg(params[2], data_seg);
                   1249:                                                end_ofs = debugger_get_ofs(params[2]);
                   1250:                                        }
                   1251:                                        UINT64 start_addr = (data_seg << 4) + data_ofs;
                   1252:                                        UINT64 end_addr = (end_seg << 4) + end_ofs;
1.1.1.37  root     1253: //                                     bool is_sjis = false;
1.1.1.33  root     1254:                                        
                   1255:                                        for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
                   1256:                                                if((addr & 0x0f) == 0) {
                   1257:                                                        if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
                   1258:                                                                data_seg += 0x1000;
                   1259:                                                                data_ofs -= 0x10000;
                   1260:                                                        }
                   1261:                                                        telnet_printf("%06X:%04X ", data_seg, data_ofs);
                   1262:                                                        memset(buffer, 0, sizeof(buffer));
                   1263:                                                }
                   1264:                                                if(addr < start_addr || addr > end_addr) {
                   1265:                                                        telnet_printf("   ");
                   1266:                                                        buffer[addr & 0x0f] = ' ';
                   1267:                                                } else {
                   1268:                                                        UINT8 data = debugger_read_byte(addr & ADDR_MASK);
                   1269:                                                        telnet_printf(" %02X", data);
1.1.1.37  root     1270: //                                                     if(is_sjis) {
1.1.1.33  root     1271: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1272: //                                                             is_sjis = false;
1.1.1.33  root     1273: //                                                     } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
                   1274: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1275: //                                                             is_sjis = true;
1.1.1.33  root     1276: //                                                     } else
                   1277:                                                        if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
                   1278:                                                                buffer[addr & 0x0f] = data;
                   1279:                                                        } else {
                   1280:                                                                buffer[addr & 0x0f] = '.';
                   1281:                                                        }
                   1282:                                                }
                   1283:                                                if((addr & 0x0f) == 0x0f) {
                   1284:                                                        telnet_printf("  %s\n", buffer);
                   1285:                                                }
                   1286:                                        }
                   1287:                                        if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
                   1288:                                                data_seg += 0x1000;
                   1289:                                                data_ofs -= 0x10000;
                   1290:                                        }
                   1291:                                        prev_command[1] = '\0'; // remove parameters to dump continuously
                   1292:                                } else {
                   1293:                                        telnet_printf("invalid parameter number\n");
                   1294:                                }
                   1295:                        } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 0) {
                   1296:                                if(num >= 3) {
                   1297:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1298:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1299:                                        for(int i = 2, j = 0; i < num; i++, j++) {
                   1300:                                                debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
                   1301:                                        }
                   1302:                                } else {
                   1303:                                        telnet_printf("invalid parameter number\n");
                   1304:                                }
                   1305:                        } else if(stricmp(params[0], "EW") == 0) {
                   1306:                                if(num >= 3) {
                   1307:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1308:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1309:                                        for(int i = 2, j = 0; i < num; i++, j += 2) {
                   1310:                                                debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
                   1311:                                        }
                   1312:                                } else {
                   1313:                                        telnet_printf("invalid parameter number\n");
                   1314:                                }
                   1315:                        } else if(stricmp(params[0], "ED") == 0) {
                   1316:                                if(num >= 3) {
                   1317:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1318:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1319:                                        for(int i = 2, j = 0; i < num; i++, j += 4) {
                   1320:                                                debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
                   1321:                                        }
                   1322:                                } else {
                   1323:                                        telnet_printf("invalid parameter number\n");
                   1324:                                }
                   1325:                        } else if(stricmp(params[0], "EA") == 0) {
                   1326:                                if(num >= 3) {
                   1327:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1328:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1329:                                        strcpy(buffer, prev_command);
                   1330:                                        if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
                   1331:                                                int len = strlen(token);
                   1332:                                                for(int i = 0; i < len; i++) {
                   1333:                                                        debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
                   1334:                                                }
                   1335:                                        } else {
                   1336:                                                telnet_printf("invalid parameter\n");
                   1337:                                        }
                   1338:                                } else {
                   1339:                                        telnet_printf("invalid parameter number\n");
                   1340:                                }
                   1341:                        } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
                   1342:                                if(num == 2) {
                   1343:                                        telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
                   1344:                                } else {
                   1345:                                        telnet_printf("invalid parameter number\n");
                   1346:                                }
                   1347:                        } else if(stricmp(params[0], "IW") == 0) {
                   1348:                                if(num == 2) {
                   1349:                                        telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
                   1350:                                } else {
                   1351:                                        telnet_printf("invalid parameter number\n");
                   1352:                                }
                   1353:                        } else if(stricmp(params[0], "ID") == 0) {
                   1354:                                if(num == 2) {
                   1355:                                        telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
                   1356:                                } else {
                   1357:                                        telnet_printf("invalid parameter number\n");
                   1358:                                }
                   1359:                        } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
                   1360:                                if(num == 3) {
                   1361:                                        debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
                   1362:                                } else {
                   1363:                                        telnet_printf("invalid parameter number\n");
                   1364:                                }
                   1365:                        } else if(stricmp(params[0], "OW") == 0) {
                   1366:                                if(num == 3) {
                   1367:                                        debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
                   1368:                                } else {
                   1369:                                        telnet_printf("invalid parameter number\n");
                   1370:                                }
                   1371:                        } else if(stricmp(params[0], "OD") == 0) {
                   1372:                                if(num == 3) {
                   1373:                                        debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
                   1374:                                } else {
                   1375:                                        telnet_printf("invalid parameter number\n");
                   1376:                                }
                   1377:                        } else if(stricmp(params[0], "R") == 0) {
                   1378:                                if(num == 1) {
                   1379:                                        debugger_regs_info(buffer);
                   1380:                                        telnet_printf("%s", buffer);
                   1381:                                } else if(num == 3) {
                   1382: #if defined(HAS_I386)
                   1383:                                        if(stricmp(params[1], "EAX") == 0) {
                   1384:                                                REG32(EAX) = debugger_get_val(params[2]);
                   1385:                                        } else if(stricmp(params[1], "EBX") == 0) {
                   1386:                                                REG32(EBX) = debugger_get_val(params[2]);
                   1387:                                        } else if(stricmp(params[1], "ECX") == 0) {
                   1388:                                                REG32(ECX) = debugger_get_val(params[2]);
                   1389:                                        } else if(stricmp(params[1], "EDX") == 0) {
                   1390:                                                REG32(EDX) = debugger_get_val(params[2]);
                   1391:                                        } else if(stricmp(params[1], "ESP") == 0) {
                   1392:                                                REG32(ESP) = debugger_get_val(params[2]);
                   1393:                                        } else if(stricmp(params[1], "EBP") == 0) {
                   1394:                                                REG32(EBP) = debugger_get_val(params[2]);
                   1395:                                        } else if(stricmp(params[1], "ESI") == 0) {
                   1396:                                                REG32(ESI) = debugger_get_val(params[2]);
                   1397:                                        } else if(stricmp(params[1], "EDI") == 0) {
                   1398:                                                REG32(EDI) = debugger_get_val(params[2]);
                   1399:                                        } else
                   1400: #endif
                   1401:                                        if(stricmp(params[1], "AX") == 0) {
                   1402:                                                REG16(AX) = debugger_get_val(params[2]);
                   1403:                                        } else if(stricmp(params[1], "BX") == 0) {
                   1404:                                                REG16(BX) = debugger_get_val(params[2]);
                   1405:                                        } else if(stricmp(params[1], "CX") == 0) {
                   1406:                                                REG16(CX) = debugger_get_val(params[2]);
                   1407:                                        } else if(stricmp(params[1], "DX") == 0) {
                   1408:                                                REG16(DX) = debugger_get_val(params[2]);
                   1409:                                        } else if(stricmp(params[1], "SP") == 0) {
                   1410:                                                REG16(SP) = debugger_get_val(params[2]);
                   1411:                                        } else if(stricmp(params[1], "BP") == 0) {
                   1412:                                                REG16(BP) = debugger_get_val(params[2]);
                   1413:                                        } else if(stricmp(params[1], "SI") == 0) {
                   1414:                                                REG16(SI) = debugger_get_val(params[2]);
                   1415:                                        } else if(stricmp(params[1], "DI") == 0) {
                   1416:                                                REG16(DI) = debugger_get_val(params[2]);
                   1417:                                        } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
                   1418: #if defined(HAS_I386)
                   1419:                                                if(m_operand_size) {
                   1420:                                                        m_eip = debugger_get_val(params[2]);
                   1421:                                                } else {
                   1422:                                                        m_eip = debugger_get_val(params[2]) & 0xffff;
                   1423:                                                }
                   1424:                                                CHANGE_PC(m_eip);
                   1425: #else
                   1426:                                                m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
                   1427:                                                CHANGE_PC(m_pc);
                   1428: #endif
                   1429:                                        } else if(stricmp(params[1], "AL") == 0) {
                   1430:                                                REG8(AL) = debugger_get_val(params[2]);
                   1431:                                        } else if(stricmp(params[1], "AH") == 0) {
                   1432:                                                REG8(AH) = debugger_get_val(params[2]);
                   1433:                                        } else if(stricmp(params[1], "BL") == 0) {
                   1434:                                                REG8(BL) = debugger_get_val(params[2]);
                   1435:                                        } else if(stricmp(params[1], "BH") == 0) {
                   1436:                                                REG8(BH) = debugger_get_val(params[2]);
                   1437:                                        } else if(stricmp(params[1], "CL") == 0) {
                   1438:                                                REG8(CL) = debugger_get_val(params[2]);
                   1439:                                        } else if(stricmp(params[1], "CH") == 0) {
                   1440:                                                REG8(CH) = debugger_get_val(params[2]);
                   1441:                                        } else if(stricmp(params[1], "DL") == 0) {
                   1442:                                                REG8(DL) = debugger_get_val(params[2]);
                   1443:                                        } else if(stricmp(params[1], "DH") == 0) {
                   1444:                                                REG8(DH) = debugger_get_val(params[2]);
                   1445:                                        } else {
                   1446:                                                telnet_printf("unknown register %s\n", params[1]);
                   1447:                                        }
                   1448:                                } else {
                   1449:                                        telnet_printf("invalid parameter number\n");
                   1450:                                }
                   1451:                        } else if(_tcsicmp(params[0], "S") == 0) {
                   1452:                                if(num >= 4) {
                   1453:                                        UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
                   1454:                                        UINT32 cur_ofs = debugger_get_ofs(params[1]);
                   1455:                                        UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
                   1456:                                        UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1457:                                        UINT8 list[32];
                   1458:                                        
                   1459:                                        for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1460:                                                list[j] = debugger_get_val(params[i]);
                   1461:                                        }
                   1462:                                        while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
                   1463:                                                bool found = true;
                   1464:                                                for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1465:                                                        if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
                   1466:                                                                found = false;
                   1467:                                                                break;
                   1468:                                                        }
                   1469:                                                }
                   1470:                                                if(found) {
                   1471:                                                        telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
                   1472:                                                }
                   1473:                                                if((cur_ofs += 1) > 0xffff) {
                   1474:                                                        cur_seg += 0x1000;
                   1475:                                                        cur_ofs -= 0x10000;
                   1476:                                                }
                   1477:                                        }
                   1478:                                } else {
                   1479:                                        telnet_printf("invalid parameter number\n");
                   1480:                                }
                   1481:                        } else if(stricmp(params[0], "U") == 0) {
                   1482:                                if(num <= 3) {
                   1483:                                        if(num >= 2) {
                   1484:                                                dasm_seg = debugger_get_seg(params[1], dasm_seg);
                   1485:                                                dasm_ofs = debugger_get_ofs(params[1]);
                   1486:                                        }
                   1487:                                        if(num == 3) {
                   1488:                                                UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
                   1489:                                                UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1490:                                                
                   1491:                                                while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
                   1492:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1493:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1494:                                                        for(int i = 0; i < len; i++) {
                   1495:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1496:                                                        }
                   1497:                                                        for(int i = len; i < 8; i++) {
                   1498:                                                                telnet_printf("  ");
                   1499:                                                        }
                   1500:                                                        telnet_printf("  %s\n", buffer);
                   1501:                                                        if((dasm_ofs += len) > 0xffff) {
                   1502:                                                                dasm_seg += 0x1000;
                   1503:                                                                dasm_ofs -= 0x10000;
                   1504:                                                        }
                   1505:                                                }
                   1506:                                        } else {
                   1507:                                                for(int i = 0; i < 16; i++) {
                   1508:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1509:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1510:                                                        for(int i = 0; i < len; i++) {
                   1511:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1512:                                                        }
                   1513:                                                        for(int i = len; i < 8; i++) {
                   1514:                                                                telnet_printf("  ");
                   1515:                                                        }
                   1516:                                                        telnet_printf("  %s\n", buffer);
                   1517:                                                        if((dasm_ofs += len) > 0xffff) {
                   1518:                                                                dasm_seg += 0x1000;
                   1519:                                                                dasm_ofs -= 0x10000;
                   1520:                                                        }
                   1521:                                                }
                   1522:                                        }
                   1523:                                        prev_command[1] = '\0'; // remove parameters to disassemble continuously
                   1524:                                } else {
                   1525:                                        telnet_printf("invalid parameter number\n");
                   1526:                                }
                   1527:                        } else if(stricmp(params[0], "H") == 0) {
                   1528:                                if(num == 3) {
                   1529:                                        UINT32 l = debugger_get_val(params[1]);
                   1530:                                        UINT32 r = debugger_get_val(params[2]);
                   1531:                                        telnet_printf("%08X  %08X\n", l + r, l - r);
                   1532:                                } else {
                   1533:                                        telnet_printf("invalid parameter number\n");
                   1534:                                }
                   1535:                        } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
                   1536:                                break_point_t *break_point_ptr;
                   1537:                                #define GET_BREAK_POINT_PTR() { \
                   1538:                                        if(params[0][0] == 'R') { \
                   1539:                                                break_point_ptr = &rd_break_point; \
                   1540:                                        } else if(params[0][0] == 'W') { \
                   1541:                                                break_point_ptr = &wr_break_point; \
                   1542:                                        } else if(params[0][0] == 'I') { \
                   1543:                                                break_point_ptr = &in_break_point; \
                   1544:                                        } else if(params[0][0] == 'O') { \
                   1545:                                                break_point_ptr = &out_break_point; \
                   1546:                                        } else { \
                   1547:                                                break_point_ptr = &break_point; \
                   1548:                                        } \
                   1549:                                }
                   1550:                                GET_BREAK_POINT_PTR();
                   1551:                                if(num == 2) {
                   1552:                                        UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1553:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1554:                                        bool found = false;
                   1555:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1556:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
                   1557:                                                        break_point_ptr->table[i].addr = (seg << 4) + ofs;
                   1558:                                                        break_point_ptr->table[i].seg = seg;
                   1559:                                                        break_point_ptr->table[i].ofs = ofs;
                   1560:                                                        break_point_ptr->table[i].status = 1;
                   1561:                                                        found = true;
                   1562:                                                }
                   1563:                                        }
                   1564:                                        if(!found) {
                   1565:                                                telnet_printf("too many break points\n");
                   1566:                                        }
                   1567:                                } else {
                   1568:                                        telnet_printf("invalid parameter number\n");
                   1569:                                }
                   1570:                        } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
                   1571:                                break_point_t *break_point_ptr;
                   1572:                                GET_BREAK_POINT_PTR();
                   1573:                                if(num == 2) {
                   1574:                                        UINT32 addr = debugger_get_val(params[1]);
                   1575:                                        bool found = false;
                   1576:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1577:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
                   1578:                                                        break_point_ptr->table[i].addr = addr;
                   1579:                                                        break_point_ptr->table[i].status = 1;
                   1580:                                                        found = true;
                   1581:                                                }
                   1582:                                        }
                   1583:                                        if(!found) {
                   1584:                                                telnet_printf("too many break points\n");
                   1585:                                        }
                   1586:                                } else {
                   1587:                                        telnet_printf("invalid parameter number\n");
                   1588:                                }
                   1589:                        } 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) {
                   1590:                                break_point_t *break_point_ptr;
                   1591:                                GET_BREAK_POINT_PTR();
                   1592:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1593:                                        memset(break_point_ptr, 0, sizeof(break_point_t));
                   1594:                                } else if(num >= 2) {
                   1595:                                        for(int i = 1; i < num; i++) {
                   1596:                                                int index = debugger_get_val(params[i]);
                   1597:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1598:                                                        telnet_printf("invalid index %x\n", index);
                   1599:                                                } else {
                   1600:                                                        break_point_ptr->table[index - 1].addr = 0;
                   1601:                                                        break_point_ptr->table[index - 1].seg = 0;
                   1602:                                                        break_point_ptr->table[index - 1].ofs = 0;
                   1603:                                                        break_point_ptr->table[index - 1].status = 0;
                   1604:                                                }
                   1605:                                        }
                   1606:                                } else {
                   1607:                                        telnet_printf("invalid parameter number\n");
                   1608:                                }
                   1609:                        } 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 ||
                   1610:                                  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) {
                   1611:                                break_point_t *break_point_ptr;
                   1612:                                GET_BREAK_POINT_PTR();
                   1613:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1614:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1615:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1616:                                                if(break_point_ptr->table[i].status != 0) {
                   1617:                                                        break_point_ptr->table[i].status = enabled ? 1 : -1;
                   1618:                                                }
                   1619:                                        }
                   1620:                                } else if(num >= 2) {
                   1621:                                        for(int i = 1; i < num; i++) {
                   1622:                                                int index = debugger_get_val(params[i]);
                   1623:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1624:                                                        telnet_printf("invalid index %x\n", index);
                   1625:                                                } else if(break_point_ptr->table[index - 1].status == 0) {
                   1626:                                                        telnet_printf("break point %x is null\n", index);
                   1627:                                                } else {
                   1628:                                                        break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
                   1629:                                                }
                   1630:                                        }
                   1631:                                } else {
                   1632:                                        telnet_printf("invalid parameter number\n");
                   1633:                                }
                   1634:                        } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
                   1635:                                break_point_t *break_point_ptr;
                   1636:                                GET_BREAK_POINT_PTR();
                   1637:                                if(num == 1) {
                   1638:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1639:                                                if(break_point_ptr->table[i].status) {
                   1640:                                                        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);
                   1641:                                                }
                   1642:                                        }
                   1643:                                } else {
                   1644:                                        telnet_printf("invalid parameter number\n");
                   1645:                                }
                   1646:                        } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
                   1647:                                break_point_t *break_point_ptr;
                   1648:                                GET_BREAK_POINT_PTR();
                   1649:                                if(num == 1) {
                   1650:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1651:                                                if(break_point_ptr->table[i].status) {
                   1652:                                                        telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
                   1653:                                                }
                   1654:                                        }
                   1655:                                } else {
                   1656:                                        telnet_printf("invalid parameter number\n");
                   1657:                                }
                   1658:                        } else if(stricmp(params[0], "INTBP") == 0) {
                   1659:                                if(num >= 2 && num <= 4) {
                   1660:                                        int int_num = debugger_get_val(params[1]);
                   1661:                                        UINT8 ah = 0, ah_registered = 0;
                   1662:                                        UINT8 al = 0, al_registered = 0;
                   1663:                                        if(num >= 3) {
                   1664:                                                ah = debugger_get_val(params[2]);
                   1665:                                                ah_registered = 1;
                   1666:                                        }
                   1667:                                        if(num == 4) {
                   1668:                                                al = debugger_get_val(params[3]);
                   1669:                                                al_registered = 1;
                   1670:                                        }
                   1671:                                        bool found = false;
                   1672:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1673:                                                if(int_break_point.table[i].status == 0 || (
                   1674:                                                   int_break_point.table[i].int_num == int_num &&
                   1675:                                                   int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
                   1676:                                                   int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
                   1677:                                                        int_break_point.table[i].int_num = int_num;
                   1678:                                                        int_break_point.table[i].ah = ah;
                   1679:                                                        int_break_point.table[i].ah_registered = ah_registered;
                   1680:                                                        int_break_point.table[i].al = al;
                   1681:                                                        int_break_point.table[i].al_registered = al_registered;
                   1682:                                                        int_break_point.table[i].status = 1;
                   1683:                                                        found = true;
                   1684:                                                }
                   1685:                                        }
                   1686:                                        if(!found) {
                   1687:                                                telnet_printf("too many break points\n");
                   1688:                                        }
                   1689:                                } else {
                   1690:                                        telnet_printf("invalid parameter number\n");
                   1691:                                }
                   1692:                        } else if(stricmp(params[0], "INTBC") == 0) {
                   1693:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1694:                                        memset(&int_break_point, 0, sizeof(int_break_point_t));
                   1695:                                } else if(num >= 2) {
                   1696:                                        for(int i = 1; i < num; i++) {
                   1697:                                                int index = debugger_get_val(params[i]);
                   1698:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1699:                                                        telnet_printf("invalid index %x\n", index);
                   1700:                                                } else {
                   1701:                                                        int_break_point.table[index - 1].int_num = 0;
                   1702:                                                        int_break_point.table[index - 1].ah = 0;
                   1703:                                                        int_break_point.table[index - 1].ah_registered = 0;
                   1704:                                                        int_break_point.table[index - 1].al = 0;
                   1705:                                                        int_break_point.table[index - 1].al_registered = 0;
                   1706:                                                        int_break_point.table[index - 1].status = 0;
                   1707:                                                }
                   1708:                                        }
                   1709:                                } else {
                   1710:                                        telnet_printf("invalid parameter number\n");
                   1711:                                }
                   1712:                        } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
                   1713:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1714:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1715:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1716:                                                if(int_break_point.table[i].status != 0) {
                   1717:                                                        int_break_point.table[i].status = enabled ? 1 : -1;
                   1718:                                                }
                   1719:                                        }
                   1720:                                } else if(num >= 2) {
                   1721:                                        for(int i = 1; i < num; i++) {
                   1722:                                                int index = debugger_get_val(params[i]);
                   1723:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1724:                                                        telnet_printf("invalid index %x\n", index);
                   1725:                                                } else if(int_break_point.table[index - 1].status == 0) {
                   1726:                                                        telnet_printf("break point %x is null\n", index);
                   1727:                                                } else {
                   1728:                                                        int_break_point.table[index - 1].status = enabled ? 1 : -1;
                   1729:                                                }
                   1730:                                        }
                   1731:                                } else {
                   1732:                                        telnet_printf("invalid parameter number\n");
                   1733:                                }
                   1734:                        } else if(stricmp(params[0], "INTBL") == 0) {
                   1735:                                if(num == 1) {
                   1736:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1737:                                                if(int_break_point.table[i].status) {
                   1738:                                                        telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
                   1739:                                                        if(int_break_point.table[i].ah_registered) {
                   1740:                                                                telnet_printf(" %02X", int_break_point.table[i].ah);
                   1741:                                                        }
                   1742:                                                        if(int_break_point.table[i].al_registered) {
                   1743:                                                                telnet_printf(" %02X", int_break_point.table[i].al);
                   1744:                                                        }
                   1745:                                                        telnet_printf("\n");
                   1746:                                                }
                   1747:                                        }
                   1748:                                } else {
                   1749:                                        telnet_printf("invalid parameter number\n");
                   1750:                                }
                   1751:                        } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
                   1752:                                if(num == 1 || num == 2) {
                   1753:                                        break_point_t break_point_stored;
                   1754:                                        bool break_points_stored = false;
                   1755:                                        
                   1756:                                        if(stricmp(params[0], "P") == 0) {
                   1757:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1758:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1759:                                                break_points_stored = true;
                   1760:                                                
                   1761:                                                break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
                   1762:                                                break_point.table[0].status = 1;
                   1763:                                        } else if(num >= 2) {
                   1764:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1765:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1766:                                                break_points_stored = true;
                   1767:                                                
                   1768:                                                UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1769:                                                UINT32 ofs = debugger_get_ofs(params[1]);
                   1770:                                                break_point.table[0].addr = (seg << 4) + ofs;
                   1771:                                                break_point.table[0].seg = seg;
                   1772:                                                break_point.table[0].ofs = ofs;
                   1773:                                                break_point.table[0].status = 1;
                   1774:                                        }
                   1775:                                        break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1776:                                        now_going = true;
                   1777:                                        now_suspended = false;
                   1778:                                        
                   1779:                                        telnet_command("\033[2l"); // key unlock
                   1780:                                        while(!m_halted && !now_suspended) {
                   1781:                                                if(telnet_kbhit()) {
                   1782:                                                        break;
                   1783:                                                }
                   1784:                                                Sleep(10);
                   1785:                                        }
                   1786:                                        now_going = false;
                   1787:                                        telnet_command("\033[2h"); // key lock
                   1788:                                        
                   1789:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1790:                                                Sleep(100);
                   1791:                                                if(!m_halted && !now_suspended) {
                   1792:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1793:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1794:                                                }
                   1795:                                        }
                   1796:                                        while(!m_halted && !now_suspended) {
                   1797:                                                if(telnet_disconnected()) {
                   1798:                                                        break;
                   1799:                                                }
                   1800:                                                Sleep(10);
                   1801:                                        }
                   1802:                                        dasm_seg = SREG(CS);
                   1803:                                        dasm_ofs = m_eip;
                   1804:                                        
                   1805:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1806:                                        debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1807:                                        telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1808:                                        
                   1809:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1810:                                        debugger_regs_info(buffer);
                   1811:                                        telnet_printf("%s", buffer);
                   1812:                                        
                   1813:                                        if(break_point.hit) {
                   1814:                                                if(stricmp(params[0], "G") == 0 && num == 1) {
                   1815:                                                        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1816:                                                        telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1817:                                                }
                   1818:                                        } else if(rd_break_point.hit) {
                   1819:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1820:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1821:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1822:                                                m_prev_cs, m_prev_eip);
                   1823:                                        } else if(wr_break_point.hit) {
                   1824:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1825:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1826:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1827:                                                m_prev_cs, m_prev_eip);
                   1828:                                        } else if(in_break_point.hit) {
                   1829:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1830:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1831:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1832:                                                m_prev_cs, m_prev_eip);
                   1833:                                        } else if(out_break_point.hit) {
                   1834:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1835:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1836:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1837:                                                m_prev_cs, m_prev_eip);
                   1838:                                        } else if(int_break_point.hit) {
                   1839:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1840:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1841:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1842:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1843:                                                }
                   1844:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1845:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1846:                                                }
                   1847:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1848:                                        } else {
                   1849:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1850:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1851:                                        }
                   1852:                                        if(break_points_stored) {
                   1853:                                                memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
                   1854:                                        }
                   1855:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1856:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1857:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1858:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1859:                                } else {
                   1860:                                        telnet_printf("invalid parameter number\n");
                   1861:                                }
                   1862:                        } else if(stricmp(params[0], "T") == 0) {
                   1863:                                if(num == 1 || num == 2) {
                   1864:                                        int steps = 1;
                   1865:                                        if(num >= 2) {
                   1866:                                                steps = debugger_get_val(params[1]);
                   1867:                                        }
                   1868:                                        
                   1869:                                        telnet_command("\033[2l"); // key unlock
                   1870:                                        while(steps-- > 0) {
                   1871:                                                break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1872:                                                now_going = false;
                   1873:                                                now_suspended = false;
                   1874:                                                
                   1875:                                                while(!m_halted && !now_suspended) {
                   1876:                                                        if(telnet_disconnected()) {
                   1877:                                                                break;
                   1878:                                                        }
                   1879:                                                        Sleep(10);
                   1880:                                                }
                   1881:                                                dasm_seg = SREG(CS);
                   1882:                                                dasm_ofs = m_eip;
                   1883:                                                
                   1884:                                                telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1885:                                                debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1886:                                                telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1887:                                                
                   1888:                                                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1889:                                                debugger_regs_info(buffer);
                   1890:                                                telnet_printf("%s", buffer);
                   1891:                                                
                   1892:                                                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()) {
                   1893:                                                        break;
                   1894:                                                }
                   1895:                                        }
                   1896:                                        telnet_command("\033[2h"); // key lock
                   1897:                                        
                   1898:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1899:                                                Sleep(100);
                   1900:                                                if(!m_halted && !now_suspended) {
                   1901:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1902:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1903:                                                }
                   1904:                                        }
                   1905:                                        while(!m_halted && !now_suspended) {
                   1906:                                                if(telnet_disconnected()) {
                   1907:                                                        break;
                   1908:                                                }
                   1909:                                                Sleep(10);
                   1910:                                        }
                   1911:                                        if(break_point.hit) {
                   1912:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1913:                                                telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1914:                                        } else if(rd_break_point.hit) {
                   1915:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1916:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1917:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1918:                                                m_prev_cs, m_prev_eip);
                   1919:                                        } else if(wr_break_point.hit) {
                   1920:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1921:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1922:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1923:                                                m_prev_cs, m_prev_eip);
                   1924:                                        } else if(in_break_point.hit) {
                   1925:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1926:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1927:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1928:                                                m_prev_cs, m_prev_eip);
                   1929:                                        } else if(out_break_point.hit) {
                   1930:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1931:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1932:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1933:                                                m_prev_cs, m_prev_eip);
                   1934:                                        } else if(int_break_point.hit) {
                   1935:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1936:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1937:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1938:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1939:                                                }
                   1940:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1941:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1942:                                                }
                   1943:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1944:                                        } else if(steps > 0) {
                   1945:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1946:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1947:                                        }
                   1948:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1949:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1950:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1951:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1952:                                } else {
                   1953:                                        telnet_printf("invalid parameter number\n");
                   1954:                                }
                   1955:                        } else if(stricmp(params[0], "Q") == 0) {
                   1956:                                break;
                   1957:                        } else if(stricmp(params[0], "X") == 0) {
                   1958:                                debugger_process_info(buffer);
                   1959:                                telnet_printf("%s", buffer);
                   1960:                        } else if(stricmp(params[0], ">") == 0) {
                   1961:                                if(num == 2) {
                   1962:                                        if(fp_debugger != NULL) {
                   1963:                                                fclose(fp_debugger);
                   1964:                                                fp_debugger = NULL;
                   1965:                                        }
                   1966:                                        fp_debugger = fopen(params[1], "w");
                   1967:                                } else {
                   1968:                                        telnet_printf("invalid parameter number\n");
                   1969:                                }
                   1970:                        } else if(stricmp(params[0], "<") == 0) {
                   1971:                                if(num == 2) {
                   1972:                                        if(fi_debugger != NULL) {
                   1973:                                                fclose(fi_debugger);
                   1974:                                                fi_debugger = NULL;
                   1975:                                        }
                   1976:                                        fi_debugger = fopen(params[1], "r");
                   1977:                                } else {
                   1978:                                        telnet_printf("invalid parameter number\n");
                   1979:                                }
                   1980:                        } else if(stricmp(params[0], "?") == 0) {
                   1981:                                telnet_printf("D [<start> [<end>]] - dump memory\n");
                   1982:                                telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
                   1983:                                telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
                   1984:                                telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
                   1985:                                telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
                   1986:                                
                   1987:                                telnet_printf("R - show registers\n");
                   1988:                                telnet_printf("R <reg> <value> - edit register\n");
                   1989:                                telnet_printf("S <start> <end> <list> - search\n");
                   1990:                                telnet_printf("U [<start> [<end>]] - unassemble\n");
                   1991:                                
                   1992:                                telnet_printf("H <value> <value> - hexadd\n");
                   1993:                                
                   1994:                                telnet_printf("BP <address> - set breakpoint\n");
                   1995:                                telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
                   1996:                                telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
                   1997:                                telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
                   1998:                                telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
                   1999:                                telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
                   2000:                                
                   2001:                                telnet_printf("G - go (press enter key to break)\n");
                   2002:                                telnet_printf("G <address> - go and break at address\n");
                   2003:                                telnet_printf("P - trace one opcode (step over)\n");
                   2004:                                telnet_printf("T [<count>] - trace (step in)\n");
                   2005:                                telnet_printf("Q - quit\n");
                   2006:                                telnet_printf("X - show dos process info\n");
                   2007:                                
                   2008:                                telnet_printf("> <filename> - output logfile\n");
                   2009:                                telnet_printf("< <filename> - input commands from file\n");
                   2010:                                
                   2011:                                telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
                   2012:                                telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
                   2013:                        } else {
                   2014:                                telnet_printf("unknown command %s\n", params[0]);
                   2015:                        }
                   2016:                }
                   2017:        }
                   2018:        if(fp_debugger != NULL) {
                   2019:                fclose(fp_debugger);
                   2020:                fp_debugger = NULL;
                   2021:        }
                   2022:        if(fi_debugger != NULL) {
                   2023:                fclose(fi_debugger);
                   2024:                fi_debugger = NULL;
                   2025:        }
                   2026:        now_debugging = now_going = now_suspended = force_suspend = false;
                   2027:        closesocket(cli_socket);
                   2028: }
                   2029: 
                   2030: const char *debugger_get_ttermpro_path()
                   2031: {
                   2032:        static char path[MAX_PATH] = {0};
                   2033:        
                   2034:        if(getenv("ProgramFiles")) {
                   2035:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
                   2036:        }
                   2037:        return(path);
                   2038: }
                   2039: 
                   2040: const char *debugger_get_ttermpro_x86_path()
                   2041: {
                   2042:        static char path[MAX_PATH] = {0};
                   2043:        
                   2044:        if(getenv("ProgramFiles(x86)")) {
                   2045:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
                   2046:        }
                   2047:        return(path);
                   2048: }
                   2049: 
                   2050: const char *debugger_get_putty_path()
                   2051: {
                   2052:        static char path[MAX_PATH] = {0};
                   2053:        
                   2054:        if(getenv("ProgramFiles")) {
                   2055:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
                   2056:        }
                   2057:        return(path);
                   2058: }
                   2059: 
                   2060: const char *debugger_get_putty_x86_path()
                   2061: {
                   2062:        static char path[MAX_PATH] = {0};
                   2063:        
                   2064:        if(getenv("ProgramFiles(x86)")) {
                   2065:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
                   2066:        }
                   2067:        return(path);
                   2068: }
                   2069: 
                   2070: const char *debugger_get_telnet_path()
                   2071: {
                   2072:        // NOTE: When you run 32bit version of msdos.exe on Windows x64,
                   2073:        // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
                   2074:        // But 32bit version of telnet.exe will not be installed in SysWOW64
                   2075:        // and 64bit version of telnet.exe will be installed in System32.
                   2076:        static char path[MAX_PATH] = {0};
                   2077:        
                   2078:        if(getenv("windir") != NULL) {
                   2079:                sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
                   2080:        }
                   2081:        return(path);
                   2082: }
                   2083: 
                   2084: DWORD WINAPI debugger_thread(LPVOID)
                   2085: {
                   2086:        WSADATA was_data;
                   2087:        struct sockaddr_in svr_addr;
                   2088:        struct sockaddr_in cli_addr;
                   2089:        int cli_addr_len = sizeof(cli_addr);
                   2090:        int port = 23;
                   2091:        int bind_stat = SOCKET_ERROR;
                   2092:        struct timeval timeout;
                   2093:        
                   2094:        WSAStartup(MAKEWORD(2,0), &was_data);
                   2095:        
                   2096:        if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
                   2097:                memset(&svr_addr, 0, sizeof(svr_addr));
                   2098:                svr_addr.sin_family = AF_INET;
                   2099:                svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
                   2100:                
                   2101:                while(!m_halted && port < 10000) {
                   2102:                        svr_addr.sin_port = htons(port);
                   2103:                        if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
                   2104:                                break;
                   2105:                        } else {
                   2106:                                port = (port == 23) ? 9000 : (port + 1);
                   2107:                        }
                   2108:                }
                   2109:                if(bind_stat == 0) {
                   2110:                        timeout.tv_sec = 1;
                   2111:                        timeout.tv_usec = 0;
1.1.1.45  root     2112:                        setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
1.1.1.33  root     2113:                        
                   2114:                        listen(svr_socket, 1);
                   2115:                        
                   2116:                        char command[MAX_PATH] = {0};
                   2117:                        STARTUPINFO si;
                   2118:                        PROCESS_INFORMATION pi;
                   2119:                        
                   2120:                        if(_access(debugger_get_ttermpro_path(), 0) == 0) {
                   2121:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
                   2122:                        } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
                   2123:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
                   2124:                        } else if(_access(debugger_get_putty_path(), 0) == 0) {
                   2125:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
                   2126:                        } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
                   2127:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
                   2128:                        } else if(_access(debugger_get_telnet_path(), 0) == 0) {
                   2129:                                sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
                   2130:                        }
                   2131:                        if(command[0] != '\0') {
                   2132:                                memset(&si, 0, sizeof(STARTUPINFO));
                   2133:                                memset(&pi, 0, sizeof(PROCESS_INFORMATION));
                   2134:                                CreateProcess(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
                   2135:                        }
                   2136:                        
                   2137:                        while(!m_halted) {
                   2138:                                if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
                   2139:                                        u_long val = 1;
                   2140:                                        ioctlsocket(cli_socket, FIONBIO, &val);
                   2141:                                        debugger_main();
                   2142:                                }
                   2143:                        }
                   2144:                }
                   2145:        }
                   2146:        WSACleanup();
                   2147:        return(0);
                   2148: }
                   2149: #endif
                   2150: 
                   2151: /* ----------------------------------------------------------------------------
1.1       root     2152:        main
                   2153: ---------------------------------------------------------------------------- */
                   2154: 
1.1.1.28  root     2155: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                   2156: {
                   2157:        if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33  root     2158:                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     2159: #ifdef USE_SERVICE_THREAD
                   2160:                        EnterCriticalSection(&key_buf_crit_sect);
                   2161: #endif
1.1.1.33  root     2162:                        key_buf_char->clear();
                   2163:                        key_buf_scan->clear();
1.1.1.35  root     2164: #ifdef USE_SERVICE_THREAD
                   2165:                        LeaveCriticalSection(&key_buf_crit_sect);
                   2166: #endif
1.1.1.33  root     2167:                }
                   2168: //             key_code = key_recv = 0;
1.1.1.28  root     2169:                return TRUE;
                   2170:        } else if(dwCtrlType == CTRL_C_EVENT) {
                   2171:                return TRUE;
                   2172:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
                   2173:                // this program will be terminated abnormally, do minimum end process
                   2174:                exit_handler();
                   2175:                exit(1);
                   2176:        }
                   2177:        return FALSE;
                   2178: }
                   2179: 
                   2180: void exit_handler()
                   2181: {
                   2182:        if(temp_file_created) {
                   2183:                DeleteFile(temp_file_path);
                   2184:                temp_file_created = false;
                   2185:        }
                   2186:        if(key_buf_char != NULL) {
                   2187:                key_buf_char->release();
                   2188:                delete key_buf_char;
                   2189:                key_buf_char = NULL;
                   2190:        }
                   2191:        if(key_buf_scan != NULL) {
                   2192:                key_buf_scan->release();
                   2193:                delete key_buf_scan;
                   2194:                key_buf_scan = NULL;
                   2195:        }
1.1.1.32  root     2196: #ifdef SUPPORT_XMS
                   2197:        msdos_xms_release();
                   2198: #endif
1.1.1.28  root     2199:        hardware_release();
                   2200: }
                   2201: 
1.1.1.35  root     2202: #ifdef USE_VRAM_THREAD
1.1.1.28  root     2203: DWORD WINAPI vram_thread(LPVOID)
                   2204: {
                   2205:        while(!m_halted) {
                   2206:                EnterCriticalSection(&vram_crit_sect);
                   2207:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                   2208:                        vram_flush_char();
                   2209:                }
                   2210:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                   2211:                        vram_flush_attr();
                   2212:                }
                   2213:                vram_last_length_char = vram_length_char;
                   2214:                vram_last_length_attr = vram_length_attr;
                   2215:                LeaveCriticalSection(&vram_crit_sect);
                   2216:                // this is about half the maximum keyboard repeat rate - any
                   2217:                // lower tends to be jerky, any higher misses updates
                   2218:                Sleep(15);
                   2219:        }
                   2220:        return 0;
                   2221: }
                   2222: #endif
                   2223: 
1.1.1.45  root     2224: long get_section_in_exec_file(FILE *fp, const char *name)
1.1.1.28  root     2225: {
                   2226:        UINT8 header[0x400];
                   2227:        
                   2228:        long position = ftell(fp);
                   2229:        fseek(fp, 0, SEEK_SET);
                   2230:        fread(header, sizeof(header), 1, fp);
                   2231:        fseek(fp, position, SEEK_SET);
                   2232:        
                   2233:        try {
                   2234:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2235:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2236:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2237:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2238:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2239:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2240:                
                   2241:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                   2242:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                   2243:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                   2244:                                return(sectionHeader->PointerToRawData);
                   2245:                        }
                   2246:                }
                   2247:        } catch(...) {
                   2248:        }
                   2249:        return(0);
                   2250: }
                   2251: 
1.1.1.10  root     2252: bool is_started_from_command_prompt()
                   2253: {
1.1.1.18  root     2254:        bool ret = false;
                   2255:        
                   2256:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                   2257:        if(hLibrary) {
                   2258:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                   2259:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                   2260:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
                   2261:                if(lpfnGetConsoleProcessList) {
                   2262:                        DWORD pl;
                   2263:                        ret = (lpfnGetConsoleProcessList(&pl, 1) > 1);
                   2264:                        FreeLibrary(hLibrary);
                   2265:                        return(ret);
                   2266:                }
                   2267:                FreeLibrary(hLibrary);
                   2268:        }
                   2269:        
                   2270:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   2271:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                   2272:                DWORD dwParentProcessID = 0;
                   2273:                PROCESSENTRY32 pe32;
                   2274:                pe32.dwSize = sizeof(PROCESSENTRY32);
                   2275:                if(Process32First(hSnapshot, &pe32)) {
                   2276:                        do {
                   2277:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                   2278:                                        dwParentProcessID = pe32.th32ParentProcessID;
                   2279:                                        break;
                   2280:                                }
                   2281:                        } while(Process32Next(hSnapshot, &pe32));
                   2282:                }
                   2283:                CloseHandle(hSnapshot);
                   2284:                if(dwParentProcessID != 0) {
                   2285:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                   2286:                        if(hProcess != NULL) {
                   2287:                                HMODULE hMod;
                   2288:                                DWORD cbNeeded;
                   2289:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                   2290:                                        char module_name[MAX_PATH];
                   2291:                                        if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
                   2292:                                                ret = (_strnicmp(module_name, "cmd.exe", 7) == 0);
                   2293:                                        }
                   2294:                                }
                   2295:                                CloseHandle(hProcess);
                   2296:                        }
                   2297:                }
                   2298:        }
                   2299:        return(ret);
1.1.1.14  root     2300: }
                   2301: 
                   2302: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                   2303: {
1.1.1.24  root     2304:        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
1.1.1.14  root     2305:        OSVERSIONINFOEX osvi;
                   2306:        DWORDLONG dwlConditionMask = 0;
                   2307:        int op = VER_GREATER_EQUAL;
                   2308:        
                   2309:        // Initialize the OSVERSIONINFOEX structure.
                   2310:        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
                   2311:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
                   2312:        osvi.dwMajorVersion = dwMajorVersion;
                   2313:        osvi.dwMinorVersion = dwMinorVersion;
                   2314:        osvi.wServicePackMajor = wServicePackMajor;
                   2315:        osvi.wServicePackMinor = wServicePackMinor;
                   2316:        
                   2317:         // Initialize the condition mask.
                   2318:        VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                   2319:        VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                   2320:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                   2321:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                   2322:        
                   2323:        // Perform the test.
                   2324:        return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                   2325: }
                   2326: 
1.1.1.27  root     2327: void get_sio_port_numbers()
                   2328: {
                   2329:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                   2330:        HDEVINFO hDevInfo = 0;
                   2331:        HKEY hKey = 0;
                   2332:        if((hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
                   2333:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                   2334:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                   2335:                                char chData[256];
                   2336:                                DWORD dwType = 0;
                   2337:                                DWORD dwSize = sizeof(chData);
                   2338:                                int port_number = 0;
                   2339:                                
                   2340:                                if(RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
                   2341:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                   2342:                                                port_number = atoi(chData + 3);
                   2343:                                        }
                   2344:                                }
                   2345:                                RegCloseKey(hKey);
                   2346:                                
1.1.1.29  root     2347:                                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     2348:                                        continue;
                   2349:                                }
                   2350:                                if(sio_port_number[0] == 0) {
                   2351:                                        sio_port_number[0] = port_number;
                   2352:                                } else if(sio_port_number[1] == 0) {
                   2353:                                        sio_port_number[1] = port_number;
1.1.1.29  root     2354:                                } else if(sio_port_number[2] == 0) {
                   2355:                                        sio_port_number[2] = port_number;
                   2356:                                } else if(sio_port_number[3] == 0) {
                   2357:                                        sio_port_number[3] = port_number;
1.1.1.27  root     2358:                                }
1.1.1.29  root     2359:                                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     2360:                                        break;
                   2361:                                }
                   2362:                        }
                   2363:                }
                   2364:        }
                   2365: }
                   2366: 
1.1.1.28  root     2367: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                   2368: 
1.1       root     2369: int main(int argc, char *argv[], char *envp[])
                   2370: {
1.1.1.9   root     2371:        int arg_offset = 0;
                   2372:        int standard_env = 0;
1.1.1.14  root     2373:        int buf_width = 0, buf_height = 0;
1.1.1.28  root     2374:        bool get_console_info_success = false;
                   2375:        bool screen_size_changed = false;
                   2376:        
                   2377:        _TCHAR path[MAX_PATH], full[MAX_PATH], *name = NULL;
                   2378:        GetModuleFileName(NULL, path, MAX_PATH);
                   2379:        GetFullPathName(path, MAX_PATH, full, &name);
1.1       root     2380:        
1.1.1.27  root     2381:        char dummy_argv_0[] = "msdos.exe";
                   2382:        char dummy_argv_1[MAX_PATH];
                   2383:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                   2384:        char new_exec_file[MAX_PATH];
                   2385:        bool convert_cmd_file = false;
1.1.1.28  root     2386:        unsigned int code_page = 0;
1.1.1.27  root     2387:        
                   2388:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root     2389:                // check if command file is embedded to this execution file
                   2390:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root     2391:                FILE* fp = fopen(full, "rb");
1.1.1.28  root     2392:                long offset = get_section_in_exec_file(fp, ".msdos");
                   2393:                if(offset != 0) {
1.1.1.30  root     2394:                        UINT8 buffer[16];
1.1.1.28  root     2395:                        fseek(fp, offset, SEEK_SET);
                   2396:                        fread(buffer, sizeof(buffer), 1, fp);
                   2397:                        
                   2398:                        // restore flags
                   2399:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                   2400:                        no_windows          = ((buffer[0] & 0x02) != 0);
                   2401:                        standard_env        = ((buffer[0] & 0x04) != 0);
                   2402:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                   2403:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                   2404:                        if((buffer[0] & 0x20) != 0) {
                   2405:                                get_sio_port_numbers();
                   2406:                        }
                   2407:                        if((buffer[0] & 0x40) != 0) {
                   2408:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                   2409:                                support_ems = true;
1.1.1.30  root     2410:                        }
1.1.1.27  root     2411: #ifdef SUPPORT_XMS
1.1.1.30  root     2412:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root     2413:                                support_xms = true;
                   2414:                        }
1.1.1.30  root     2415: #endif
1.1.1.28  root     2416:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                   2417:                                buf_width  = buffer[1] | (buffer[2] << 8);
                   2418:                                buf_height = buffer[3] | (buffer[4] << 8);
                   2419:                        }
                   2420:                        if(buffer[5] != 0) {
1.1.1.30  root     2421:                                dos_major_version = buffer[5];
                   2422:                                dos_minor_version = buffer[6];
                   2423:                        }
                   2424:                        if(buffer[7] != 0) {
                   2425:                                win_major_version = buffer[7];
                   2426:                                win_minor_version = buffer[8];
1.1.1.28  root     2427:                        }
1.1.1.30  root     2428:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root     2429:                                SetConsoleCP(code_page);
                   2430:                                SetConsoleOutputCP(code_page);
                   2431:                        }
1.1.1.30  root     2432:                        int name_len = buffer[11];
                   2433:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root     2434:                        
                   2435:                        // restore command file name
                   2436:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                   2437:                        fread(dummy_argv_1, name_len, 1, fp);
                   2438:                        
                   2439:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                   2440:                                // if original command file exists, create a temporary file name
                   2441:                                if(GetTempFileName(_T("."), _T("DOS"), 0, dummy_argv_1) != 0) {
                   2442:                                        // create a temporary command file in the current director
                   2443:                                        DeleteFile(dummy_argv_1);
1.1.1.27  root     2444:                                } else {
1.1.1.28  root     2445:                                        // create a temporary command file in the temporary folder
                   2446:                                        GetTempPath(MAX_PATH, path);
                   2447:                                        if(GetTempFileName(path, _T("DOS"), 0, dummy_argv_1) != 0) {
                   2448:                                                DeleteFile(dummy_argv_1);
                   2449:                                        } else {
                   2450:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                   2451:                                        }
1.1.1.27  root     2452:                                }
1.1.1.28  root     2453:                                // check the command file type
                   2454:                                fread(buffer, 2, 1, fp);
                   2455:                                fseek(fp, -2, SEEK_CUR);
                   2456:                                if(memcmp(buffer, "MZ", 2) != 0) {
                   2457:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                   2458:                                } else {
                   2459:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root     2460:                                }
                   2461:                        }
1.1.1.28  root     2462:                        
                   2463:                        // restore command file
                   2464:                        FILE* fo = fopen(dummy_argv_1, "wb");
                   2465:                        for(int i = 0; i < file_len; i++) {
                   2466:                                fputc(fgetc(fp), fo);
                   2467:                        }
                   2468:                        fclose(fo);
                   2469:                        
                   2470:                        GetFullPathName(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
                   2471:                        temp_file_created = true;
                   2472:                        SetFileAttributes(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
                   2473:                        
                   2474:                        // adjust argc/argv
                   2475:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                   2476:                                dummy_argv[i + 1] = argv[i];
                   2477:                        }
                   2478:                        argc++;
                   2479:                        argv = dummy_argv;
1.1.1.27  root     2480:                }
                   2481:                fclose(fp);
                   2482:        }
1.1.1.9   root     2483:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     2484:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   2485:                        stay_busy = true;
                   2486:                        arg_offset++;
1.1.1.27  root     2487:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   2488:                        if(argv[i][2] != '\0') {
                   2489:                                strcpy(new_exec_file, &argv[i][2]);
                   2490:                        } else {
                   2491:                                strcpy(new_exec_file, "new_exec_file.exe");
                   2492:                        }
                   2493:                        convert_cmd_file = true;
                   2494:                        arg_offset++;
1.1.1.28  root     2495:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   2496:                        if(IS_NUMERIC(argv[i][2])) {
                   2497:                                code_page = atoi(&argv[i][2]);
                   2498:                        } else {
                   2499:                                code_page = GetConsoleCP();
                   2500:                        }
                   2501:                        arg_offset++;
1.1.1.25  root     2502:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   2503:                        no_windows = true;
                   2504:                        arg_offset++;
                   2505:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     2506:                        standard_env = 1;
                   2507:                        arg_offset++;
1.1.1.14  root     2508:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   2509:                        ignore_illegal_insn = true;
                   2510:                        arg_offset++;
                   2511:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   2512:                        limit_max_memory = true;
                   2513:                        arg_offset++;
                   2514:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.17  root     2515:                        if(sscanf(argv[1] + 2, "%d,%d", &buf_height, &buf_width) != 2) {
                   2516:                                buf_width = buf_height = 0;
                   2517:                        }
                   2518:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   2519:                                buf_width = 80;
                   2520:                        }
                   2521:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   2522:                                buf_height = 25;
                   2523:                        }
1.1.1.14  root     2524:                        arg_offset++;
1.1.1.25  root     2525:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     2526:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     2527:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   2528:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   2529:                                        sio_port_number[1] = atoi(p1 + 1);
                   2530:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   2531:                                                sio_port_number[2] = atoi(p2 + 1);
                   2532:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   2533:                                                        sio_port_number[3] = atoi(p3 + 1);
                   2534:                                                }
                   2535:                                        }
1.1.1.25  root     2536:                                }
1.1.1.29  root     2537:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     2538:                        }
1.1.1.29  root     2539:                        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     2540:                                get_sio_port_numbers();
1.1.1.25  root     2541:                        }
                   2542:                        arg_offset++;
1.1.1.9   root     2543:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     2544:                        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     2545:                                dos_major_version = argv[i][2] - '0';
                   2546:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   2547:                        }
                   2548:                        arg_offset++;
                   2549:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   2550:                        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]))) {
                   2551:                                win_major_version = argv[i][2] - '0';
                   2552:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     2553:                        }
                   2554:                        arg_offset++;
1.1.1.25  root     2555:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   2556:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   2557:                        support_ems = true;
                   2558: #ifdef SUPPORT_XMS
                   2559:                        support_xms = true;
                   2560: #endif
                   2561:                        arg_offset++;
1.1.1.9   root     2562:                } else {
                   2563:                        break;
                   2564:                }
                   2565:        }
                   2566:        if(argc < 2 + arg_offset) {
1.1       root     2567: #ifdef _WIN64
1.1.1.14  root     2568:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     2569: #else
1.1.1.14  root     2570:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     2571: #endif
1.1.1.25  root     2572:                fprintf(stderr,
1.1.1.28  root     2573:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     2574:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     2575:                        "\n"
                   2576:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     2577: #ifdef _WIN64
1.1.1.27  root     2578:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     2579: #else
1.1.1.27  root     2580:                        "\t-c\tconvert command file to 32bit execution file\n"
                   2581: #endif
1.1.1.28  root     2582:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     2583:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   2584:                        "\t-e\tuse a reduced environment block\n"
                   2585:                        "\t-i\tignore invalid instructions\n"
                   2586:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   2587:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   2588:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   2589:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     2590:                        "\t-w\tset the Windows version\n"
1.1.1.19  root     2591: #ifdef SUPPORT_XMS
1.1.1.28  root     2592:                        "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19  root     2593: #else
1.1.1.28  root     2594:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2595: #endif
                   2596:                );
1.1.1.10  root     2597:                
                   2598:                if(!is_started_from_command_prompt()) {
                   2599:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2600:                        while(!_kbhit()) {
                   2601:                                Sleep(10);
                   2602:                        }
                   2603:                }
1.1.1.20  root     2604: #ifdef _DEBUG
                   2605:                _CrtDumpMemoryLeaks();
                   2606: #endif
1.1       root     2607:                return(EXIT_FAILURE);
                   2608:        }
1.1.1.27  root     2609:        if(convert_cmd_file) {
                   2610:                retval = EXIT_FAILURE;
1.1.1.28  root     2611:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2612:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2613:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2614:                        
1.1.1.28  root     2615:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2616:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2617:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2618:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2619:                        } else {
1.1.1.28  root     2620:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2621:                                if(offset != 0) {
                   2622:                                        UINT8 buffer[14];
                   2623:                                        fseek(fp, offset, SEEK_SET);
                   2624:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2625:                                        memset(path, 0, sizeof(path));
                   2626:                                        fread(path, buffer[9], 1, fp);
                   2627:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2628:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2629:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2630:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2631:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2632:                                } else {
                   2633:                                        // read pe header of msdos.exe
                   2634:                                        UINT8 header[0x400];
                   2635:                                        fseek(fp, 0, SEEK_SET);
                   2636:                                        fread(header, sizeof(header), 1, fp);
                   2637:                                        
                   2638:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2639:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2640:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2641:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2642:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2643:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2644:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2645:                                        
                   2646:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2647:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2648:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2649:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2650:                                        if(dwExtraLastSectionBytes != 0) {
                   2651:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2652:                                                dwLastSectionSize += dwRemain;
                   2653:                                        }
                   2654:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2655:                                        
                   2656:                                        // store msdos.exe
                   2657:                                        fseek(fp, 0, SEEK_SET);
                   2658:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2659:                                                if((data = fgetc(fp)) != EOF) {
                   2660:                                                        fputc(data, fo);
                   2661:                                                } else {
                   2662:                                                        // we should not reach here :-(
                   2663:                                                        fputc(0, fo);
                   2664:                                                }
                   2665:                                        }
                   2666:                                        
                   2667:                                        // store options
                   2668:                                        UINT8 flags = 0;
                   2669:                                        if(stay_busy) {
                   2670:                                                flags |= 0x01;
                   2671:                                        }
                   2672:                                        if(no_windows) {
                   2673:                                                flags |= 0x02;
                   2674:                                        }
                   2675:                                        if(standard_env) {
                   2676:                                                flags |= 0x04;
                   2677:                                        }
                   2678:                                        if(ignore_illegal_insn) {
                   2679:                                                flags |= 0x08;
                   2680:                                        }
                   2681:                                        if(limit_max_memory) {
                   2682:                                                flags |= 0x10;
                   2683:                                        }
1.1.1.29  root     2684:                                        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     2685:                                                flags |= 0x20;
                   2686:                                        }
                   2687:                                        if(support_ems) {
                   2688:                                                flags |= 0x40;
                   2689:                                        }
1.1.1.30  root     2690: #ifdef SUPPORT_XMS
                   2691:                                        if(support_xms) {
                   2692:                                                flags |= 0x80;
                   2693:                                        }
                   2694: #endif
1.1.1.28  root     2695:                                        
                   2696:                                        fputc(flags, fo);
                   2697:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2698:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2699:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2700:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2701:                                        fputc(dos_major_version, fo);
                   2702:                                        fputc(dos_minor_version, fo);
                   2703:                                        fputc(win_major_version, fo);
                   2704:                                        fputc(win_minor_version, fo);
1.1.1.28  root     2705:                                        fputc((code_page >> 0) & 0xff, fo);
                   2706:                                        fputc((code_page >> 8) & 0xff, fo);
                   2707:                                        
                   2708:                                        // store command file info
                   2709:                                        GetFullPathName(argv[arg_offset + 1], MAX_PATH, full, &name);
                   2710:                                        int name_len = strlen(name);
                   2711:                                        fseek(fs, 0, SEEK_END);
                   2712:                                        long file_size = ftell(fs);
                   2713:                                        
                   2714:                                        fputc(name_len, fo);
                   2715:                                        fputc((file_size >>  0) & 0xff, fo);
                   2716:                                        fputc((file_size >>  8) & 0xff, fo);
                   2717:                                        fputc((file_size >> 16) & 0xff, fo);
                   2718:                                        fputc((file_size >> 24) & 0xff, fo);
                   2719:                                        fwrite(name, name_len, 1, fo);
                   2720:                                        
                   2721:                                        // store command file
                   2722:                                        fseek(fs, 0, SEEK_SET);
                   2723:                                        for(int i = 0; i < file_size; i++) {
                   2724:                                                if((data = fgetc(fs)) != EOF) {
                   2725:                                                        fputc(data, fo);
                   2726:                                                } else {
                   2727:                                                        // we should not reach here :-(
                   2728:                                                        fputc(0, fo);
                   2729:                                                }
                   2730:                                        }
                   2731:                                        
                   2732:                                        // store padding data and update pe header
1.1.1.29  root     2733:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   2734:                                        coffHeader->NumberOfSections++;
                   2735:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   2736:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   2737:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   2738:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   2739:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     2740:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   2741:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   2742:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     2743:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     2744:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   2745:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     2746:                                                        if(i < 2) {
                   2747:                                                                fputc(padding[i & 15], fo);
                   2748:                                                        } else {
                   2749:                                                                fputc(padding[(i - 2) & 15], fo);
                   2750:                                                        }
1.1.1.28  root     2751:                                                }
                   2752:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   2753:                                        }
                   2754:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   2755:                                        
                   2756:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   2757:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   2758:                                        if(dwExtraNewSectionBytes != 0) {
                   2759:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   2760:                                                dwNewSectionSize += dwRemain;
                   2761:                                        }
                   2762:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   2763:                                        
                   2764:                                        fseek(fo, 0, SEEK_SET);
                   2765:                                        fwrite(header, sizeof(header), 1, fo);
                   2766:                                        
                   2767:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   2768:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     2769:                                }
                   2770:                        }
                   2771:                        if(fp != NULL) {
                   2772:                                fclose(fp);
                   2773:                        }
                   2774:                        if(fs != NULL) {
                   2775:                                fclose(fs);
                   2776:                        }
                   2777:                        if(fo != NULL) {
                   2778:                                fclose(fo);
                   2779:                        }
                   2780:                }
                   2781: #ifdef _DEBUG
                   2782:                _CrtDumpMemoryLeaks();
                   2783: #endif
                   2784:                return(retval);
                   2785:        }
1.1       root     2786:        
1.1.1.14  root     2787:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   2788:        
1.1.1.23  root     2789:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     2790:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     2791:        CONSOLE_CURSOR_INFO ci;
1.1.1.23  root     2792:        
1.1.1.28  root     2793:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     2794:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.24  root     2795:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1       root     2796:        
1.1.1.14  root     2797:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   2798:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   2799:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   2800:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     2801:                }
                   2802:        }
1.1.1.28  root     2803:        if(get_console_info_success) {
1.1.1.12  root     2804:                scr_width = csbi.dwSize.X;
1.1.1.14  root     2805:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2806:                
1.1.1.28  root     2807:                // v-text shadow buffer size must be lesser than 0x7fd0
                   2808:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     2809:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   2810:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   2811:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     2812:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     2813:                                scr_width = 80;
                   2814:                                scr_height = 25;
                   2815:                        }
1.1.1.28  root     2816:                        screen_size_changed = true;
1.1.1.14  root     2817:                }
1.1.1.12  root     2818:        } else {
                   2819:                // for a proof (not a console)
                   2820:                scr_width = 80;
                   2821:                scr_height = 25;
                   2822:        }
1.1.1.14  root     2823:        scr_buf_size.X = scr_width;
                   2824:        scr_buf_size.Y = scr_height;
                   2825:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   2826:        scr_top = csbi.srWindow.Top;
1.1       root     2827:        cursor_moved = false;
                   2828:        
1.1.1.35  root     2829: #ifdef USE_SERVICE_THREAD
                   2830:        InitializeCriticalSection(&input_crit_sect);
                   2831:        InitializeCriticalSection(&key_buf_crit_sect);
                   2832:        InitializeCriticalSection(&putch_crit_sect);
1.1.1.50! root     2833:        main_thread_id = GetCurrentThreadId();
1.1.1.35  root     2834: #endif
1.1.1.50! root     2835:        
1.1.1.25  root     2836:        key_buf_char = new FIFO(256);
                   2837:        key_buf_scan = new FIFO(256);
1.1       root     2838:        
                   2839:        hardware_init();
                   2840:        
1.1.1.33  root     2841: #ifdef USE_DEBUGGER
                   2842:        debugger_init();
                   2843: #endif
                   2844:        
1.1.1.9   root     2845:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     2846:                retval = EXIT_FAILURE;
                   2847:        } else {
1.1.1.27  root     2848: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     2849:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   2850: #endif
                   2851:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   2852:                
1.1.1.28  root     2853:                if(screen_size_changed) {
1.1.1.24  root     2854:                        change_console_size(scr_width, scr_height);
                   2855:                }
1.1.1.8   root     2856:                TIMECAPS caps;
                   2857:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   2858:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.35  root     2859: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2860:                InitializeCriticalSection(&vram_crit_sect);
                   2861:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   2862: #endif
1.1.1.33  root     2863: #ifdef USE_DEBUGGER
                   2864:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   2865:                // wait until telnet client starts and connects to me
                   2866:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   2867:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   2868:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   2869:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   2870:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   2871:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   2872:                                Sleep(100);
                   2873:                        }
                   2874:                }
                   2875: #endif
1.1       root     2876:                hardware_run();
1.1.1.35  root     2877: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2878:                vram_flush();
                   2879:                DeleteCriticalSection(&vram_crit_sect);
                   2880: #endif
1.1.1.24  root     2881:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     2882:                
1.1.1.24  root     2883:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.28  root     2884:                if(get_console_info_success) {
1.1.1.23  root     2885:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2886:                        if(restore_console_on_exit) {
1.1.1.14  root     2887:                                // window can't be bigger than buffer,
                   2888:                                // buffer can't be smaller than window,
                   2889:                                // so make a tiny window,
                   2890:                                // set the required buffer,
                   2891:                                // then set the required window
                   2892:                                SMALL_RECT rect;
                   2893:                                SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
                   2894:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     2895:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     2896:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12  root     2897:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   2898:                        }
1.1.1.14  root     2899:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   2900:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     2901:                }
1.1.1.24  root     2902:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   2903:                
1.1       root     2904:                msdos_finish();
1.1.1.14  root     2905:                
                   2906:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     2907:        }
1.1.1.35  root     2908:        if(temp_file_created) {
                   2909:                DeleteFile(temp_file_path);
                   2910:                temp_file_created = false;
                   2911:        }
1.1.1.10  root     2912:        hardware_finish();
                   2913:        
1.1.1.28  root     2914:        if(key_buf_char != NULL) {
                   2915:                key_buf_char->release();
                   2916:                delete key_buf_char;
                   2917:                key_buf_char = NULL;
                   2918:        }
                   2919:        if(key_buf_scan != NULL) {
                   2920:                key_buf_scan->release();
                   2921:                delete key_buf_scan;
                   2922:                key_buf_scan = NULL;
                   2923:        }
1.1.1.35  root     2924: #ifdef USE_SERVICE_THREAD
                   2925:        DeleteCriticalSection(&input_crit_sect);
                   2926:        DeleteCriticalSection(&key_buf_crit_sect);
                   2927:        DeleteCriticalSection(&putch_crit_sect);
                   2928: #endif
1.1.1.20  root     2929: #ifdef _DEBUG
                   2930:        _CrtDumpMemoryLeaks();
                   2931: #endif
1.1       root     2932:        return(retval);
                   2933: }
                   2934: 
1.1.1.20  root     2935: /* ----------------------------------------------------------------------------
                   2936:        console
                   2937: ---------------------------------------------------------------------------- */
                   2938: 
1.1.1.14  root     2939: void change_console_size(int width, int height)
1.1.1.12  root     2940: {
1.1.1.23  root     2941:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2942:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2943:        SMALL_RECT rect;
                   2944:        COORD co;
                   2945:        
                   2946:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     2947:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   2948:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
                   2949:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
                   2950:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2951:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2952:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   2953:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
                   2954:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2955:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2956:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     2957:                }
                   2958:        }
1.1.1.14  root     2959:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     2960:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     2961:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     2962:                SetConsoleCursorPosition(hStdout, co);
                   2963:                cursor_moved = true;
                   2964:        }
1.1.1.14  root     2965:        
                   2966:        // window can't be bigger than buffer,
                   2967:        // buffer can't be smaller than window,
                   2968:        // so make a tiny window,
                   2969:        // set the required buffer,
                   2970:        // then set the required window
                   2971:        SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12  root     2972:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     2973:        co.X = width;
                   2974:        co.Y = height;
1.1.1.12  root     2975:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     2976:        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2977:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   2978:        
                   2979:        scr_width = scr_buf_size.X = width;
                   2980:        scr_height = scr_buf_size.Y = height;
                   2981:        scr_top = 0;
                   2982:        
                   2983:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   2984:        
                   2985:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     2986:        text_vram_end_address = text_vram_top_address + regen;
                   2987:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   2988:        
1.1.1.14  root     2989:        if(regen > 0x4000) {
                   2990:                regen = 0x8000;
                   2991:                vram_pages = 1;
                   2992:        } else if(regen > 0x2000) {
                   2993:                regen = 0x4000;
                   2994:                vram_pages = 2;
                   2995:        } else if(regen > 0x1000) {
                   2996:                regen = 0x2000;
                   2997:                vram_pages = 4;
                   2998:        } else {
                   2999:                regen = 0x1000;
                   3000:                vram_pages = 8;
                   3001:        }
1.1.1.15  root     3002:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   3003:        *(UINT16 *)(mem + 0x44c) = regen;
                   3004:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   3005:        
1.1.1.24  root     3006:        mouse.min_position.x = 0;
                   3007:        mouse.min_position.y = 0;
1.1.1.34  root     3008:        mouse.max_position.x = 8 * (scr_width  - 1);
                   3009:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     3010:        
1.1.1.15  root     3011:        restore_console_on_exit = true;
1.1.1.14  root     3012: }
                   3013: 
                   3014: void clear_scr_buffer(WORD attr)
                   3015: {
                   3016:        for(int y = 0; y < scr_height; y++) {
                   3017:                for(int x = 0; x < scr_width; x++) {
                   3018:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3019:                        SCR_BUF(y,x).Attributes = attr;
                   3020:                }
                   3021:        }
1.1.1.12  root     3022: }
                   3023: 
1.1.1.24  root     3024: bool update_console_input()
1.1       root     3025: {
1.1.1.35  root     3026: #ifdef USE_SERVICE_THREAD
                   3027:        EnterCriticalSection(&input_crit_sect);
                   3028: #endif
1.1.1.23  root     3029:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     3030:        DWORD dwNumberOfEvents = 0;
1.1       root     3031:        DWORD dwRead;
                   3032:        INPUT_RECORD ir[16];
1.1.1.24  root     3033:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   3034:        bool result = false;
1.1       root     3035:        
1.1.1.8   root     3036:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   3037:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   3038:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     3039:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.34  root     3040:                                        if(mouse.hidden == 0) {
                   3041:                                                // NOTE: if restore_console_on_exit, console is not scrolled
                   3042:                                                if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   3043:                                                        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   3044:                                                }
                   3045:                                                // FIXME: character size is always 8x8 ???
                   3046:                                                int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   3047:                                                int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
                   3048:                                                
                   3049:                                                if(mouse.position.x != x || mouse.position.y != y) {
                   3050:                                                        mouse.position.x = x;
                   3051:                                                        mouse.position.y = y;
                   3052:                                                        mouse.status |= 1;
1.1.1.43  root     3053:                                                        mouse.status_alt |= 1;
1.1.1.34  root     3054:                                                }
                   3055:                                        }
                   3056:                                        if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   3057:                                                for(int i = 0; i < MAX_MOUSE_BUTTONS; i++) {
                   3058:                                                        static const DWORD bits[] = {
                   3059:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   3060:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
                   3061:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
                   3062:                                                        };
                   3063:                                                        bool prev_status = mouse.buttons[i].status;
                   3064:                                                        mouse.buttons[i].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[i]) != 0);
                   3065:                                                        
                   3066:                                                        if(!prev_status && mouse.buttons[i].status) {
                   3067:                                                                mouse.buttons[i].pressed_times++;
                   3068:                                                                mouse.buttons[i].pressed_position.x = mouse.position.x;
                   3069:                                                                mouse.buttons[i].pressed_position.y = mouse.position.x;
1.1.1.43  root     3070:                                                                if(i < 2) {
                   3071:                                                                        mouse.status_alt |= 2 << (i * 2);
                   3072:                                                                }
1.1.1.34  root     3073:                                                                mouse.status |= 2 << (i * 2);
                   3074:                                                        } else if(prev_status && !mouse.buttons[i].status) {
                   3075:                                                                mouse.buttons[i].released_times++;
                   3076:                                                                mouse.buttons[i].released_position.x = mouse.position.x;
                   3077:                                                                mouse.buttons[i].released_position.y = mouse.position.x;
1.1.1.43  root     3078:                                                                if(i < 2) {
                   3079:                                                                        mouse.status_alt |= 4 << (i * 2);
                   3080:                                                                }
1.1.1.34  root     3081:                                                                mouse.status |= 4 << (i * 2);
1.1.1.14  root     3082:                                                        }
                   3083:                                                }
                   3084:                                        }
1.1.1.24  root     3085:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3086:                                        // update keyboard flags in bios data area
1.1.1.35  root     3087:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
                   3088:                                                mem[0x417] |= 0x40;
1.1.1.33  root     3089:                                        } else {
1.1.1.35  root     3090:                                                mem[0x417] &= ~0x40;
1.1.1.33  root     3091:                                        }
1.1.1.35  root     3092:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
                   3093:                                                mem[0x417] |= 0x20;
1.1.1.33  root     3094:                                        } else {
1.1.1.35  root     3095:                                                mem[0x417] &= ~0x20;
                   3096:                                        }
                   3097:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
                   3098:                                                mem[0x417] |= 0x10;
                   3099:                                        } else {
                   3100:                                                mem[0x417] &= ~0x10;
1.1.1.33  root     3101:                                        }
                   3102:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43  root     3103:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3104:                                                        mouse.status_alt |= 0x80;
                   3105:                                                }
1.1.1.33  root     3106:                                                mem[0x417] |= 0x08;
                   3107:                                        } else {
                   3108:                                                mem[0x417] &= ~0x08;
                   3109:                                        }
1.1.1.35  root     3110:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43  root     3111:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3112:                                                        mouse.status_alt |= 0x40;
                   3113:                                                }
1.1.1.35  root     3114:                                                mem[0x417] |= 0x04;
1.1.1.33  root     3115:                                        } else {
1.1.1.35  root     3116:                                                mem[0x417] &= ~0x04;
1.1.1.33  root     3117:                                        }
                   3118:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43  root     3119:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3120:                                                        mouse.status_alt |= 0x20;
                   3121:                                                }
1.1.1.33  root     3122:                                                if(!(mem[0x417] & 0x03)) {
                   3123:                                                        mem[0x417] |= 0x02; // left shift
                   3124:                                                }
                   3125:                                        } else {
                   3126:                                                mem[0x417] &= ~0x03;
                   3127:                                        }
1.1.1.35  root     3128:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3129:                                                mem[0x418] |= 0x02;
                   3130:                                        } else {
                   3131:                                                mem[0x418] &= ~0x02;
                   3132:                                        }
                   3133:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3134:                                                mem[0x418] |= 0x01;
                   3135:                                        } else {
                   3136:                                                mem[0x418] &= ~0x01;
                   3137:                                        }
1.1.1.33  root     3138:                                        
1.1.1.28  root     3139:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.24  root     3140:                                        kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3141:                                        kbd_status |= 1;
                   3142:                                        
                   3143:                                        // update dos key buffer
                   3144:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3145:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
                   3146:                                        
                   3147:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3148:                                                // make
1.1.1.24  root     3149:                                                kbd_data &= 0x7f;
                   3150:                                                
1.1.1.33  root     3151:                                                if(chr == 0x00) {
1.1.1.24  root     3152:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3153:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3154:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3155:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3156:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3157:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3158:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3159:                                                                } else if(scn == 0x35) {
                   3160:                                                                        scn = 0xa4;             // keypad /
                   3161:                                                                }
                   3162:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3163:                                                                if(scn == 0x07) {
                   3164:                                                                        chr = 0x1e;     // Ctrl+^
                   3165:                                                                } else if(scn == 0x0c) {
                   3166:                                                                        chr = 0x1f;     // Ctrl+_
                   3167:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3168:                                                                        static const UINT8 ctrl_map[] = {
                   3169:                                                                                0x95,   // keypad /
                   3170:                                                                                0,
                   3171:                                                                                0x96,   // keypad *
                   3172:                                                                                0, 0, 0,
                   3173:                                                                                0x5e,   // F1
                   3174:                                                                                0x5f,   // F2
                   3175:                                                                                0x60,   // F3
                   3176:                                                                                0x61,   // F4
                   3177:                                                                                0x62,   // F5
                   3178:                                                                                0x63,   // F6
                   3179:                                                                                0x64,   // F7
                   3180:                                                                                0x65,   // F8
                   3181:                                                                                0x66,   // F9
                   3182:                                                                                0x67,   // F10
                   3183:                                                                                0,
                   3184:                                                                                0,
                   3185:                                                                                0x77,   // Home
                   3186:                                                                                0x8d,   // Up
                   3187:                                                                                0x84,   // PgUp
                   3188:                                                                                0x8e,   // keypad -
                   3189:                                                                                0x73,   // Left
                   3190:                                                                                0x8f,   // keypad center
                   3191:                                                                                0x74,   // Right
                   3192:                                                                                0x90,   // keyapd +
                   3193:                                                                                0x75,   // End
                   3194:                                                                                0x91,   // Down
                   3195:                                                                                0x76,   // PgDn
                   3196:                                                                                0x92,   // Insert
                   3197:                                                                                0x93,   // Delete
                   3198:                                                                                0, 0, 0,
                   3199:                                                                                0x89,   // F11
                   3200:                                                                                0x8a,   // F12
                   3201:                                                                        };
                   3202:                                                                        scn = ctrl_map[scn - 0x35];
                   3203:                                                                }
                   3204:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3205:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3206:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3207:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3208:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3209:                                                                }
                   3210:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3211:                                                                scn += 0x85 - 0x57;
                   3212:                                                        }
                   3213:                                                        // ignore shift, ctrl, alt, win and menu keys
                   3214:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && (scn < 0x5b || scn > 0x5e)) {
1.1.1.32  root     3215:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3216: #ifdef USE_SERVICE_THREAD
                   3217:                                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3218: #endif
1.1.1.32  root     3219:                                                                        if(chr == 0) {
                   3220:                                                                                key_buf_char->write(0x00);
                   3221:                                                                                key_buf_scan->write(ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
                   3222:                                                                        }
                   3223:                                                                        key_buf_char->write(chr);
                   3224:                                                                        key_buf_scan->write(scn);
1.1.1.35  root     3225: #ifdef USE_SERVICE_THREAD
                   3226:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3227: #endif
1.1.1.24  root     3228:                                                                }
                   3229:                                                        }
                   3230:                                                } else {
                   3231:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3232:                                                                chr = 0;
                   3233:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3234:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3235:                                                                }
                   3236:                                                        }
1.1.1.32  root     3237:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3238: #ifdef USE_SERVICE_THREAD
                   3239:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3240: #endif
1.1.1.32  root     3241:                                                                key_buf_char->write(chr);
                   3242:                                                                key_buf_scan->write(scn);
1.1.1.35  root     3243: #ifdef USE_SERVICE_THREAD
                   3244:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3245: #endif
1.1.1.32  root     3246:                                                        }
1.1.1.24  root     3247:                                                }
1.1.1.33  root     3248:                                        } else if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
                   3249:                                                // ctrl-break, ctrl-c
                   3250:                                                if(scn == 0x46) {
                   3251:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3252: #ifdef USE_SERVICE_THREAD
                   3253:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3254: #endif
1.1.1.33  root     3255:                                                                key_buf_char->write(0x00);
                   3256:                                                                key_buf_scan->write(0x00);
1.1.1.35  root     3257: #ifdef USE_SERVICE_THREAD
                   3258:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3259: #endif
1.1.1.33  root     3260:                                                        }
                   3261:                                                        ctrl_break_pressed = true;
                   3262:                                                        mem[0x471] = 0x80;
                   3263:                                                        raise_int_1bh = true;
                   3264:                                                } else {
                   3265:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3266: #ifdef USE_SERVICE_THREAD
                   3267:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3268: #endif
1.1.1.33  root     3269:                                                                key_buf_char->write(chr);
                   3270:                                                                key_buf_scan->write(scn);
1.1.1.35  root     3271: #ifdef USE_SERVICE_THREAD
                   3272:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3273: #endif
1.1.1.33  root     3274:                                                        }
                   3275:                                                        ctrl_c_pressed = (scn == 0x2e);
                   3276:                                                }
                   3277:                                        } else {
                   3278:                                                // break
                   3279:                                                kbd_data |= 0x80;
1.1       root     3280:                                        }
1.1.1.24  root     3281:                                        result = key_changed = true;
1.1.1.36  root     3282:                                        // IME may be on and it may causes screen scroll up and cursor position change
                   3283:                                        cursor_moved = true;
1.1       root     3284:                                }
                   3285:                        }
                   3286:                }
                   3287:        }
1.1.1.35  root     3288: #ifdef USE_SERVICE_THREAD
                   3289:        LeaveCriticalSection(&input_crit_sect);
                   3290: #endif
1.1.1.24  root     3291:        return(result);
1.1.1.8   root     3292: }
                   3293: 
1.1.1.14  root     3294: bool update_key_buffer()
1.1.1.8   root     3295: {
1.1.1.35  root     3296:        if(update_console_input()) {
                   3297:                return(true);
                   3298:        }
                   3299:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3300: #ifdef USE_SERVICE_THREAD
                   3301:                EnterCriticalSection(&key_buf_crit_sect);
                   3302: #endif
                   3303:                bool empty = key_buf_char->empty();
                   3304: #ifdef USE_SERVICE_THREAD
                   3305:                LeaveCriticalSection(&key_buf_crit_sect);
                   3306: #endif
                   3307:                if(!empty) return(true);
                   3308:        }
                   3309:        return(false);
1.1.1.8   root     3310: }
                   3311: 
1.1.1.20  root     3312: /* ----------------------------------------------------------------------------
                   3313:        MS-DOS virtual machine
                   3314: ---------------------------------------------------------------------------- */
                   3315: 
1.1.1.32  root     3316: static const struct {
1.1.1.33  root     3317:        char *name;
                   3318:        DWORD lcid;
                   3319:        char *std;
                   3320:        char *dlt;
                   3321: } tz_table[] = {
                   3322:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3323: //     0       GMT             Greenwich Mean Time             GMT0
                   3324:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3325:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3326:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3327:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3328: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3329:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3330:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3331: //     3       BST             Brazil Standard Time            BST3
                   3332:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3333:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3334:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3335: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3336:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3337: //     3       GST             Greenland Standard Time         GST3
                   3338:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3339: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3340:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3341: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3342:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3343: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3344:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3345:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3346: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3347:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3348:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3349:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3350: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3351:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3352: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3353:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3354: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3355:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3356: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3357:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3358:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3359:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3360: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3361:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3362: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3363:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3364:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3365:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3366: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3367:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3368:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3369: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3370: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3371:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3372: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3373:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3374:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3375: //     11      SST             Samoa Standard Time             SST11
                   3376:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3377: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3378:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3379: //     -10     GST             Guam Standard Time              GST-10
                   3380:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3381: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3382:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3383:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3384:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3385: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3386:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3387:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3388: //     -9      JST             Japan Standard Time             JST-9
                   3389:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3390: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3391:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3392:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3393: //     -8      HKT             Hong Kong Time                  HKT-8
                   3394:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3395: //     -8      CCT             China Coast Time                CCT-8
                   3396:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3397:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3398: //     -8      SST             Singapore Standard Time         SST-8
                   3399:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3400: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3401:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3402:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3403: //     -7:30   JT              Java Standard Time              JST-7:30
                   3404: //     -7      NST             North Sumatra Time              NST-7
                   3405:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3406: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3407:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3408: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3409:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3410: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3411:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3412:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3413: //     -2      EET             Eastern Europe Time             EET-2
                   3414:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3415:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3416:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3417:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3418: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3419:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3420: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3421: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3422: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3423: //     -1      CET     CES     Central European Time           CET-1CES
                   3424:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3425:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3426:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3427:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3428: //     -1      WAT             West African Time               WAT-1
                   3429:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3430:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3431: //     0       UTC             Universal Coordinated Time      UTC0
                   3432:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3433:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3434:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3435:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3436:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3437:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3438: };
                   3439: 
                   3440: static const struct {
1.1.1.32  root     3441:        UINT16 code;
                   3442:        char *message_english;
                   3443:        char *message_japanese;
                   3444: } standard_error_table[] = {
                   3445:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3446:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3447:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3448:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3449:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3450:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3451:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3452:        {0x08,  "Insufficient memory", "������������܂���."},
                   3453:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3454:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3455:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3456:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3457:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3458:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3459:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3460:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3461:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3462:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3463:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3464:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3465:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3466:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3467:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3468:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3469:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3470:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3471:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3472:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3473:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3474:        {0x1F,  "General failure", "�G���[�ł�."},
                   3475:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3476:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3477:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3478:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3479:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3480:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3481:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3482:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3483: /*
                   3484:        {0x32,  "Network request not supported", NULL},
                   3485:        {0x33,  "Remote computer not listening", NULL},
                   3486:        {0x34,  "Duplicate name on network", NULL},
                   3487:        {0x35,  "Network name not found", NULL},
                   3488:        {0x36,  "Network busy", NULL},
                   3489:        {0x37,  "Network device no longer exists", NULL},
                   3490:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3491:        {0x39,  "Network adapter hardware error", NULL},
                   3492:        {0x3A,  "Incorrect response from network", NULL},
                   3493:        {0x3B,  "Unexpected network error", NULL},
                   3494:        {0x3C,  "Incompatible remote adapter", NULL},
                   3495:        {0x3D,  "Print queue full", NULL},
                   3496:        {0x3E,  "Queue not full", NULL},
                   3497:        {0x3F,  "Not enough space to print file", NULL},
                   3498:        {0x40,  "Network name was deleted", NULL},
                   3499:        {0x41,  "Network: Access denied", NULL},
                   3500:        {0x42,  "Network device type incorrect", NULL},
                   3501:        {0x43,  "Network name not found", NULL},
                   3502:        {0x44,  "Network name limit exceeded", NULL},
                   3503:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3504:        {0x46,  "Temporarily paused", NULL},
                   3505:        {0x47,  "Network request not accepted", NULL},
                   3506:        {0x48,  "Network print/disk redirection paused", NULL},
                   3507:        {0x49,  "Network software not installed", NULL},
                   3508:        {0x4A,  "Unexpected adapter close", NULL},
                   3509: */
                   3510:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3511:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3512:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3513:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3514:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3515:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3516:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3517:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3518:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3519:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
                   3520: /*
                   3521:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3522:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3523:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3524:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3525:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
                   3526: */
                   3527:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3528:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3529:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3530:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3531:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3532:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3533: };
                   3534: 
                   3535: static const struct {
                   3536:        UINT16 code;
                   3537:        char *message_english;
                   3538:        char *message_japanese;
                   3539: } param_error_table[] = {
                   3540:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3541:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3542:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3543:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3544:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3545:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3546:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3547:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3548:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3549:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3550:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3551: };
                   3552: 
                   3553: static const struct {
                   3554:        UINT16 code;
                   3555:        char *message_english;
                   3556:        char *message_japanese;
                   3557: } critical_error_table[] = {
                   3558:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3559:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3560:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3561:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3562:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3563:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3564:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3565:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3566:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3567:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3568:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3569:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3570:        {0x0C,  "General failure", "�G���[�ł�."},
                   3571:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3572:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3573:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3574:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3575:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3576:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3577:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3578:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3579:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3580: };
                   3581: 
1.1.1.20  root     3582: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3583: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3584: void msdos_putch(UINT8 data);
1.1.1.50! root     3585: void msdos_putch_fast(UINT8 data);
1.1.1.35  root     3586: #ifdef USE_SERVICE_THREAD
                   3587: void msdos_putch_tmp(UINT8 data);
                   3588: #endif
1.1.1.45  root     3589: const char *msdos_short_path(const char *path);
1.1.1.44  root     3590: bool msdos_is_valid_drive(int drv);
                   3591: bool msdos_is_removable_drive(int drv);
                   3592: bool msdos_is_cdrom_drive(int drv);
                   3593: bool msdos_is_remote_drive(int drv);
                   3594: bool msdos_is_subst_drive(int drv);
1.1.1.20  root     3595: 
1.1       root     3596: // process info
                   3597: 
                   3598: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3599: {
                   3600:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3601:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3602:                        memset(&process[i], 0, sizeof(process_t));
                   3603:                        process[i].psp = psp_seg;
                   3604:                        return(&process[i]);
                   3605:                }
                   3606:        }
                   3607:        fatalerror("too many processes\n");
                   3608:        return(NULL);
                   3609: }
                   3610: 
1.1.1.33  root     3611: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error)
1.1       root     3612: {
                   3613:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3614:                if(process[i].psp == psp_seg) {
                   3615:                        return(&process[i]);
                   3616:                }
                   3617:        }
1.1.1.33  root     3618:        if(show_error) {
                   3619:                fatalerror("invalid psp address\n");
                   3620:        }
1.1       root     3621:        return(NULL);
                   3622: }
                   3623: 
1.1.1.33  root     3624: process_t *msdos_process_info_get(UINT16 psp_seg)
                   3625: {
                   3626:        return(msdos_process_info_get(psp_seg, true));
                   3627: }
                   3628: 
1.1.1.23  root     3629: void msdos_sda_update(int psp_seg)
                   3630: {
                   3631:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3632:        
                   3633:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3634:                if(process[i].psp == psp_seg) {
                   3635:                        sda->switchar = process[i].switchar;
                   3636:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3637:                        sda->current_dta.w.h = process[i].dta.w.h;
                   3638:                        sda->current_psp = process[i].psp;
                   3639:                        break;
                   3640:                }
                   3641:        }
                   3642:        sda->malloc_strategy = malloc_strategy;
                   3643:        sda->return_code = retval;
                   3644:        sda->current_drive = _getdrive();
                   3645: }
                   3646: 
1.1.1.13  root     3647: // dta info
                   3648: 
                   3649: void msdos_dta_info_init()
                   3650: {
1.1.1.14  root     3651:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     3652:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3653:        }
                   3654: }
                   3655: 
                   3656: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   3657: {
                   3658:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     3659:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3660:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   3661:                        if(free_dta == NULL) {
1.1.1.13  root     3662:                                free_dta = &dtalist[i];
                   3663:                        }
1.1.1.14  root     3664:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     3665:                        return(&dtalist[i]);
                   3666:                }
                   3667:        }
1.1.1.14  root     3668:        if(free_dta) {
1.1.1.13  root     3669:                free_dta->psp = psp_seg;
                   3670:                free_dta->dta = dta_laddr;
                   3671:                return(free_dta);
                   3672:        }
                   3673:        fatalerror("too many dta\n");
                   3674:        return(NULL);
                   3675: }
                   3676: 
                   3677: void msdos_dta_info_free(UINT16 psp_seg)
                   3678: {
1.1.1.14  root     3679:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3680:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     3681:                        FindClose(dtalist[i].find_handle);
                   3682:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3683:                }
                   3684:        }
                   3685: }
                   3686: 
1.1       root     3687: void msdos_cds_update(int drv)
                   3688: {
1.1.1.44  root     3689:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1       root     3690:        
1.1.1.44  root     3691:        memset(cds, 0, 88);
                   3692:        
                   3693:        if(msdos_is_valid_drive(drv)) {
                   3694:                char path[MAX_PATH];
                   3695:                if(msdos_is_remote_drive(drv)) {
                   3696:                        cds->drive_attrib = 0xc000;     // network drive
                   3697:                } else if(msdos_is_subst_drive(drv)) {
                   3698:                        cds->drive_attrib = 0x5000;     // subst drive
                   3699:                } else {
                   3700:                        cds->drive_attrib = 0x4000;     // physical drive
                   3701:                }
                   3702:                if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
                   3703:                        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
                   3704:                }
                   3705:        }
                   3706:        if(cds->path_name[0] == '\0') {
                   3707:                sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   3708:        }
                   3709:        cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   3710:        cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
                   3711:        cds->word_1 = cds->word_2 = 0xffff;
                   3712:        cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
                   3713:        cds->bs_offset = 2;
                   3714: }
                   3715: 
1.1.1.45  root     3716: void msdos_cds_update(int drv, const char *path)
1.1.1.44  root     3717: {
                   3718:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
                   3719:        
                   3720:        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1       root     3721: }
                   3722: 
1.1.1.17  root     3723: // nls information tables
                   3724: 
                   3725: // uppercase table (func 6502h)
                   3726: void msdos_upper_table_update()
                   3727: {
                   3728:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3729:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3730:                UINT8 c[4];
1.1.1.33  root     3731:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3732:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     3733:                c[0] = 0x80 + i;
                   3734:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   3735:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3736:        }
                   3737: }
                   3738: 
1.1.1.23  root     3739: // lowercase table (func 6503h)
                   3740: void msdos_lower_table_update()
                   3741: {
                   3742:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   3743:        for(unsigned i = 0; i < 0x80; ++i) {
                   3744:                UINT8 c[4];
1.1.1.33  root     3745:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3746:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     3747:                c[0] = 0x80 + i;
                   3748:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   3749:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3750:        }
                   3751: }
                   3752: 
1.1.1.17  root     3753: // filename uppercase table (func 6504h)
                   3754: void msdos_filename_upper_table_init()
                   3755: {
                   3756:        // depended on (file)system, not on active codepage
                   3757:        // temporary solution: just filling data
                   3758:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3759:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3760:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   3761:        }
                   3762: }
                   3763: 
                   3764: // filaname terminator table (func 6505h)
                   3765: void msdos_filename_terminator_table_init()
                   3766: {
                   3767:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   3768:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   3769:        
                   3770:        data[2] = 1;            // marker? (permissible character value)
                   3771:        data[3] = 0x00;         // 00h...FFh
                   3772:        data[4] = 0xff;
                   3773:        data[5] = 0;            // marker? (excluded character)
                   3774:        data[6] = 0x00;         // 00h...20h
                   3775:        data[7] = 0x20;
                   3776:        data[8] = 2;            // marker? (illegal characters for filename)
                   3777:        data[9] = (UINT8)strlen(illegal_chars);
                   3778:        memcpy(data + 10, illegal_chars, data[9]);
                   3779:        
                   3780:        // total length
                   3781:        *(UINT16 *)data = (10 - 2) + data[9];
                   3782: }
                   3783: 
                   3784: // collating table (func 6506h)
                   3785: void msdos_collating_table_update()
                   3786: {
                   3787:        // temporary solution: just filling data
                   3788:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     3789:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     3790:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   3791:        }
                   3792: }
                   3793: 
1.1       root     3794: // dbcs
                   3795: 
                   3796: void msdos_dbcs_table_update()
                   3797: {
                   3798:        UINT8 dbcs_data[DBCS_SIZE];
                   3799:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   3800:        
                   3801:        CPINFO info;
                   3802:        GetCPInfo(active_code_page, &info);
                   3803:        
                   3804:        if(info.MaxCharSize != 1) {
                   3805:                for(int i = 0;; i += 2) {
                   3806:                        UINT8 lo = info.LeadByte[i + 0];
                   3807:                        UINT8 hi = info.LeadByte[i + 1];
                   3808:                        dbcs_data[2 + i + 0] = lo;
                   3809:                        dbcs_data[2 + i + 1] = hi;
                   3810:                        if(lo == 0 && hi == 0) {
                   3811:                                dbcs_data[0] = i + 2;
                   3812:                                break;
                   3813:                        }
                   3814:                }
                   3815:        } else {
                   3816:                dbcs_data[0] = 2;       // ???
                   3817:        }
                   3818:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   3819: }
                   3820: 
1.1.1.17  root     3821: void msdos_dbcs_table_finish()
                   3822: {
1.1.1.32  root     3823:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     3824:                _setmbcp(system_code_page);
                   3825:        }
1.1.1.32  root     3826:        if(console_code_page != GetConsoleCP()) {
                   3827:                SetConsoleCP(console_code_page);
                   3828:                SetConsoleOutputCP(console_code_page);
                   3829:        }
1.1.1.17  root     3830: }
                   3831: 
                   3832: void msdos_nls_tables_init()
1.1       root     3833: {
1.1.1.32  root     3834:        active_code_page = console_code_page = GetConsoleCP();
                   3835:        system_code_page = _getmbcp();
                   3836:        
                   3837:        if(active_code_page != system_code_page) {
                   3838:                if(_setmbcp(active_code_page) != 0) {
                   3839:                        active_code_page = system_code_page;
                   3840:                }
                   3841:        }
                   3842:        
1.1.1.17  root     3843:        msdos_upper_table_update();
1.1.1.23  root     3844:        msdos_lower_table_update();
1.1.1.17  root     3845:        msdos_filename_terminator_table_init();
                   3846:        msdos_filename_upper_table_init();
                   3847:        msdos_collating_table_update();
1.1       root     3848:        msdos_dbcs_table_update();
                   3849: }
                   3850: 
1.1.1.17  root     3851: void msdos_nls_tables_update()
1.1       root     3852: {
1.1.1.17  root     3853:        msdos_dbcs_table_update();
                   3854:        msdos_upper_table_update();
1.1.1.23  root     3855:        msdos_lower_table_update();
                   3856: //     msdos_collating_table_update();
1.1       root     3857: }
                   3858: 
                   3859: int msdos_lead_byte_check(UINT8 code)
                   3860: {
                   3861:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   3862:        
                   3863:        for(int i = 0;; i += 2) {
                   3864:                UINT8 lo = dbcs_table[i + 0];
                   3865:                UINT8 hi = dbcs_table[i + 1];
                   3866:                if(lo == 0 && hi == 0) {
                   3867:                        break;
                   3868:                }
                   3869:                if(lo <= code && code <= hi) {
                   3870:                        return(1);
                   3871:                }
                   3872:        }
                   3873:        return(0);
                   3874: }
                   3875: 
1.1.1.20  root     3876: int msdos_ctrl_code_check(UINT8 code)
                   3877: {
1.1.1.22  root     3878:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     3879: }
                   3880: 
1.1.1.36  root     3881: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
                   3882: {
                   3883:        int is_kanji_1st = 0;
                   3884:        int is_kanji_2nd = 0;
                   3885:        
                   3886:        for(int p = 0;; p++) {
                   3887:                if(is_kanji_1st) {
                   3888:                        is_kanji_1st = 0;
                   3889:                        is_kanji_2nd = 1;
                   3890:                } else if(msdos_lead_byte_check(buf[p])) {
                   3891:                        is_kanji_1st = 1;
                   3892:                }
                   3893:                if(p == n) {
                   3894:                        return(is_kanji_2nd);
                   3895:                }
                   3896:                is_kanji_2nd = 0;
                   3897:        }
                   3898: }
                   3899: 
1.1       root     3900: // file control
                   3901: 
1.1.1.45  root     3902: const char *msdos_remove_double_quote(const char *path)
1.1.1.14  root     3903: {
                   3904:        static char tmp[MAX_PATH];
                   3905:        
                   3906:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45  root     3907:                memset(tmp, 0, sizeof(tmp));
1.1.1.14  root     3908:                memcpy(tmp, path + 1, strlen(path) - 2);
                   3909:        } else {
                   3910:                strcpy(tmp, path);
                   3911:        }
                   3912:        return(tmp);
                   3913: }
                   3914: 
1.1.1.45  root     3915: const char *msdos_remove_end_separator(const char *path)
1.1.1.32  root     3916: {
                   3917:        static char tmp[MAX_PATH];
                   3918:        
                   3919:        strcpy(tmp, path);
1.1.1.45  root     3920:        
                   3921:        // for example "C:\" case, the end separator should not be removed
                   3922:        if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
                   3923:                tmp[strlen(tmp) - 1] = '\0';
1.1.1.32  root     3924:        }
                   3925:        return(tmp);
                   3926: }
                   3927: 
1.1.1.45  root     3928: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14  root     3929: {
                   3930:        static char tmp[MAX_PATH];
1.1.1.45  root     3931:        const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14  root     3932:        
                   3933:        if(strlen(tmp_dir) == 0) {
                   3934:                strcpy(tmp, file);
                   3935:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   3936:                sprintf(tmp, "%s%s", tmp_dir, file);
                   3937:        } else {
                   3938:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   3939:        }
                   3940:        return(tmp);
                   3941: }
                   3942: 
1.1.1.45  root     3943: const char *msdos_trimmed_path(const char *path, int lfn)
1.1       root     3944: {
                   3945:        static char tmp[MAX_PATH];
                   3946:        
                   3947:        if(lfn) {
                   3948:                strcpy(tmp, path);
                   3949:        } else {
                   3950:                // remove space in the path
1.1.1.45  root     3951:                const char *src = path;
                   3952:                char *dst = tmp;
1.1       root     3953:                
                   3954:                while(*src != '\0') {
                   3955:                        if(msdos_lead_byte_check(*src)) {
                   3956:                                *dst++ = *src++;
                   3957:                                *dst++ = *src++;
                   3958:                        } else if(*src != ' ') {
                   3959:                                *dst++ = *src++;
                   3960:                        } else {
                   3961:                                src++;  // skip space
                   3962:                        }
                   3963:                }
                   3964:                *dst = '\0';
                   3965:        }
1.1.1.14  root     3966:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   3967:                // redirect C:\COMMAND.COM to comspec_path
                   3968:                strcpy(tmp, comspec_path);
                   3969:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   3970:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   3971:                static int root_drive_protected = -1;
                   3972:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   3973:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   3974:                
                   3975:                if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
                   3976:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   3977:                        strcpy(name, name_temp);
                   3978:                        name_temp[0] = '\0';
                   3979:                        
                   3980:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   3981:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   3982:                                if(root_drive_protected == -1) {
                   3983:                                        FILE *fp = NULL;
                   3984:                                        
                   3985:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   3986:                                        root_drive_protected = 1;
                   3987:                                        try {
                   3988:                                                if((fp = fopen(temp, "w")) != NULL) {
                   3989:                                                        if(fprintf(fp, "TEST") == 4) {
                   3990:                                                                root_drive_protected = 0;
                   3991:                                                        }
                   3992:                                                }
                   3993:                                        } catch(...) {
                   3994:                                        }
                   3995:                                        if(fp != NULL) {
                   3996:                                                fclose(fp);
                   3997:                                        }
                   3998:                                        if(_access(temp, 0) == 0) {
                   3999:                                                remove(temp);
                   4000:                                        }
                   4001:                                }
                   4002:                                if(root_drive_protected == 1) {
                   4003:                                        if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
                   4004:                                           GetEnvironmentVariable("TMP",  temp, MAX_PATH) != 0) {
                   4005:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   4006:                                        }
                   4007:                                }
                   4008:                        }
                   4009:                }
                   4010:        }
1.1       root     4011:        return(tmp);
                   4012: }
                   4013: 
1.1.1.45  root     4014: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28  root     4015: {
1.1.1.32  root     4016:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     4017:        static char env_path[ENV_SIZE];
                   4018:        char tmp[ENV_SIZE], *token;
                   4019:        
                   4020:        memset(env_path, 0, sizeof(env_path));
                   4021:        strcpy(tmp, src);
                   4022:        token = my_strtok(tmp, ";");
                   4023:        
                   4024:        while(token != NULL) {
                   4025:                if(token[0] != '\0') {
1.1.1.45  root     4026:                        const char *path = msdos_remove_double_quote(token);
                   4027:                        char short_path[MAX_PATH];
1.1.1.32  root     4028:                        if(path != NULL && strlen(path) != 0) {
                   4029:                                if(env_path[0] != '\0') {
                   4030:                                        strcat(env_path, ";");
                   4031:                                }
1.1.1.28  root     4032:                                if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     4033:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     4034:                                } else {
                   4035:                                        my_strupr(short_path);
1.1.1.32  root     4036:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     4037:                                }
                   4038:                        }
                   4039:                }
                   4040:                token = my_strtok(NULL, ";");
                   4041:        }
                   4042:        return(env_path);
                   4043: }
                   4044: 
1.1.1.45  root     4045: bool match(const char *text, const char *pattern)
1.1       root     4046: {
1.1.1.24  root     4047:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     4048:        switch(*pattern) {
1.1       root     4049:        case '\0':
                   4050:                return !*text;
                   4051:        case '*':
1.1.1.14  root     4052:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     4053:        case '?':
                   4054:                return *text && match(text + 1, pattern + 1);
                   4055:        default:
                   4056:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   4057:        }
                   4058: }
                   4059: 
1.1.1.45  root     4060: bool msdos_match_volume_label(const char *path, const char *volume)
1.1       root     4061: {
1.1.1.45  root     4062:        const char *p = NULL;
1.1       root     4063:        
1.1.1.14  root     4064:        if(!*volume) {
                   4065:                return false;
                   4066:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     4067:                return msdos_match_volume_label(p + 1, volume);
                   4068:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   4069:                return msdos_match_volume_label(p + 1, volume);
                   4070:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     4071:                char tmp[MAX_PATH];
                   4072:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   4073:                return match(volume, tmp);
1.1       root     4074:        } else {
                   4075:                return match(volume, path);
                   4076:        }
                   4077: }
                   4078: 
1.1.1.45  root     4079: const char *msdos_fcb_path(fcb_t *fcb)
1.1       root     4080: {
                   4081:        static char tmp[MAX_PATH];
                   4082:        char name[9], ext[4];
                   4083:        
                   4084:        memset(name, 0, sizeof(name));
                   4085:        memcpy(name, fcb->file_name, 8);
                   4086:        strcpy(name, msdos_trimmed_path(name, 0));
                   4087:        
                   4088:        memset(ext, 0, sizeof(ext));
                   4089:        memcpy(ext, fcb->file_name + 8, 3);
                   4090:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4091:        
                   4092:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4093:                strcpy(name, "*");
                   4094:        }
                   4095:        if(ext[0] == '\0') {
                   4096:                strcpy(tmp, name);
                   4097:        } else {
                   4098:                if(strcmp(ext, "???") == 0) {
                   4099:                        strcpy(ext, "*");
                   4100:                }
                   4101:                sprintf(tmp, "%s.%s", name, ext);
                   4102:        }
                   4103:        return(tmp);
                   4104: }
                   4105: 
1.1.1.45  root     4106: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1       root     4107: {
                   4108:        char *ext = my_strchr(path, '.');
                   4109:        
                   4110:        memset(fcb->file_name, 0x20, 8 + 3);
                   4111:        if(ext != NULL && path[0] != '.') {
                   4112:                *ext = '\0';
                   4113:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4114:        }
                   4115:        memcpy(fcb->file_name, path, strlen(path));
                   4116: }
                   4117: 
1.1.1.45  root     4118: const char *msdos_short_path(const char *path)
1.1       root     4119: {
                   4120:        static char tmp[MAX_PATH];
                   4121:        
1.1.1.24  root     4122:        if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
                   4123:                strcpy(tmp, path);
                   4124:        }
1.1       root     4125:        my_strupr(tmp);
                   4126:        return(tmp);
                   4127: }
                   4128: 
1.1.1.45  root     4129: const char *msdos_short_name(WIN32_FIND_DATA *fd)
1.1.1.13  root     4130: {
                   4131:        static char tmp[MAX_PATH];
1.1.1.45  root     4132:        
1.1.1.14  root     4133:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4134:                strcpy(tmp, fd->cAlternateFileName);
                   4135:        } else {
                   4136:                strcpy(tmp, fd->cFileName);
                   4137:        }
                   4138:        my_strupr(tmp);
                   4139:        return(tmp);
                   4140: }
                   4141: 
1.1.1.45  root     4142: const char *msdos_short_full_path(const char *path)
1.1       root     4143: {
                   4144:        static char tmp[MAX_PATH];
                   4145:        char full[MAX_PATH], *name;
                   4146:        
1.1.1.14  root     4147:        // Full works with non-existent files, but Short does not
1.1       root     4148:        GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14  root     4149:        *tmp = '\0';
                   4150:        if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
                   4151:                name[-1] = '\0';
                   4152:                DWORD len = GetShortPathName(full, tmp, MAX_PATH);
                   4153:                if(len == 0) {
                   4154:                        strcpy(tmp, full);
                   4155:                } else {
                   4156:                        tmp[len++] = '\\';
                   4157:                        strcpy(tmp + len, name);
                   4158:                }
                   4159:        }
1.1       root     4160:        my_strupr(tmp);
                   4161:        return(tmp);
                   4162: }
                   4163: 
1.1.1.45  root     4164: const char *msdos_short_full_dir(const char *path)
1.1       root     4165: {
                   4166:        static char tmp[MAX_PATH];
                   4167:        char full[MAX_PATH], *name;
                   4168:        
                   4169:        GetFullPathName(path, MAX_PATH, full, &name);
                   4170:        name[-1] = '\0';
1.1.1.24  root     4171:        if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
                   4172:                strcpy(tmp, full);
                   4173:        }
1.1       root     4174:        my_strupr(tmp);
                   4175:        return(tmp);
                   4176: }
                   4177: 
1.1.1.45  root     4178: const char *msdos_local_file_path(const char *path, int lfn)
1.1       root     4179: {
1.1.1.45  root     4180:        static char trimmed[MAX_PATH];
                   4181:        
                   4182:        strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14  root     4183: #if 0
                   4184:        // I have forgotten the reason of this routine... :-(
1.1       root     4185:        if(_access(trimmed, 0) != 0) {
                   4186:                process_t *process = msdos_process_info_get(current_psp);
                   4187:                static char tmp[MAX_PATH];
                   4188:                
                   4189:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4190:                if(_access(tmp, 0) == 0) {
                   4191:                        return(tmp);
                   4192:                }
                   4193:        }
1.1.1.14  root     4194: #endif
1.1       root     4195:        return(trimmed);
                   4196: }
                   4197: 
1.1.1.45  root     4198: bool msdos_is_device_path(const char *path)
1.1.1.11  root     4199: {
                   4200:        char full[MAX_PATH], *name;
                   4201:        
1.1.1.24  root     4202:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4203:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4204:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4205:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4206:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4207:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4208:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4209:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4210:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4211:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4212:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4213:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4214:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4215:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4216:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4217:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4218:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4219:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4220:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4221:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4222:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4223:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4224:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4225:                        return(true);
                   4226:                } else if(name != NULL) {
                   4227:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4228:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4229:                           _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43  root     4230: //                        _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30  root     4231:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4232:                                return(true);
                   4233:                        }
                   4234:                }
1.1.1.24  root     4235:        }
                   4236:        return(false);
1.1.1.11  root     4237: }
                   4238: 
1.1.1.45  root     4239: bool msdos_is_con_path(const char *path)
1.1.1.8   root     4240: {
1.1.1.14  root     4241:        char full[MAX_PATH], *name;
1.1.1.8   root     4242:        
1.1.1.24  root     4243:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4244:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4245:        }
                   4246:        return(false);
                   4247: }
                   4248: 
1.1.1.45  root     4249: int msdos_is_comm_path(const char *path)
1.1.1.24  root     4250: {
                   4251:        char full[MAX_PATH], *name;
                   4252:        
                   4253:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4254:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4255:                        return(1);
                   4256:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4257:                        return(2);
                   4258:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4259:                        return(3);
                   4260:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4261:                        return(4);
1.1.1.24  root     4262:                }
                   4263:        }
1.1.1.29  root     4264:        return(0);
                   4265: }
                   4266: 
1.1.1.45  root     4267: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37  root     4268: {
                   4269:        // 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     4270:        const char *p = NULL;
1.1.1.37  root     4271:        
                   4272:        if((p = strstr(path, ":")) != NULL) {
                   4273:                UINT8 selector = sio_read(sio_port - 1, 3);
                   4274:                
                   4275:                // baud rate
                   4276:                int baud = max(110, min(9600, atoi(p + 1)));
                   4277:                UINT16 divisor = 115200 / baud;
                   4278:                
                   4279:                if((p = strstr(p + 1, ",")) != NULL) {
                   4280:                        // parity
                   4281:                        if(p[1] == 'N' || p[1] == 'n') {
                   4282:                                selector = (selector & ~0x38) | 0x00;
                   4283:                        } else if(p[1] == 'O' || p[1] == 'o') {
                   4284:                                selector = (selector & ~0x38) | 0x08;
                   4285:                        } else if(p[1] == 'E' || p[1] == 'e') {
                   4286:                                selector = (selector & ~0x38) | 0x18;
                   4287:                        } else if(p[1] == 'M' || p[1] == 'm') {
                   4288:                                selector = (selector & ~0x38) | 0x28;
                   4289:                        } else if(p[1] == 'S' || p[1] == 's') {
                   4290:                                selector = (selector & ~0x38) | 0x38;
                   4291:                        }
                   4292:                        if((p = strstr(p + 1, ",")) != NULL) {
                   4293:                                // word length
                   4294:                                if(p[1] == '8') {
                   4295:                                        selector = (selector & ~0x03) | 0x03;
                   4296:                                } else if(p[1] == '7') {
                   4297:                                        selector = (selector & ~0x03) | 0x02;
                   4298:                                } else if(p[1] == '6') {
                   4299:                                        selector = (selector & ~0x03) | 0x01;
                   4300:                                } else if(p[1] == '5') {
                   4301:                                        selector = (selector & ~0x03) | 0x00;
                   4302:                                }
                   4303:                                if((p = strstr(p + 1, ",")) != NULL) {
                   4304:                                        // stop bits
                   4305:                                        float bits = atof(p + 1);
                   4306:                                        if(bits > 1.0F) {
                   4307:                                                selector |= 0x04;
                   4308:                                        } else {
                   4309:                                                selector &= ~0x04;
                   4310:                                        }
                   4311:                                }
                   4312:                        }
                   4313:                }
                   4314:                sio_write(sio_port - 1, 3, selector | 0x80);
                   4315:                sio_write(sio_port - 1, 0, divisor & 0xff);
                   4316:                sio_write(sio_port - 1, 1, divisor >> 8);
                   4317:                sio_write(sio_port - 1, 3, selector);
                   4318:        }
                   4319: }
                   4320: 
1.1.1.45  root     4321: int msdos_is_prn_path(const char *path)
1.1.1.30  root     4322: {
                   4323:        char full[MAX_PATH], *name;
                   4324:        
                   4325:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4326:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4327:                        return(1);
                   4328:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4329:                        return(1);
                   4330:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4331:                        return(2);
                   4332:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4333:                        return(3);
                   4334:                }
                   4335:        }
                   4336:        return(0);
                   4337: }
                   4338: 
1.1.1.44  root     4339: bool msdos_is_valid_drive(int drv)
                   4340: {
                   4341:        return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
                   4342: }
                   4343: 
                   4344: bool msdos_is_removable_drive(int drv)
                   4345: {
                   4346:        char volume[] = "A:\\";
                   4347:        
                   4348:        volume[0] = 'A' + drv;
                   4349:        
                   4350:        return(GetDriveType(volume) == DRIVE_REMOVABLE);
                   4351: }
                   4352: 
                   4353: bool msdos_is_cdrom_drive(int drv)
                   4354: {
                   4355:        char volume[] = "A:\\";
                   4356:        
                   4357:        volume[0] = 'A' + drv;
                   4358:        
                   4359:        return(GetDriveType(volume) == DRIVE_CDROM);
                   4360: }
                   4361: 
                   4362: bool msdos_is_remote_drive(int drv)
                   4363: {
                   4364:        char volume[] = "A:\\";
                   4365:        
                   4366:        volume[0] = 'A' + drv;
                   4367:        
                   4368:        return(GetDriveType(volume) == DRIVE_REMOTE);
                   4369: }
                   4370: 
                   4371: bool msdos_is_subst_drive(int drv)
                   4372: {
                   4373:        char device[] = "A:", path[MAX_PATH];
                   4374:        
                   4375:        device[0] = 'A' + drv;
                   4376:        
                   4377:        if(QueryDosDevice(device, path, MAX_PATH)) {
                   4378:                if(strncmp(path, "\\??\\", 4) == 0) {
                   4379:                        return(true);
                   4380:                }
                   4381:        }
                   4382:        return(false);
                   4383: }
                   4384: 
1.1.1.45  root     4385: bool msdos_is_existing_file(const char *path)
1.1.1.24  root     4386: {
                   4387:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   4388:        WIN32_FIND_DATA FindData;
                   4389:        HANDLE hFind;
                   4390:        
                   4391:        if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
                   4392:                FindClose(hFind);
                   4393:                return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
                   4394:        }
                   4395:        return(false);
1.1.1.8   root     4396: }
                   4397: 
1.1.1.45  root     4398: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9   root     4399: {
                   4400:        static char tmp[MAX_PATH];
1.1.1.28  root     4401:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4402:        
1.1.1.28  root     4403:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.9   root     4404:        if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
                   4405:                sprintf(file_name, "COMMAND.COM");
                   4406:                if(_access(tmp, 0) == 0) {
                   4407:                        return(tmp);
                   4408:                }
                   4409:        }
1.1.1.28  root     4410:        
                   4411:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.9   root     4412:        if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
                   4413:                sprintf(file_name, "COMMAND.COM");
                   4414:                if(_access(tmp, 0) == 0) {
                   4415:                        return(tmp);
                   4416:                }
                   4417:        }
1.1.1.28  root     4418:        
                   4419:        // check if COMMAND.COM is in the current directory
1.1.1.9   root     4420:        if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
                   4421:                if(_access(tmp, 0) == 0) {
                   4422:                        return(tmp);
                   4423:                }
                   4424:        }
1.1.1.28  root     4425:        
                   4426:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4427:        strcpy(path, env_path);
                   4428:        char *token = my_strtok(path, ";");
1.1.1.9   root     4429:        while(token != NULL) {
1.1.1.14  root     4430:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4431:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4432:                        if(_access(tmp, 0) == 0) {
                   4433:                                return(tmp);
                   4434:                        }
                   4435:                }
                   4436:                token = my_strtok(NULL, ";");
                   4437:        }
                   4438:        return(NULL);
                   4439: }
                   4440: 
1.1.1.14  root     4441: int msdos_drive_number(const char *path)
1.1       root     4442: {
                   4443:        char tmp[MAX_PATH], *name;
                   4444:        
1.1.1.45  root     4445:        if(GetFullPathName(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
                   4446:                if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4447:                        return(tmp[0] - 'a');
                   4448:                } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
                   4449:                        return(tmp[0] - 'A');
                   4450:                }
1.1       root     4451:        }
1.1.1.45  root     4452: //     return(msdos_drive_number("."));
                   4453:        return(_getdrive() - 1);
1.1       root     4454: }
                   4455: 
1.1.1.45  root     4456: const char *msdos_volume_label(const char *path)
1.1       root     4457: {
                   4458:        static char tmp[MAX_PATH];
                   4459:        char volume[] = "A:\\";
                   4460:        
                   4461:        if(path[1] == ':') {
                   4462:                volume[0] = path[0];
                   4463:        } else {
                   4464:                volume[0] = 'A' + _getdrive() - 1;
                   4465:        }
                   4466:        if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
                   4467:                memset(tmp, 0, sizeof(tmp));
                   4468:        }
                   4469:        return(tmp);
                   4470: }
                   4471: 
1.1.1.45  root     4472: const char *msdos_short_volume_label(const char *label)
1.1       root     4473: {
                   4474:        static char tmp[(8 + 1 + 3) + 1];
1.1.1.45  root     4475:        const char *src = label;
1.1       root     4476:        int remain = strlen(label);
                   4477:        char *dst_n = tmp;
                   4478:        char *dst_e = tmp + 9;
                   4479:        
                   4480:        strcpy(tmp, "        .   ");
                   4481:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4482:                if(msdos_lead_byte_check(*src)) {
                   4483:                        if(++i == 8) {
                   4484:                                break;
                   4485:                        }
                   4486:                        *dst_n++ = *src++;
                   4487:                        remain--;
                   4488:                }
                   4489:                *dst_n++ = *src++;
                   4490:                remain--;
                   4491:        }
                   4492:        if(remain > 0) {
                   4493:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4494:                        if(msdos_lead_byte_check(*src)) {
                   4495:                                if(++i == 3) {
                   4496:                                        break;
                   4497:                                }
                   4498:                                *dst_e++ = *src++;
                   4499:                                remain--;
                   4500:                        }
                   4501:                        *dst_e++ = *src++;
                   4502:                        remain--;
                   4503:                }
                   4504:                *dst_e = '\0';
                   4505:        } else {
                   4506:                *dst_n = '\0';
                   4507:        }
                   4508:        my_strupr(tmp);
                   4509:        return(tmp);
                   4510: }
                   4511: 
1.1.1.13  root     4512: errno_t msdos_maperr(unsigned long oserrno)
                   4513: {
                   4514:        _doserrno = oserrno;
1.1.1.14  root     4515:        switch(oserrno) {
1.1.1.13  root     4516:        case ERROR_FILE_NOT_FOUND:         // 2
                   4517:        case ERROR_PATH_NOT_FOUND:         // 3
                   4518:        case ERROR_INVALID_DRIVE:          // 15
                   4519:        case ERROR_NO_MORE_FILES:          // 18
                   4520:        case ERROR_BAD_NETPATH:            // 53
                   4521:        case ERROR_BAD_NET_NAME:           // 67
                   4522:        case ERROR_BAD_PATHNAME:           // 161
                   4523:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4524:                return ENOENT;
                   4525:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4526:                return EMFILE;
                   4527:        case ERROR_ACCESS_DENIED:          // 5
                   4528:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4529:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4530:        case ERROR_CANNOT_MAKE:            // 82
                   4531:        case ERROR_FAIL_I24:               // 83
                   4532:        case ERROR_DRIVE_LOCKED:           // 108
                   4533:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4534:        case ERROR_NOT_LOCKED:             // 158
                   4535:        case ERROR_LOCK_FAILED:            // 167
                   4536:                return EACCES;
                   4537:        case ERROR_INVALID_HANDLE:         // 6
                   4538:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4539:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4540:                return EBADF;
                   4541:        case ERROR_ARENA_TRASHED:          // 7
                   4542:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4543:        case ERROR_INVALID_BLOCK:          // 9
                   4544:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4545:                return ENOMEM;
                   4546:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4547:                return E2BIG;
                   4548:        case ERROR_BAD_FORMAT:             // 11
                   4549:                return ENOEXEC;
                   4550:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4551:                return EXDEV;
                   4552:        case ERROR_FILE_EXISTS:            // 80
                   4553:        case ERROR_ALREADY_EXISTS:         // 183
                   4554:                return EEXIST;
                   4555:        case ERROR_NO_PROC_SLOTS:          // 89
                   4556:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4557:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4558:                return EAGAIN;
                   4559:        case ERROR_BROKEN_PIPE:            // 109
                   4560:                return EPIPE;
                   4561:        case ERROR_DISK_FULL:              // 112
                   4562:                return ENOSPC;
                   4563:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4564:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4565:                return ECHILD;
                   4566:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4567:                return ENOTEMPTY;
                   4568:        }
1.1.1.14  root     4569:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4570:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4571:                return EACCES;
                   4572:        }
1.1.1.14  root     4573:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4574:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4575:                return ENOEXEC;
                   4576:        }
                   4577:        return EINVAL;
                   4578: }
                   4579: 
1.1.1.45  root     4580: int msdos_open(const char *path, int oflag)
1.1.1.13  root     4581: {
1.1.1.14  root     4582:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45  root     4583:                return(_open(path, oflag));
1.1.1.13  root     4584:        }
1.1.1.14  root     4585:        
                   4586:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4587:        DWORD disposition;
1.1.1.14  root     4588:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4589:        default:
1.1.1.13  root     4590:        case _O_EXCL:
                   4591:                disposition = OPEN_EXISTING;
                   4592:                break;
                   4593:        case _O_CREAT:
                   4594:                disposition = OPEN_ALWAYS;
                   4595:                break;
                   4596:        case _O_CREAT | _O_EXCL:
                   4597:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4598:                disposition = CREATE_NEW;
                   4599:                break;
                   4600:        case _O_TRUNC:
                   4601:        case _O_TRUNC | _O_EXCL:
                   4602:                disposition = TRUNCATE_EXISTING;
                   4603:                break;
                   4604:        case _O_CREAT | _O_TRUNC:
                   4605:                disposition = CREATE_ALWAYS;
                   4606:                break;
                   4607:        }
1.1.1.14  root     4608:        
1.1.1.45  root     4609:        HANDLE h = CreateFile(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
1.1.1.13  root     4610:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4611:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4612:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4613:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4614:                // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.45  root     4615:                h = CreateFile(path, GENERIC_READ,
1.1.1.13  root     4616:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4617:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4618:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4619:                        errno = msdos_maperr(GetLastError());
1.1.1.45  root     4620:                        return(-1);
1.1.1.13  root     4621:                }
                   4622:        }
1.1.1.14  root     4623:        
1.1.1.13  root     4624:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     4625:        if(fd == -1) {
1.1.1.13  root     4626:                CloseHandle(h);
                   4627:        }
1.1.1.45  root     4628:        return(fd);
                   4629: }
                   4630: 
                   4631: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
                   4632: {
                   4633:        int fd = -1;
                   4634:        
                   4635:        *sio_port = *lpt_port = 0;
                   4636:        
                   4637:        if(msdos_is_con_path(path)) {
                   4638:                // MODE.COM opens CON device with read/write mode :-(
                   4639:                if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
                   4640:                        oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
                   4641:                        oflag |= _O_RDONLY;
                   4642:                }
                   4643:                if((fd = msdos_open("CON", oflag)) == -1) {
                   4644: //                     fd = msdos_open("NUL", oflag);
                   4645:                }
                   4646:        } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
                   4647:                fd = msdos_open("NUL", oflag);
                   4648:                msdos_set_comm_params(*sio_port, path);
                   4649:        } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
                   4650:                fd = msdos_open("NUL", oflag);
                   4651:        } else if(msdos_is_device_path(path)) {
                   4652:                fd = msdos_open("NUL", oflag);
                   4653: //     } else if(oflag & _O_CREAT) {
                   4654: //             fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
                   4655: //     } else {
                   4656: //             fd = _open(path, oflag);
                   4657:        }
                   4658:        return(fd);
                   4659: }
                   4660: 
                   4661: UINT16 msdos_device_info(const char *path)
                   4662: {
                   4663:        if(msdos_is_con_path(path)) {
                   4664:                return(0x80d3);
                   4665:        } else if(msdos_is_comm_path(path)) {
                   4666:                return(0x80a0);
                   4667:        } else if(msdos_is_prn_path(path)) {
                   4668: //             return(0xa8c0);
                   4669:                return(0x80a0);
                   4670:        } else if(msdos_is_device_path(path)) {
                   4671:                if(strstr(path, "EMMXXXX0") != NULL) {
                   4672:                        return(0xc0c0);
                   4673:                } else if(strstr(path, "MSCD001") != NULL) {
                   4674:                        return(0xc880);
                   4675:                } else {
                   4676:                        return(0x8084);
                   4677:                }
                   4678:        } else {
                   4679:                return(msdos_drive_number(path));
                   4680:        }
1.1.1.13  root     4681: }
                   4682: 
1.1.1.37  root     4683: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg, int sio_port, int lpt_port)
1.1       root     4684: {
                   4685:        static int id = 0;
                   4686:        char full[MAX_PATH], *name;
                   4687:        
                   4688:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4689:                strcpy(file_handler[fd].path, full);
                   4690:        } else {
                   4691:                strcpy(file_handler[fd].path, path);
                   4692:        }
1.1.1.14  root     4693:        // isatty makes no distinction between CON & NUL
                   4694:        // GetFileSize fails on CON, succeeds on NUL
                   4695:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45  root     4696:                if(info == 0x80d3) {
                   4697:                        info = 0x8084;
                   4698:                }
1.1.1.14  root     4699:                atty = 0;
                   4700:        } else if(!atty && info == 0x80d3) {
1.1.1.45  root     4701: //             info = msdos_drive_number(".");
                   4702:                info = msdos_drive_number(path);
1.1.1.14  root     4703:        }
1.1       root     4704:        file_handler[fd].valid = 1;
                   4705:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37  root     4706:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     4707:        file_handler[fd].mode = mode;
                   4708:        file_handler[fd].info = info;
                   4709:        file_handler[fd].psp = psp_seg;
1.1.1.37  root     4710:        file_handler[fd].sio_port = sio_port;
                   4711:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     4712:        
                   4713:        // init system file table
                   4714:        if(fd < 20) {
                   4715:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   4716:                
                   4717:                memset(sft, 0, 0x3b);
                   4718:                
                   4719:                *(UINT16 *)(sft + 0x00) = 1;
                   4720:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
                   4721:                *(UINT8  *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
                   4722:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   4723:                
                   4724:                if(!(file_handler[fd].info & 0x80)) {
                   4725:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   4726:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   4727:                        
                   4728:                        FILETIME time, local;
                   4729:                        HANDLE hHandle;
                   4730:                        WORD dos_date = 0, dos_time = 0;
                   4731:                        DWORD file_size = 0;
                   4732:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   4733:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   4734:                                        FileTimeToLocalFileTime(&time, &local);
                   4735:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   4736:                                }
                   4737:                                file_size = GetFileSize(hHandle, NULL);
                   4738:                        }
                   4739:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   4740:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   4741:                        *(UINT32 *)(sft + 0x11) = file_size;
                   4742:                }
                   4743:                
                   4744:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   4745:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   4746:                my_strupr(fname);
                   4747:                my_strupr(ext);
                   4748:                memset(sft + 0x20, 0x20, 11);
                   4749:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   4750:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   4751:                
                   4752:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   4753:        }
1.1       root     4754: }
                   4755: 
1.1.1.37  root     4756: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg)
                   4757: {
                   4758:         msdos_file_handler_open(fd, path, atty, mode, info, psp_seg, 0, 0);
                   4759: }
                   4760: 
1.1       root     4761: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   4762: {
                   4763:        strcpy(file_handler[dst].path, file_handler[src].path);
                   4764:        file_handler[dst].valid = 1;
                   4765:        file_handler[dst].id = file_handler[src].id;
                   4766:        file_handler[dst].atty = file_handler[src].atty;
                   4767:        file_handler[dst].mode = file_handler[src].mode;
                   4768:        file_handler[dst].info = file_handler[src].info;
                   4769:        file_handler[dst].psp = psp_seg;
1.1.1.37  root     4770:        file_handler[dst].sio_port = file_handler[src].sio_port;
                   4771:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     4772: }
                   4773: 
1.1.1.20  root     4774: void msdos_file_handler_close(int fd)
1.1       root     4775: {
                   4776:        file_handler[fd].valid = 0;
1.1.1.21  root     4777:        
                   4778:        if(fd < 20) {
                   4779:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   4780:        }
1.1       root     4781: }
                   4782: 
1.1.1.14  root     4783: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     4784: {
1.1.1.14  root     4785:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   4786:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   4787:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     4788: }
                   4789: 
                   4790: // find file
                   4791: 
                   4792: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   4793: {
                   4794:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4795:                return(0);      // search directory only !!!
                   4796:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   4797:                return(0);
                   4798:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   4799:                return(0);
                   4800:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4801:                return(0);
                   4802:        } else if((attribute & required_mask) != required_mask) {
                   4803:                return(0);
                   4804:        } else {
                   4805:                return(1);
                   4806:        }
                   4807: }
                   4808: 
1.1.1.13  root     4809: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
                   4810: {
1.1.1.14  root     4811:        if(fd->cAlternateFileName[0]) {
1.1.1.42  root     4812:                return(1);
1.1.1.13  root     4813:        }
                   4814:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     4815:        if(len > 12) {
1.1.1.42  root     4816:                return(0);
1.1.1.13  root     4817:        }
                   4818:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     4819:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42  root     4820:                return(0);
1.1.1.13  root     4821:        }
1.1.1.42  root     4822:        return(1);
1.1.1.13  root     4823: }
                   4824: 
1.1       root     4825: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
                   4826: {
                   4827:        FILETIME local;
                   4828:        
                   4829:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   4830:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   4831:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   4832:        
                   4833:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   4834:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   4835:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   4836:        
                   4837:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   4838:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   4839:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   4840: }
                   4841: 
                   4842: // i/o
                   4843: 
                   4844: void msdos_stdio_reopen()
                   4845: {
                   4846:        if(!file_handler[0].valid) {
                   4847:                _dup2(DUP_STDIN, 0);
                   4848:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   4849:        }
                   4850:        if(!file_handler[1].valid) {
                   4851:                _dup2(DUP_STDOUT, 1);
                   4852:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   4853:        }
                   4854:        if(!file_handler[2].valid) {
                   4855:                _dup2(DUP_STDERR, 2);
                   4856:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   4857:        }
1.1.1.21  root     4858:        if(!file_handler[3].valid) {
                   4859:                _dup2(DUP_STDAUX, 3);
                   4860:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   4861:        }
                   4862:        if(!file_handler[4].valid) {
                   4863:                _dup2(DUP_STDPRN, 4);
1.1.1.45  root     4864: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   4865:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     4866:        }
                   4867:        for(int i = 0; i < 5; i++) {
                   4868:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   4869:                        msdos_psp_set_file_table(i, i, current_psp);
                   4870:                }
                   4871:        }
1.1       root     4872: }
                   4873: 
1.1.1.37  root     4874: int msdos_read(int fd, void *buffer, unsigned int count)
                   4875: {
                   4876:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   4877:                // read from serial port
                   4878:                int read = 0;
                   4879:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   4880:                        UINT8 *buf = (UINT8 *)buffer;
                   4881:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   4882:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     4883:                        DWORD timeout = timeGetTime() + 1000;
                   4884:                        while(read < count) {
                   4885:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
                   4886:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
                   4887:                                        timeout = timeGetTime() + 1000;
                   4888:                                } else {
                   4889:                                        if(timeGetTime() > timeout) {
                   4890:                                                break;
                   4891:                                        }
                   4892:                                        Sleep(10);
1.1.1.37  root     4893:                                }
                   4894:                        }
                   4895:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   4896:                }
                   4897:                return(read);
                   4898:        }
                   4899:        return(_read(fd, buffer, count));
                   4900: }
                   4901: 
1.1       root     4902: int msdos_kbhit()
                   4903: {
                   4904:        msdos_stdio_reopen();
                   4905:        
1.1.1.20  root     4906:        process_t *process = msdos_process_info_get(current_psp);
                   4907:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4908:        
                   4909:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4910:                // stdin is redirected to file
1.1.1.20  root     4911:                return(eof(fd) == 0);
1.1       root     4912:        }
                   4913:        
                   4914:        // check keyboard status
1.1.1.35  root     4915:        if(key_recv != 0) {
1.1       root     4916:                return(1);
                   4917:        }
1.1.1.35  root     4918:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4919: #ifdef USE_SERVICE_THREAD
                   4920:                EnterCriticalSection(&key_buf_crit_sect);
                   4921: #endif
                   4922:                bool empty = key_buf_char->empty();
                   4923: #ifdef USE_SERVICE_THREAD
                   4924:                LeaveCriticalSection(&key_buf_crit_sect);
                   4925: #endif
                   4926:                if(!empty) return(1);
                   4927:        }
                   4928:        return(_kbhit());
1.1       root     4929: }
                   4930: 
                   4931: int msdos_getch_ex(int echo)
                   4932: {
                   4933:        static char prev = 0;
                   4934:        
                   4935:        msdos_stdio_reopen();
                   4936:        
1.1.1.20  root     4937:        process_t *process = msdos_process_info_get(current_psp);
                   4938:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4939:        
                   4940:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4941:                // stdin is redirected to file
                   4942: retry:
                   4943:                char data;
1.1.1.37  root     4944:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     4945:                        char tmp = data;
                   4946:                        if(data == 0x0a) {
                   4947:                                if(prev == 0x0d) {
                   4948:                                        goto retry; // CRLF -> skip LF
                   4949:                                } else {
                   4950:                                        data = 0x0d; // LF only -> CR
                   4951:                                }
                   4952:                        }
                   4953:                        prev = tmp;
                   4954:                        return(data);
                   4955:                }
                   4956:                return(EOF);
                   4957:        }
                   4958:        
                   4959:        // input from console
1.1.1.5   root     4960:        int key_char, key_scan;
1.1.1.33  root     4961:        if(key_recv != 0) {
1.1.1.5   root     4962:                key_char = (key_code >> 0) & 0xff;
                   4963:                key_scan = (key_code >> 8) & 0xff;
                   4964:                key_code >>= 16;
1.1.1.33  root     4965:                key_recv >>= 16;
1.1.1.5   root     4966:        } else {
1.1.1.35  root     4967:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_halted) {
                   4968:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4969: #ifdef USE_SERVICE_THREAD
                   4970:                                EnterCriticalSection(&key_buf_crit_sect);
                   4971: #endif
                   4972:                                bool empty = key_buf_char->empty();
                   4973: #ifdef USE_SERVICE_THREAD
                   4974:                                LeaveCriticalSection(&key_buf_crit_sect);
                   4975: #endif
                   4976:                                if(!empty) break;
                   4977:                        }
1.1.1.23  root     4978:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   4979:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   4980:                                if(_kbhit()) {
1.1.1.32  root     4981:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     4982: #ifdef USE_SERVICE_THREAD
                   4983:                                                EnterCriticalSection(&key_buf_crit_sect);
                   4984: #endif
1.1.1.32  root     4985:                                                key_buf_char->write(_getch());
1.1.1.35  root     4986:                                                key_buf_scan->write(0x00);
                   4987: #ifdef USE_SERVICE_THREAD
                   4988:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   4989: #endif
1.1.1.32  root     4990:                                        }
1.1.1.23  root     4991:                                } else {
                   4992:                                        Sleep(10);
                   4993:                                }
                   4994:                        } else {
                   4995:                                if(!update_key_buffer()) {
                   4996:                                        Sleep(10);
                   4997:                                }
1.1.1.14  root     4998:                        }
                   4999:                }
                   5000:                if(m_halted) {
1.1.1.33  root     5001:                        // insert CR to terminate input loops
1.1.1.14  root     5002:                        key_char = 0x0d;
                   5003:                        key_scan = 0;
1.1.1.32  root     5004:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5005: #ifdef USE_SERVICE_THREAD
                   5006:                        EnterCriticalSection(&key_buf_crit_sect);
                   5007: #endif
1.1.1.14  root     5008:                        key_char = key_buf_char->read();
                   5009:                        key_scan = key_buf_scan->read();
1.1.1.41  root     5010:                        // write to bottom of key buffer
                   5011:                        mem[0x43c] = (UINT8)key_char;
                   5012:                        mem[0x43d] = (UINT8)key_scan;
1.1.1.35  root     5013: #ifdef USE_SERVICE_THREAD
                   5014:                        LeaveCriticalSection(&key_buf_crit_sect);
                   5015: #endif
1.1.1.5   root     5016:                }
1.1       root     5017:        }
                   5018:        if(echo && key_char) {
                   5019:                msdos_putch(key_char);
                   5020:        }
                   5021:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   5022: }
                   5023: 
                   5024: inline int msdos_getch()
                   5025: {
                   5026:        return(msdos_getch_ex(0));
                   5027: }
                   5028: 
                   5029: inline int msdos_getche()
                   5030: {
                   5031:        return(msdos_getch_ex(1));
                   5032: }
                   5033: 
                   5034: int msdos_write(int fd, const void *buffer, unsigned int count)
                   5035: {
1.1.1.37  root     5036:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5037:                // write to serial port
1.1.1.38  root     5038:                int written = 0;
1.1.1.37  root     5039:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5040:                        UINT8 *buf = (UINT8 *)buffer;
                   5041:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5042:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5043:                        DWORD timeout = timeGetTime() + 1000;
                   5044:                        while(written < count) {
                   5045:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
                   5046:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
                   5047:                                        timeout = timeGetTime() + 1000;
                   5048:                                } else {
                   5049:                                        if(timeGetTime() > timeout) {
                   5050:                                                break;
                   5051:                                        }
                   5052:                                        Sleep(10);
                   5053:                                }
1.1.1.37  root     5054:                        }
                   5055:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5056:                }
1.1.1.38  root     5057:                return(written);
1.1.1.37  root     5058:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   5059:                // write to printer port
                   5060:                UINT8 *buf = (UINT8 *)buffer;
                   5061:                for(unsigned int i = 0; i < count; i++) {
                   5062: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5063:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5064:                }
                   5065:                return(count);
                   5066:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     5067:                // CR+LF -> LF
1.1.1.37  root     5068:                static int is_cr = 0;
1.1       root     5069:                UINT8 *buf = (UINT8 *)buffer;
                   5070:                for(unsigned int i = 0; i < count; i++) {
                   5071:                        UINT8 data = buf[i];
                   5072:                        if(is_cr) {
                   5073:                                if(data != 0x0a) {
                   5074:                                        UINT8 tmp = 0x0d;
                   5075:                                        _write(1, &tmp, 1);
                   5076:                                }
                   5077:                                _write(1, &data, 1);
                   5078:                                is_cr = 0;
                   5079:                        } else if(data == 0x0d) {
                   5080:                                is_cr = 1;
                   5081:                        } else {
                   5082:                                _write(1, &data, 1);
                   5083:                        }
                   5084:                }
                   5085:                return(count);
                   5086:        }
1.1.1.14  root     5087:        vram_flush();
1.1       root     5088:        return(_write(fd, buffer, count));
                   5089: }
                   5090: 
                   5091: void msdos_putch(UINT8 data)
1.1.1.50! root     5092: {
        !          5093:        static bool reenter = false;
        !          5094:        
        !          5095:        msdos_stdio_reopen();
        !          5096:        
        !          5097:        process_t *process = msdos_process_info_get(current_psp);
        !          5098:        int fd = msdos_psp_get_file_table(1, current_psp);
        !          5099:        
        !          5100:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
        !          5101:                // stdout is redirected to file
        !          5102:                msdos_write(fd, &data, 1);
        !          5103:                return;
        !          5104:        }
        !          5105:        
        !          5106:        // call int 29h ?
        !          5107:        if(*(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
        !          5108:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
        !          5109:                // int 29h is not hooked, no need to call int 29h
        !          5110:                msdos_putch_fast(data);
        !          5111: #ifdef USE_SERVICE_THREAD
        !          5112:        } else if(in_service && main_thread_id != GetCurrentThreadId()) {
        !          5113:                // XXX: in usually we should not reach here
        !          5114:                // this is called from service thread to echo the input
        !          5115:                // we can not call int 29h because it causes a critial issue to control cpu running in main thread :-(
        !          5116:                msdos_putch_fast(data);
        !          5117: #endif
        !          5118:        } else if(reenter) {
        !          5119:                // disallow reentering call int 29h routine to prevent an infinite loop :-(
        !          5120:                msdos_putch_fast(data);
        !          5121:        } else {
        !          5122:                // this is called from main thread, so we can call int 29h :-)
        !          5123:                reenter = true;
        !          5124:                try {
        !          5125:                        UINT32 tmp_pc = m_pc;
        !          5126:                        UINT16 tmp_ax = REG16(AX);
        !          5127:                        UINT32 tmp_bx = REG16(BX); // BX may be destroyed by some versions of DOS 3.3
        !          5128:                        
        !          5129:                        // call int 29h routine is at fffc:0027
        !          5130:                        i386_call_far(DUMMY_TOP >> 4, 0x0027);
        !          5131:                        REG8(AL) = data;
        !          5132:                        
        !          5133:                        // run cpu until call int 29h routine is done
        !          5134:                        while(!m_halted && tmp_pc != m_pc) {
        !          5135:                                try {
        !          5136:                                        hardware_run_cpu();
        !          5137:                                } catch(...) {
        !          5138:                                }
        !          5139:                        }
        !          5140:                        REG16(AX) = tmp_ax;
        !          5141:                        REG16(BX) = tmp_bx;
        !          5142:                } catch(...) {
        !          5143:                }
        !          5144:                reenter = false;
        !          5145:        }
        !          5146: }
        !          5147: 
        !          5148: void msdos_putch_fast(UINT8 data)
1.1.1.35  root     5149: #ifdef USE_SERVICE_THREAD
                   5150: {
                   5151:        EnterCriticalSection(&putch_crit_sect);
                   5152:        msdos_putch_tmp(data);
                   5153:        LeaveCriticalSection(&putch_crit_sect);
                   5154: }
                   5155: void msdos_putch_tmp(UINT8 data)
                   5156: #endif
1.1       root     5157: {
1.1.1.34  root     5158:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5159:        SMALL_RECT rect;
                   5160:        COORD co;
1.1       root     5161:        static int p = 0;
                   5162:        static int is_kanji = 0;
                   5163:        static int is_esc = 0;
                   5164:        static int stored_x;
                   5165:        static int stored_y;
                   5166:        static WORD stored_a;
1.1.1.20  root     5167:        static char tmp[64], out[64];
1.1       root     5168:        
1.1.1.23  root     5169:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     5170:        
                   5171:        // output to console
                   5172:        tmp[p++] = data;
                   5173:        
1.1.1.14  root     5174:        vram_flush();
                   5175:        
1.1       root     5176:        if(is_kanji) {
                   5177:                // kanji character
                   5178:                is_kanji = 0;
                   5179:        } else if(is_esc) {
                   5180:                // escape sequense
                   5181:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   5182:                        p = is_esc = 0;
                   5183:                } else if(tmp[1] == '=' && p == 4) {
                   5184:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     5185:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     5186:                        SetConsoleCursorPosition(hStdout, co);
                   5187:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5188:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     5189:                        cursor_moved = false;
                   5190:                        p = is_esc = 0;
                   5191:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
                   5192:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5193:                        co.X = csbi.dwCursorPosition.X;
                   5194:                        co.Y = csbi.dwCursorPosition.Y;
                   5195:                        WORD wAttributes = csbi.wAttributes;
                   5196:                        
                   5197:                        if(tmp[1] == 'D') {
                   5198:                                co.Y++;
                   5199:                        } else if(tmp[1] == 'E') {
                   5200:                                co.X = 0;
                   5201:                                co.Y++;
                   5202:                        } else if(tmp[1] == 'M') {
                   5203:                                co.Y--;
                   5204:                        } else if(tmp[1] == '*') {
1.1.1.14  root     5205:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5206:                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5207:                                co.X = 0;
                   5208:                                co.Y = csbi.srWindow.Top;
1.1       root     5209:                        } else if(tmp[1] == '[') {
                   5210:                                int param[256], params = 0;
                   5211:                                memset(param, 0, sizeof(param));
                   5212:                                for(int i = 2; i < p; i++) {
                   5213:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   5214:                                                param[params] *= 10;
                   5215:                                                param[params] += tmp[i] - '0';
                   5216:                                        } else {
                   5217:                                                params++;
                   5218:                                        }
                   5219:                                }
                   5220:                                if(data == 'A') {
1.1.1.14  root     5221:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     5222:                                } else if(data == 'B') {
1.1.1.14  root     5223:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     5224:                                } else if(data == 'C') {
1.1.1.14  root     5225:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     5226:                                } else if(data == 'D') {
1.1.1.14  root     5227:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     5228:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     5229:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   5230:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     5231:                                } else if(data == 'J') {
1.1.1.14  root     5232:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5233:                                        if(param[0] == 0) {
                   5234:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5235:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5236:                                                if(co.Y < csbi.srWindow.Bottom) {
                   5237:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5238:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5239:                                                }
                   5240:                                        } else if(param[0] == 1) {
1.1.1.14  root     5241:                                                if(co.Y > csbi.srWindow.Top) {
                   5242:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
                   5243:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5244:                                                }
                   5245:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5246:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5247:                                        } else if(param[0] == 2) {
1.1.1.14  root     5248:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5249:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5250:                                                co.X = co.Y = 0;
                   5251:                                        }
                   5252:                                } else if(data == 'K') {
1.1.1.14  root     5253:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5254:                                        if(param[0] == 0) {
                   5255:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5256:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5257:                                        } else if(param[0] == 1) {
                   5258:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5259:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5260:                                        } else if(param[0] == 2) {
                   5261:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5262:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5263:                                        }
                   5264:                                } else if(data == 'L') {
1.1.1.14  root     5265:                                        if(params == 0) {
                   5266:                                                param[0] = 1;
1.1       root     5267:                                        }
1.1.1.14  root     5268:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5269:                                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5270:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5271:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5272:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5273:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14  root     5274:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5275:                                        co.X = 0;
                   5276:                                } else if(data == 'M') {
1.1.1.14  root     5277:                                        if(params == 0) {
                   5278:                                                param[0] = 1;
                   5279:                                        }
                   5280:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5281:                                                clear_scr_buffer(csbi.wAttributes);
                   5282:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5283:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5284:                                        } else {
1.1.1.14  root     5285:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5286:                                                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5287:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5288:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5289:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5290:                                        }
                   5291:                                        co.X = 0;
                   5292:                                } else if(data == 'h') {
                   5293:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5294:                                                CONSOLE_CURSOR_INFO cur;
                   5295:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5296:                                                if(cur.bVisible) {
                   5297:                                                        cur.bVisible = FALSE;
1.1.1.14  root     5298: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5299:                                                }
                   5300:                                        }
                   5301:                                } else if(data == 'l') {
                   5302:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5303:                                                CONSOLE_CURSOR_INFO cur;
                   5304:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5305:                                                if(!cur.bVisible) {
                   5306:                                                        cur.bVisible = TRUE;
1.1.1.14  root     5307: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5308:                                                }
                   5309:                                        }
                   5310:                                } else if(data == 'm') {
                   5311:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5312:                                        int reverse = 0, hidden = 0;
                   5313:                                        for(int i = 0; i < params; i++) {
                   5314:                                                if(param[i] == 1) {
                   5315:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5316:                                                } else if(param[i] == 4) {
                   5317:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5318:                                                } else if(param[i] == 7) {
                   5319:                                                        reverse = 1;
                   5320:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5321:                                                        hidden = 1;
                   5322:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5323:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5324:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5325:                                                                param[i] -= 16;
                   5326:                                                        } else {
                   5327:                                                                param[i] -= 30;
                   5328:                                                        }
                   5329:                                                        if(param[i] & 1) {
                   5330:                                                                wAttributes |= FOREGROUND_RED;
                   5331:                                                        }
                   5332:                                                        if(param[i] & 2) {
                   5333:                                                                wAttributes |= FOREGROUND_GREEN;
                   5334:                                                        }
                   5335:                                                        if(param[i] & 4) {
                   5336:                                                                wAttributes |= FOREGROUND_BLUE;
                   5337:                                                        }
                   5338:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5339:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5340:                                                        if((param[i] - 40) & 1) {
                   5341:                                                                wAttributes |= BACKGROUND_RED;
                   5342:                                                        }
                   5343:                                                        if((param[i] - 40) & 2) {
                   5344:                                                                wAttributes |= BACKGROUND_GREEN;
                   5345:                                                        }
                   5346:                                                        if((param[i] - 40) & 4) {
                   5347:                                                                wAttributes |= BACKGROUND_BLUE;
                   5348:                                                        }
                   5349:                                                }
                   5350:                                        }
                   5351:                                        if(reverse) {
                   5352:                                                wAttributes &= ~0xff;
                   5353:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5354:                                        }
                   5355:                                        if(hidden) {
                   5356:                                                wAttributes &= ~0x0f;
                   5357:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5358:                                        }
                   5359:                                } else if(data == 'n') {
                   5360:                                        if(param[0] == 6) {
                   5361:                                                char tmp[16];
                   5362:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5363:                                                int len = strlen(tmp);
1.1.1.32  root     5364:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5365: #ifdef USE_SERVICE_THREAD
                   5366:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5367: #endif
1.1.1.32  root     5368:                                                        for(int i = 0; i < len; i++) {
                   5369:                                                                key_buf_char->write(tmp[i]);
                   5370:                                                                key_buf_scan->write(0x00);
                   5371:                                                        }
1.1.1.35  root     5372: #ifdef USE_SERVICE_THREAD
                   5373:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5374: #endif
1.1       root     5375:                                                }
                   5376:                                        }
                   5377:                                } else if(data == 's') {
                   5378:                                        stored_x = co.X;
                   5379:                                        stored_y = co.Y;
                   5380:                                        stored_a = wAttributes;
                   5381:                                } else if(data == 'u') {
                   5382:                                        co.X = stored_x;
                   5383:                                        co.Y = stored_y;
                   5384:                                        wAttributes = stored_a;
                   5385:                                }
                   5386:                        }
                   5387:                        if(co.X < 0) {
                   5388:                                co.X = 0;
                   5389:                        } else if(co.X >= csbi.dwSize.X) {
                   5390:                                co.X = csbi.dwSize.X - 1;
                   5391:                        }
1.1.1.14  root     5392:                        if(co.Y < csbi.srWindow.Top) {
                   5393:                                co.Y = csbi.srWindow.Top;
                   5394:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5395:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5396:                        }
                   5397:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5398:                                SetConsoleCursorPosition(hStdout, co);
                   5399:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5400:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5401:                                cursor_moved = false;
                   5402:                        }
                   5403:                        if(wAttributes != csbi.wAttributes) {
                   5404:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5405:                        }
                   5406:                        p = is_esc = 0;
                   5407:                }
                   5408:                return;
                   5409:        } else {
                   5410:                if(msdos_lead_byte_check(data)) {
                   5411:                        is_kanji = 1;
                   5412:                        return;
                   5413:                } else if(data == 0x1b) {
                   5414:                        is_esc = 1;
                   5415:                        return;
                   5416:                }
                   5417:        }
1.1.1.20  root     5418:        
                   5419:        DWORD q = 0, num;
                   5420:        is_kanji = 0;
                   5421:        for(int i = 0; i < p; i++) {
                   5422:                UINT8 c = tmp[i];
                   5423:                if(is_kanji) {
                   5424:                        is_kanji = 0;
                   5425:                } else if(msdos_lead_byte_check(data)) {
                   5426:                        is_kanji = 1;
                   5427:                } else if(msdos_ctrl_code_check(data)) {
                   5428:                        out[q++] = '^';
                   5429:                        c += 'A' - 1;
                   5430:                }
                   5431:                out[q++] = c;
                   5432:        }
1.1.1.34  root     5433:        if(q == 1 && out[0] == 0x08) {
                   5434:                // back space
                   5435:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5436:                if(csbi.dwCursorPosition.X > 0) {
                   5437:                        co.X = csbi.dwCursorPosition.X - 1;
                   5438:                        co.Y = csbi.dwCursorPosition.Y;
                   5439:                        SetConsoleCursorPosition(hStdout, co);
                   5440:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5441:                        co.X = csbi.dwSize.X - 1;
                   5442:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5443:                        SetConsoleCursorPosition(hStdout, co);
                   5444:                } else {
                   5445:                        WriteConsole(hStdout, out, q, &num, NULL); // to make sure
                   5446:                }
                   5447:        } else {
                   5448:                WriteConsole(hStdout, out, q, &num, NULL);
                   5449:        }
1.1       root     5450:        p = 0;
1.1.1.14  root     5451:        
1.1.1.15  root     5452:        if(!restore_console_on_exit) {
                   5453:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5454:                scr_top = csbi.srWindow.Top;
                   5455:        }
1.1       root     5456:        cursor_moved = true;
                   5457: }
                   5458: 
                   5459: int msdos_aux_in()
                   5460: {
1.1.1.21  root     5461:        msdos_stdio_reopen();
                   5462:        
1.1.1.20  root     5463:        process_t *process = msdos_process_info_get(current_psp);
                   5464:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5465:        
                   5466:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5467:                char data = 0;
1.1.1.37  root     5468:                msdos_read(fd, &data, 1);
1.1       root     5469:                return(data);
                   5470:        } else {
                   5471:                return(EOF);
                   5472:        }
                   5473: }
                   5474: 
                   5475: void msdos_aux_out(char data)
                   5476: {
1.1.1.21  root     5477:        msdos_stdio_reopen();
                   5478:        
1.1.1.20  root     5479:        process_t *process = msdos_process_info_get(current_psp);
                   5480:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5481:        
                   5482:        if(fd < process->max_files && file_handler[fd].valid) {
                   5483:                msdos_write(fd, &data, 1);
1.1       root     5484:        }
                   5485: }
                   5486: 
                   5487: void msdos_prn_out(char data)
                   5488: {
1.1.1.21  root     5489:        msdos_stdio_reopen();
                   5490:        
1.1.1.20  root     5491:        process_t *process = msdos_process_info_get(current_psp);
                   5492:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5493:        
                   5494:        if(fd < process->max_files && file_handler[fd].valid) {
                   5495:                msdos_write(fd, &data, 1);
1.1       root     5496:        }
                   5497: }
                   5498: 
                   5499: // memory control
                   5500: 
1.1.1.45  root     5501: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name)
1.1       root     5502: {
                   5503:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5504:        
                   5505:        mcb->mz = mz;
                   5506:        mcb->psp = psp;
1.1.1.30  root     5507:        mcb->paragraphs = paragraphs;
1.1.1.39  root     5508:        
                   5509:        if(prog_name != NULL) {
                   5510:                memset(mcb->prog_name, 0, 8);
                   5511:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
                   5512:        }
1.1       root     5513:        return(mcb);
                   5514: }
                   5515: 
1.1.1.39  root     5516: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs)
                   5517: {
                   5518:        return(msdos_mcb_create(mcb_seg, mz, psp, paragraphs, NULL));
                   5519: }
                   5520: 
1.1       root     5521: void msdos_mcb_check(mcb_t *mcb)
                   5522: {
                   5523:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5524:                #if 0
                   5525:                        // shutdown now !!!
                   5526:                        fatalerror("broken memory control block\n");
                   5527:                #else
                   5528:                        // return error code and continue
                   5529:                        throw(0x07); // broken memory control block
                   5530:                #endif
1.1       root     5531:        }
                   5532: }
                   5533: 
1.1.1.39  root     5534: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5535: {
                   5536:        int mcb_seg = seg - 1;
                   5537:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5538:        msdos_mcb_check(mcb);
                   5539:        
1.1.1.30  root     5540:        if(mcb->paragraphs > paragraphs) {
1.1       root     5541:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5542:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5543:                
                   5544:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5545:                mcb->mz = 'M';
1.1.1.30  root     5546:                mcb->paragraphs = paragraphs;
1.1       root     5547:        }
                   5548: }
                   5549: 
                   5550: void msdos_mem_merge(int seg)
                   5551: {
                   5552:        int mcb_seg = seg - 1;
                   5553:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5554:        msdos_mcb_check(mcb);
                   5555:        
                   5556:        while(1) {
                   5557:                if(mcb->mz == 'Z') {
                   5558:                        break;
                   5559:                }
1.1.1.30  root     5560:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5561:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5562:                msdos_mcb_check(next_mcb);
                   5563:                
                   5564:                if(next_mcb->psp != 0) {
                   5565:                        break;
                   5566:                }
                   5567:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5568:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5569:        }
                   5570: }
                   5571: 
1.1.1.8   root     5572: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5573: {
                   5574:        while(1) {
                   5575:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5576:                bool last_block;
1.1       root     5577:                
1.1.1.14  root     5578:                if(mcb->psp == 0) {
                   5579:                        msdos_mem_merge(mcb_seg + 1);
                   5580:                } else {
                   5581:                        msdos_mcb_check(mcb);
                   5582:                }
1.1.1.33  root     5583:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5584:                        // check if the next is dummy mcb to link to umb
                   5585:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5586:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5587:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5588:                }
                   5589:                if(!(new_process && !last_block)) {
1.1.1.30  root     5590:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5591:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5592:                                mcb->psp = current_psp;
                   5593:                                return(mcb_seg + 1);
                   5594:                        }
                   5595:                }
                   5596:                if(mcb->mz == 'Z') {
                   5597:                        break;
                   5598:                }
1.1.1.30  root     5599:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5600:        }
                   5601:        return(-1);
                   5602: }
                   5603: 
                   5604: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5605: {
                   5606:        int mcb_seg = seg - 1;
                   5607:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5608:        msdos_mcb_check(mcb);
1.1.1.30  root     5609:        int current_paragraphs = mcb->paragraphs;
1.1       root     5610:        
                   5611:        msdos_mem_merge(seg);
1.1.1.30  root     5612:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5613:                if(max_paragraphs) {
1.1.1.30  root     5614:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5615:                }
1.1       root     5616:                msdos_mem_split(seg, current_paragraphs);
                   5617:                return(-1);
                   5618:        }
                   5619:        msdos_mem_split(seg, paragraphs);
                   5620:        return(0);
                   5621: }
                   5622: 
                   5623: void msdos_mem_free(int seg)
                   5624: {
                   5625:        int mcb_seg = seg - 1;
                   5626:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5627:        msdos_mcb_check(mcb);
                   5628:        
                   5629:        mcb->psp = 0;
                   5630:        msdos_mem_merge(seg);
                   5631: }
                   5632: 
1.1.1.8   root     5633: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5634: {
                   5635:        int max_paragraphs = 0;
                   5636:        
                   5637:        while(1) {
                   5638:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5639:                bool last_block;
                   5640:                
1.1       root     5641:                msdos_mcb_check(mcb);
                   5642:                
1.1.1.33  root     5643:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5644:                        // check if the next is dummy mcb to link to umb
                   5645:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5646:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5647:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5648:                }
                   5649:                if(!(new_process && !last_block)) {
1.1.1.30  root     5650:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   5651:                                max_paragraphs = mcb->paragraphs;
1.1       root     5652:                        }
                   5653:                }
                   5654:                if(mcb->mz == 'Z') {
                   5655:                        break;
                   5656:                }
1.1.1.30  root     5657:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5658:        }
1.1.1.14  root     5659:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     5660: }
                   5661: 
1.1.1.8   root     5662: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   5663: {
                   5664:        int last_seg = -1;
                   5665:        
                   5666:        while(1) {
                   5667:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5668:                msdos_mcb_check(mcb);
                   5669:                
1.1.1.14  root     5670:                if(mcb->psp == psp) {
1.1.1.8   root     5671:                        last_seg = mcb_seg;
                   5672:                }
1.1.1.14  root     5673:                if(mcb->mz == 'Z') {
                   5674:                        break;
                   5675:                }
1.1.1.30  root     5676:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     5677:        }
                   5678:        return(last_seg);
                   5679: }
                   5680: 
1.1.1.19  root     5681: int msdos_mem_get_umb_linked()
                   5682: {
1.1.1.33  root     5683:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5684:        msdos_mcb_check(mcb);
1.1.1.19  root     5685:        
1.1.1.33  root     5686:        if(mcb->mz == 'M') {
                   5687:                return(-1);
1.1.1.19  root     5688:        }
                   5689:        return(0);
                   5690: }
                   5691: 
1.1.1.33  root     5692: void msdos_mem_link_umb()
1.1.1.19  root     5693: {
1.1.1.33  root     5694:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5695:        msdos_mcb_check(mcb);
1.1.1.19  root     5696:        
1.1.1.33  root     5697:        mcb->mz = 'M';
                   5698:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39  root     5699:        
                   5700:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     5701: }
                   5702: 
1.1.1.33  root     5703: void msdos_mem_unlink_umb()
1.1.1.19  root     5704: {
1.1.1.33  root     5705:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5706:        msdos_mcb_check(mcb);
1.1.1.19  root     5707:        
1.1.1.33  root     5708:        mcb->mz = 'Z';
                   5709:        mcb->paragraphs = 0;
1.1.1.39  root     5710:        
                   5711:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     5712: }
                   5713: 
1.1.1.29  root     5714: #ifdef SUPPORT_HMA
                   5715: 
                   5716: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   5717: {
                   5718:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5719:        
                   5720:        mcb->ms[0] = 'M';
                   5721:        mcb->ms[1] = 'S';
                   5722:        mcb->owner = owner;
                   5723:        mcb->size = size;
                   5724:        mcb->next = next;
                   5725:        return(mcb);
                   5726: }
                   5727: 
                   5728: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   5729: {
                   5730:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   5731: }
                   5732: 
                   5733: int msdos_hma_mem_split(int offset, int size)
                   5734: {
                   5735:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5736:        
                   5737:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5738:                return(-1);
                   5739:        }
                   5740:        if(mcb->size >= size + 0x10) {
                   5741:                int new_offset = offset + 0x10 + size;
                   5742:                int new_size = mcb->size - 0x10 - size;
                   5743:                
                   5744:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   5745:                mcb->size = size;
                   5746:                mcb->next = new_offset;
                   5747:                return(0);
                   5748:        }
                   5749:        return(-1);
                   5750: }
                   5751: 
                   5752: void msdos_hma_mem_merge(int offset)
                   5753: {
                   5754:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5755:        
                   5756:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5757:                return;
                   5758:        }
                   5759:        while(1) {
                   5760:                if(mcb->next == 0) {
                   5761:                        break;
                   5762:                }
                   5763:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   5764:                
                   5765:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   5766:                        return;
                   5767:                }
                   5768:                if(next_mcb->owner != 0) {
                   5769:                        break;
                   5770:                }
                   5771:                mcb->size += 0x10 + next_mcb->size;
                   5772:                mcb->next = next_mcb->next;
                   5773:        }
                   5774: }
                   5775: 
                   5776: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   5777: {
                   5778:        int offset = 0x10; // first mcb in HMA
                   5779:        
                   5780:        while(1) {
                   5781:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5782:                
                   5783:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5784:                        return(-1);
                   5785:                }
                   5786:                if(mcb->owner == 0) {
                   5787:                        msdos_hma_mem_merge(offset);
                   5788:                }
                   5789:                if(mcb->owner == 0 && mcb->size >= size) {
                   5790:                        msdos_hma_mem_split(offset, size);
                   5791:                        mcb->owner = owner;
                   5792:                        return(offset);
                   5793:                }
                   5794:                if(mcb->next == 0) {
                   5795:                        break;
                   5796:                }
                   5797:                offset = mcb->next;
                   5798:        }
                   5799:        return(-1);
                   5800: }
                   5801: 
                   5802: int msdos_hma_mem_realloc(int offset, int size)
                   5803: {
                   5804:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5805:        
                   5806:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5807:                return(-1);
                   5808:        }
                   5809:        if(mcb->size < size) {
                   5810:                return(-1);
                   5811:        }
                   5812:        msdos_hma_mem_split(offset, size);
                   5813:        return(0);
                   5814: }
                   5815: 
                   5816: void msdos_hma_mem_free(int offset)
                   5817: {
                   5818:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5819:        
                   5820:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5821:                return;
                   5822:        }
                   5823:        mcb->owner = 0;
                   5824:        msdos_hma_mem_merge(offset);
                   5825: }
                   5826: 
                   5827: int msdos_hma_mem_get_free(int *available_offset)
                   5828: {
                   5829:        int offset = 0x10; // first mcb in HMA
                   5830:        int size = 0;
                   5831:        
                   5832:        while(1) {
                   5833:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5834:                
                   5835:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5836:                        return(0);
                   5837:                }
                   5838:                if(mcb->owner == 0 && size < mcb->size) {
                   5839:                        if(available_offset != NULL) {
                   5840:                                *available_offset = offset;
                   5841:                        }
                   5842:                        size = mcb->size;
                   5843:                }
                   5844:                if(mcb->next == 0) {
                   5845:                        break;
                   5846:                }
                   5847:                offset = mcb->next;
                   5848:        }
                   5849:        return(size);
                   5850: }
                   5851: 
                   5852: #endif
                   5853: 
1.1       root     5854: // environment
                   5855: 
1.1.1.45  root     5856: void msdos_env_set_argv(int env_seg, const char *argv)
1.1       root     5857: {
                   5858:        char *dst = (char *)(mem + (env_seg << 4));
                   5859:        
                   5860:        while(1) {
                   5861:                if(dst[0] == 0) {
                   5862:                        break;
                   5863:                }
                   5864:                dst += strlen(dst) + 1;
                   5865:        }
                   5866:        *dst++ = 0; // end of environment
                   5867:        *dst++ = 1; // top of argv[0]
                   5868:        *dst++ = 0;
                   5869:        memcpy(dst, argv, strlen(argv));
                   5870:        dst += strlen(argv);
                   5871:        *dst++ = 0;
                   5872:        *dst++ = 0;
                   5873: }
                   5874: 
1.1.1.45  root     5875: const char *msdos_env_get_argv(int env_seg)
1.1       root     5876: {
                   5877:        static char env[ENV_SIZE];
                   5878:        char *src = env;
                   5879:        
                   5880:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5881:        while(1) {
                   5882:                if(src[0] == 0) {
                   5883:                        if(src[1] == 1) {
                   5884:                                return(src + 3);
                   5885:                        }
                   5886:                        break;
                   5887:                }
                   5888:                src += strlen(src) + 1;
                   5889:        }
                   5890:        return(NULL);
                   5891: }
                   5892: 
1.1.1.45  root     5893: const char *msdos_env_get(int env_seg, const char *name)
1.1       root     5894: {
                   5895:        static char env[ENV_SIZE];
                   5896:        char *src = env;
                   5897:        
                   5898:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5899:        while(1) {
                   5900:                if(src[0] == 0) {
                   5901:                        break;
                   5902:                }
                   5903:                int len = strlen(src);
                   5904:                char *n = my_strtok(src, "=");
                   5905:                char *v = src + strlen(n) + 1;
                   5906:                
                   5907:                if(_stricmp(name, n) == 0) {
                   5908:                        return(v);
                   5909:                }
                   5910:                src += len + 1;
                   5911:        }
                   5912:        return(NULL);
                   5913: }
                   5914: 
1.1.1.45  root     5915: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1       root     5916: {
                   5917:        char env[ENV_SIZE];
                   5918:        char *src = env;
                   5919:        char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45  root     5920:        const char *argv = msdos_env_get_argv(env_seg);
1.1       root     5921:        int done = 0;
                   5922:        
                   5923:        memcpy(src, dst, ENV_SIZE);
                   5924:        memset(dst, 0, ENV_SIZE);
                   5925:        while(1) {
                   5926:                if(src[0] == 0) {
                   5927:                        break;
                   5928:                }
                   5929:                int len = strlen(src);
                   5930:                char *n = my_strtok(src, "=");
                   5931:                char *v = src + strlen(n) + 1;
                   5932:                char tmp[1024];
                   5933:                
                   5934:                if(_stricmp(name, n) == 0) {
                   5935:                        sprintf(tmp, "%s=%s", n, value);
                   5936:                        done = 1;
                   5937:                } else {
                   5938:                        sprintf(tmp, "%s=%s", n, v);
                   5939:                }
                   5940:                memcpy(dst, tmp, strlen(tmp));
                   5941:                dst += strlen(tmp) + 1;
                   5942:                src += len + 1;
                   5943:        }
                   5944:        if(!done) {
                   5945:                char tmp[1024];
                   5946:                
                   5947:                sprintf(tmp, "%s=%s", name, value);
                   5948:                memcpy(dst, tmp, strlen(tmp));
                   5949:                dst += strlen(tmp) + 1;
                   5950:        }
                   5951:        if(argv) {
                   5952:                *dst++ = 0; // end of environment
                   5953:                *dst++ = 1; // top of argv[0]
                   5954:                *dst++ = 0;
                   5955:                memcpy(dst, argv, strlen(argv));
                   5956:                dst += strlen(argv);
                   5957:                *dst++ = 0;
                   5958:                *dst++ = 0;
                   5959:        }
                   5960: }
                   5961: 
                   5962: // process
                   5963: 
1.1.1.8   root     5964: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     5965: {
                   5966:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5967:        
                   5968:        memset(psp, 0, PSP_SIZE);
                   5969:        psp->exit[0] = 0xcd;
                   5970:        psp->exit[1] = 0x20;
1.1.1.8   root     5971:        psp->first_mcb = mcb_seg;
1.1.1.46  root     5972: #if 1
1.1.1.49  root     5973:        psp->call5[0] = 0xcd;   // int 30h
                   5974:        psp->call5[1] = 0x30;
1.1.1.46  root     5975:        psp->call5[2] = 0xc3;   // ret
                   5976: #else
                   5977:        psp->call5[0] = 0x8a;   // mov ah, cl
                   5978:        psp->call5[1] = 0xe1;
                   5979:        psp->call5[2] = 0xcd;   // int 21h
                   5980:        psp->call5[3] = 0x21;
                   5981:        psp->call5[4] = 0xc3;   // ret
                   5982: #endif
1.1       root     5983:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   5984:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   5985:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   5986:        psp->parent_psp = parent_psp;
1.1.1.20  root     5987:        if(parent_psp == (UINT16)-1) {
                   5988:                for(int i = 0; i < 20; i++) {
                   5989:                        if(file_handler[i].valid) {
                   5990:                                psp->file_table[i] = i;
                   5991:                        } else {
                   5992:                                psp->file_table[i] = 0xff;
                   5993:                        }
1.1       root     5994:                }
1.1.1.20  root     5995:        } else {
                   5996:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     5997:        }
                   5998:        psp->env_seg = env_seg;
                   5999:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     6000:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     6001:        psp->file_table_size = 20;
                   6002:        psp->file_table_ptr.w.l = 0x18;
                   6003:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     6004:        psp->service[0] = 0xcd;
                   6005:        psp->service[1] = 0x21;
                   6006:        psp->service[2] = 0xcb;
                   6007:        return(psp);
                   6008: }
                   6009: 
1.1.1.20  root     6010: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   6011: {
                   6012:        if(psp_seg && fd < 20) {
                   6013:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6014:                psp->file_table[fd] = value;
                   6015:        }
                   6016: }
                   6017: 
                   6018: int msdos_psp_get_file_table(int fd, int psp_seg)
                   6019: {
                   6020:        if(psp_seg && fd < 20) {
                   6021:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6022:                fd = psp->file_table[fd];
                   6023:        }
                   6024:        return fd;
                   6025: }
                   6026: 
1.1.1.45  root     6027: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al)
1.1       root     6028: {
                   6029:        // load command file
                   6030:        int fd = -1;
1.1.1.45  root     6031:        int sio_port = 0;
                   6032:        int lpt_port = 0;
1.1       root     6033:        int dos_command = 0;
1.1.1.24  root     6034:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     6035:        char pipe_stdin_path[MAX_PATH] = {0};
                   6036:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39  root     6037:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     6038:        
                   6039:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6040:        int opt_len = mem[opt_ofs];
                   6041:        memset(opt, 0, sizeof(opt));
                   6042:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6043:        
1.1.1.14  root     6044:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   6045:                // this is a batch file, run command.com
                   6046:                char tmp[MAX_PATH];
                   6047:                if(opt_len != 0) {
                   6048:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   6049:                } else {
                   6050:                        sprintf(tmp, "/C %s", cmd);
                   6051:                }
                   6052:                strcpy(opt, tmp);
                   6053:                opt_len = strlen(opt);
                   6054:                mem[opt_ofs] = opt_len;
                   6055:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6056:                strcpy(command, comspec_path);
                   6057:                strcpy(name_tmp, "COMMAND.COM");
                   6058:        } else {
                   6059:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   6060:                        // redirect C:\COMMAND.COM to comspec_path
                   6061:                        strcpy(command, comspec_path);
                   6062:                } else {
                   6063:                        strcpy(command, cmd);
                   6064:                }
1.1.1.24  root     6065:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   6066:                        return(-1);
                   6067:                }
1.1.1.14  root     6068:                memset(name_tmp, 0, sizeof(name_tmp));
                   6069:                strcpy(name_tmp, name);
                   6070:                
                   6071:                // check command.com
1.1.1.38  root     6072:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   6073:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     6074:                        if(opt_len == 0) {
                   6075: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   6076:                                process_t *current_process = NULL;
                   6077:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   6078:                                        if(process[i].psp == current_psp) {
                   6079:                                                current_process = &process[i];
                   6080:                                                break;
                   6081:                                        }
                   6082:                                }
                   6083:                                if(current_process != NULL) {
                   6084:                                        param->cmd_line.dw = current_process->dta.dw;
                   6085:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6086:                                        opt_len = mem[opt_ofs];
                   6087:                                        memset(opt, 0, sizeof(opt));
                   6088:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6089:                                }
                   6090:                        }
                   6091:                        for(int i = 0; i < opt_len; i++) {
                   6092:                                if(opt[i] == ' ') {
                   6093:                                        continue;
                   6094:                                }
                   6095:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   6096:                                        for(int j = i + 3; j < opt_len; j++) {
                   6097:                                                if(opt[j] == ' ') {
                   6098:                                                        continue;
                   6099:                                                }
                   6100:                                                char *token = my_strtok(opt + j, " ");
                   6101:                                                
1.1.1.38  root     6102:                                                strcpy(command, token);
                   6103:                                                char tmp[MAX_PATH];
                   6104:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39  root     6105:                                                strcpy(opt, "");
                   6106:                                                for(int i = 0; i < strlen(tmp); i++) {
                   6107:                                                        if(tmp[i] != ' ') {
                   6108:                                                                strcpy(opt, tmp + i);
                   6109:                                                                break;
                   6110:                                                        }
                   6111:                                                }
                   6112:                                                strcpy(tmp, opt);
1.1.1.38  root     6113:                                                
                   6114:                                                if(al == 0x00) {
1.1.1.39  root     6115:                                                        #define GET_FILE_PATH() { \
                   6116:                                                                if(token[0] != '>' && token[0] != '<') { \
                   6117:                                                                        token++; \
                   6118:                                                                } \
                   6119:                                                                token++; \
                   6120:                                                                while(*token == ' ') { \
                   6121:                                                                        token++; \
                   6122:                                                                } \
                   6123:                                                                char *ptr = token; \
                   6124:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
                   6125:                                                                        ptr++; \
                   6126:                                                                } \
                   6127:                                                                *ptr = '\0'; \
                   6128:                                                        }
                   6129:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
                   6130:                                                                GET_FILE_PATH();
1.1.1.38  root     6131:                                                                strcpy(pipe_stdin_path, token);
                   6132:                                                                strcpy(opt, tmp);
                   6133:                                                        }
1.1.1.39  root     6134:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
                   6135:                                                                GET_FILE_PATH();
1.1.1.38  root     6136:                                                                strcpy(pipe_stdout_path, token);
                   6137:                                                                strcpy(opt, tmp);
                   6138:                                                        }
1.1.1.39  root     6139:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6140:                                                                GET_FILE_PATH();
                   6141:                                                                strcpy(pipe_stderr_path, token);
                   6142:                                                                strcpy(opt, tmp);
                   6143:                                                        }
                   6144:                                                        #undef GET_FILE_PATH
                   6145:                                                        
                   6146:                                                        if((token = strstr(opt, "0<")) != NULL) {
                   6147:                                                                *token = '\0';
                   6148:                                                        }
                   6149:                                                        if((token = strstr(opt, "1>")) != NULL) {
                   6150:                                                                *token = '\0';
                   6151:                                                        }
                   6152:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6153:                                                                *token = '\0';
                   6154:                                                        }
1.1.1.38  root     6155:                                                        if((token = strstr(opt, "<")) != NULL) {
                   6156:                                                                *token = '\0';
                   6157:                                                        }
                   6158:                                                        if((token = strstr(opt, ">")) != NULL) {
                   6159:                                                                *token = '\0';
                   6160:                                                        }
1.1.1.14  root     6161:                                                }
1.1.1.39  root     6162:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
                   6163:                                                        opt[i] = '\0';
                   6164:                                                }
1.1.1.38  root     6165:                                                opt_len = strlen(opt);
                   6166:                                                mem[opt_ofs] = opt_len;
                   6167:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6168:                                                dos_command = 1;
1.1.1.14  root     6169:                                                break;
1.1       root     6170:                                        }
                   6171:                                }
1.1.1.14  root     6172:                                break;
1.1       root     6173:                        }
                   6174:                }
                   6175:        }
                   6176:        
                   6177:        // load command file
                   6178:        strcpy(path, command);
                   6179:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6180:                sprintf(path, "%s.COM", command);
                   6181:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6182:                        sprintf(path, "%s.EXE", command);
                   6183:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     6184:                                sprintf(path, "%s.BAT", command);
                   6185:                                if(_access(path, 0) == 0) {
                   6186:                                        // this is a batch file, run command.com
                   6187:                                        char tmp[MAX_PATH];
                   6188:                                        if(opt_len != 0) {
                   6189:                                                sprintf(tmp, "/C %s %s", path, opt);
                   6190:                                        } else {
                   6191:                                                sprintf(tmp, "/C %s", path);
                   6192:                                        }
                   6193:                                        strcpy(opt, tmp);
                   6194:                                        opt_len = strlen(opt);
                   6195:                                        mem[opt_ofs] = opt_len;
                   6196:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6197:                                        strcpy(path, comspec_path);
                   6198:                                        strcpy(name_tmp, "COMMAND.COM");
                   6199:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6200:                                } else {
                   6201:                                        // search path in parent environments
                   6202:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45  root     6203:                                        const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14  root     6204:                                        if(env != NULL) {
                   6205:                                                char env_path[4096];
                   6206:                                                strcpy(env_path, env);
                   6207:                                                char *token = my_strtok(env_path, ";");
                   6208:                                                
                   6209:                                                while(token != NULL) {
                   6210:                                                        if(strlen(token) != 0) {
                   6211:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   6212:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6213:                                                                        break;
                   6214:                                                                }
                   6215:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   6216:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6217:                                                                        break;
                   6218:                                                                }
                   6219:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   6220:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6221:                                                                        break;
                   6222:                                                                }
                   6223:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   6224:                                                                if(_access(path, 0) == 0) {
                   6225:                                                                        // this is a batch file, run command.com
                   6226:                                                                        char tmp[MAX_PATH];
                   6227:                                                                        if(opt_len != 0) {
                   6228:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   6229:                                                                        } else {
                   6230:                                                                                sprintf(tmp, "/C %s", path);
                   6231:                                                                        }
                   6232:                                                                        strcpy(opt, tmp);
                   6233:                                                                        opt_len = strlen(opt);
                   6234:                                                                        mem[opt_ofs] = opt_len;
                   6235:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6236:                                                                        strcpy(path, comspec_path);
                   6237:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   6238:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6239:                                                                        break;
                   6240:                                                                }
1.1.1.8   root     6241:                                                        }
1.1.1.14  root     6242:                                                        token = my_strtok(NULL, ";");
1.1       root     6243:                                                }
                   6244:                                        }
                   6245:                                }
                   6246:                        }
                   6247:                }
                   6248:        }
                   6249:        if(fd == -1) {
1.1.1.38  root     6250:                // we can not find command.com in the path, so open comspec_path
                   6251:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6252:                        strcpy(command, comspec_path);
                   6253:                        strcpy(path, command);
                   6254:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6255:                }
                   6256:        }
                   6257:        if(fd == -1) {
1.1       root     6258:                if(dos_command) {
                   6259:                        // may be dos command
                   6260:                        char tmp[MAX_PATH];
                   6261:                        sprintf(tmp, "%s %s", command, opt);
                   6262:                        system(tmp);
                   6263:                        return(0);
                   6264:                } else {
                   6265:                        return(-1);
                   6266:                }
                   6267:        }
                   6268:        _read(fd, file_buffer, sizeof(file_buffer));
                   6269:        _close(fd);
                   6270:        
                   6271:        // copy environment
1.1.1.29  root     6272:        int umb_linked, env_seg, psp_seg;
1.1       root     6273:        
1.1.1.29  root     6274:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6275:                msdos_mem_unlink_umb();
                   6276:        }
1.1.1.8   root     6277:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6278:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6279:                        if(umb_linked != 0) {
                   6280:                                msdos_mem_link_umb();
                   6281:                        }
                   6282:                        return(-1);
                   6283:                }
1.1       root     6284:        }
                   6285:        if(param->env_seg == 0) {
                   6286:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6287:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6288:        } else {
                   6289:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6290:        }
                   6291:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6292:        
                   6293:        // check exe header
                   6294:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6295:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6296:        UINT16 cs, ss, ip, sp;
                   6297:        
                   6298:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6299:                // memory allocation
                   6300:                int header_size = header->header_size * 16;
                   6301:                int load_size = header->pages * 512 - header_size;
                   6302:                if(header_size + load_size < 512) {
                   6303:                        load_size = 512 - header_size;
                   6304:                }
                   6305:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6306:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6307:                        msdos_mem_free(env_seg);
                   6308:                        return(-1);
                   6309:                }
                   6310:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6311:                if(paragraphs > free_paragraphs) {
                   6312:                        paragraphs = free_paragraphs;
                   6313:                }
1.1.1.8   root     6314:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6315:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6316:                                if(umb_linked != 0) {
                   6317:                                        msdos_mem_link_umb();
                   6318:                                }
                   6319:                                msdos_mem_free(env_seg);
                   6320:                                return(-1);
                   6321:                        }
1.1       root     6322:                }
                   6323:                // relocation
                   6324:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6325:                for(int i = 0; i < header->relocations; i++) {
                   6326:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6327:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6328:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6329:                }
                   6330:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6331:                // segments
                   6332:                cs = header->init_cs + start_seg;
                   6333:                ss = header->init_ss + start_seg;
                   6334:                ip = header->init_ip;
                   6335:                sp = header->init_sp - 2; // for symdeb
                   6336:        } else {
                   6337:                // memory allocation
                   6338:                paragraphs = free_paragraphs;
1.1.1.8   root     6339:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6340:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6341:                                if(umb_linked != 0) {
                   6342:                                        msdos_mem_link_umb();
                   6343:                                }
                   6344:                                msdos_mem_free(env_seg);
                   6345:                                return(-1);
                   6346:                        }
1.1       root     6347:                }
                   6348:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6349:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6350:                // segments
                   6351:                cs = ss = psp_seg;
                   6352:                ip = 0x100;
                   6353:                sp = 0xfffe;
                   6354:        }
1.1.1.29  root     6355:        if(umb_linked != 0) {
                   6356:                msdos_mem_link_umb();
                   6357:        }
1.1       root     6358:        
                   6359:        // create psp
1.1.1.3   root     6360:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6361:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6362:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6363:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6364:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6365:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6366:        
                   6367:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6368:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6369:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6370:        
1.1.1.4   root     6371:        for(int i = 0; i < 8; i++) {
                   6372:                if(name_tmp[i] == '.') {
                   6373:                        mcb_psp->prog_name[i] = '\0';
                   6374:                        break;
                   6375:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6376:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6377:                        i++;
                   6378:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6379:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6380:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6381:                } else {
                   6382:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6383:                }
                   6384:        }
                   6385:        
1.1       root     6386:        // process info
                   6387:        process_t *process = msdos_process_info_create(psp_seg);
                   6388:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6389: #ifdef USE_DEBUGGER
                   6390:        strcpy(process->module_path, path);
                   6391: #endif
1.1       root     6392:        process->dta.w.l = 0x80;
                   6393:        process->dta.w.h = psp_seg;
                   6394:        process->switchar = '/';
                   6395:        process->max_files = 20;
                   6396:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6397:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6398:        process->parent_ds = SREG(DS);
1.1.1.31  root     6399:        process->parent_es = SREG(ES);
1.1       root     6400:        
                   6401:        current_psp = psp_seg;
1.1.1.23  root     6402:        msdos_sda_update(current_psp);
1.1       root     6403:        
                   6404:        if(al == 0x00) {
                   6405:                int_10h_feh_called = int_10h_ffh_called = false;
                   6406:                
1.1.1.38  root     6407:                // pipe
                   6408:                if(pipe_stdin_path[0] != '\0') {
1.1.1.45  root     6409: //                     if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
                   6410:                        if(msdos_is_device_path(pipe_stdin_path)) {
                   6411:                                fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
                   6412:                        } else {
                   6413:                                fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
                   6414:                        }
                   6415:                        if(fd != -1) {
                   6416:                                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     6417:                                psp->file_table[0] = fd;
                   6418:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6419:                        }
                   6420:                }
                   6421:                if(pipe_stdout_path[0] != '\0') {
                   6422:                        if(_access(pipe_stdout_path, 0) == 0) {
                   6423:                                SetFileAttributes(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6424:                                DeleteFile(pipe_stdout_path);
                   6425:                        }
1.1.1.45  root     6426: //                     if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6427:                        if(msdos_is_device_path(pipe_stdout_path)) {
                   6428:                                fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6429:                        } else {
                   6430:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6431:                        }
                   6432:                        if(fd != -1) {
                   6433:                                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     6434:                                psp->file_table[1] = fd;
                   6435:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6436:                        }
                   6437:                }
1.1.1.39  root     6438:                if(pipe_stderr_path[0] != '\0') {
                   6439:                        if(_access(pipe_stderr_path, 0) == 0) {
                   6440:                                SetFileAttributes(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
                   6441:                                DeleteFile(pipe_stderr_path);
                   6442:                        }
1.1.1.45  root     6443: //                     if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6444:                        if(msdos_is_device_path(pipe_stderr_path)) {
                   6445:                                fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6446:                        } else {
                   6447:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6448:                        }
                   6449:                        if(fd != -1) {
                   6450:                                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     6451:                                psp->file_table[2] = fd;
                   6452:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6453:                        }
                   6454:                }
1.1.1.38  root     6455:                
1.1       root     6456:                // registers and segments
                   6457:                REG16(AX) = REG16(BX) = 0x00;
                   6458:                REG16(CX) = 0xff;
                   6459:                REG16(DX) = psp_seg;
                   6460:                REG16(SI) = ip;
                   6461:                REG16(DI) = sp;
                   6462:                REG16(SP) = sp;
1.1.1.3   root     6463:                SREG(DS) = SREG(ES) = psp_seg;
                   6464:                SREG(SS) = ss;
                   6465:                i386_load_segment_descriptor(DS);
                   6466:                i386_load_segment_descriptor(ES);
                   6467:                i386_load_segment_descriptor(SS);
1.1       root     6468:                
                   6469:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6470:                i386_jmp_far(cs, ip);
                   6471:        } else if(al == 0x01) {
                   6472:                // copy ss:sp and cs:ip to param block
                   6473:                param->sp = sp;
                   6474:                param->ss = ss;
                   6475:                param->ip = ip;
                   6476:                param->cs = cs;
1.1.1.31  root     6477:                
                   6478:                // the AX value to be passed to the child program is put on top of the child's stack
                   6479:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6480:        }
                   6481:        return(0);
                   6482: }
                   6483: 
                   6484: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6485: {
                   6486:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6487:        
                   6488:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6489:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6490:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6491:        
1.1.1.3   root     6492:        SREG(SS) = psp->stack.w.h;
                   6493:        i386_load_segment_descriptor(SS);
1.1       root     6494:        REG16(SP) = psp->stack.w.l;
                   6495:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6496:        
1.1.1.28  root     6497: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6498:        process_t *current_process = NULL;
                   6499:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6500:                if(process[i].psp == psp_seg) {
                   6501:                        current_process = &process[i];
                   6502:                        break;
                   6503:                }
                   6504:        }
                   6505:        if(current_process == NULL) {
                   6506:                throw(0x1f); // general failure
                   6507:        }
                   6508:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6509:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6510:        if(current_process->called_by_int2eh) {
                   6511:                REG16(AX) = ret;
                   6512:        }
                   6513:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6514:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6515:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6516:        i386_load_segment_descriptor(ES);
1.1       root     6517:        
                   6518:        if(mem_free) {
1.1.1.8   root     6519:                int mcb_seg;
                   6520:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6521:                        msdos_mem_free(mcb_seg + 1);
                   6522:                }
                   6523:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6524:                        msdos_mem_free(mcb_seg + 1);
                   6525:                }
1.1       root     6526:                
                   6527:                for(int i = 0; i < MAX_FILES; i++) {
                   6528:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6529:                                _close(i);
1.1.1.20  root     6530:                                msdos_file_handler_close(i);
                   6531:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6532:                        }
                   6533:                }
1.1.1.13  root     6534:                msdos_dta_info_free(psp_seg);
1.1       root     6535:        }
1.1.1.14  root     6536:        msdos_stdio_reopen();
1.1       root     6537:        
1.1.1.28  root     6538:        memset(current_process, 0, sizeof(process_t));
1.1       root     6539:        
                   6540:        current_psp = psp->parent_psp;
                   6541:        retval = ret;
1.1.1.23  root     6542:        msdos_sda_update(current_psp);
1.1       root     6543: }
                   6544: 
                   6545: // drive
                   6546: 
1.1.1.42  root     6547: int pcbios_update_drive_param(int drive_num, int force_update);
                   6548: 
1.1       root     6549: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6550: {
1.1.1.41  root     6551:        if(!(drive_num >= 0 && drive_num < 26)) {
                   6552:                return(0);
                   6553:        }
1.1.1.42  root     6554:        pcbios_update_drive_param(drive_num, force_update);
                   6555:        
                   6556:        drive_param_t *drive_param = &drive_params[drive_num];
1.1       root     6557:        *seg = DPB_TOP >> 4;
                   6558:        *ofs = sizeof(dpb_t) * drive_num;
                   6559:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6560:        
                   6561:        memset(dpb, 0, sizeof(dpb_t));
                   6562:        
1.1.1.41  root     6563:        dpb->drive_num = drive_num;
                   6564:        dpb->unit_num = drive_num;
1.1.1.42  root     6565:        
                   6566:        if(drive_param->valid) {
                   6567:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   6568:                
                   6569:                dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
                   6570:                dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
                   6571:                dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6572:                dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6573:                switch(geo->MediaType) {
                   6574:                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6575:                        dpb->media_type = 0xff;
                   6576:                        break;
                   6577:                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6578:                        dpb->media_type = 0xfe;
                   6579:                        break;
                   6580:                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6581:                        dpb->media_type = 0xfd;
                   6582:                        break;
                   6583:                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6584:                        dpb->media_type = 0xfc;
                   6585:                        break;
                   6586:                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6587:                case F3_1Pt2_512:
                   6588:                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6589:                case F5_720_512:
                   6590:                        dpb->media_type = 0xf9;
                   6591:                        break;
                   6592:                case FixedMedia:        // hard disk
                   6593:                case RemovableMedia:
                   6594:                case Unknown:
                   6595:                        dpb->media_type = 0xf8;
                   6596:                        break;
                   6597:                default:
                   6598:                        dpb->media_type = 0xf0;
                   6599:                        break;
                   6600:                }
                   6601:        }
1.1.1.41  root     6602:        dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
                   6603:        dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42  root     6604:        dpb->info_sector = 0xffff;
                   6605:        dpb->backup_boot_sector = 0xffff;
                   6606:        dpb->free_clusters = 0xffff;
                   6607:        dpb->free_search_cluster = 0xffffffff;
                   6608:        
                   6609:        return(drive_param->valid);
1.1       root     6610: }
                   6611: 
                   6612: // pc bios
                   6613: 
1.1.1.35  root     6614: #ifdef USE_SERVICE_THREAD
                   6615: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   6616: {
                   6617: #if defined(HAS_I386)
                   6618:        if(m_SF != 0) {
                   6619:                m_SF = 0;
1.1.1.49  root     6620:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6621:        } else {
                   6622:                m_SF = 1;
1.1.1.49  root     6623:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6624:        }
                   6625: #else
                   6626:        if(m_SignVal < 0) {
                   6627:                m_SignVal = 0;
1.1.1.49  root     6628:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6629:        } else {
                   6630:                m_SignVal = -1;
1.1.1.49  root     6631:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6632:        }
                   6633: #endif
1.1.1.49  root     6634:        i386_call_far(DUMMY_TOP >> 4, 0x0013);
1.1.1.35  root     6635:        in_service = true;
                   6636:        service_exit = false;
                   6637:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   6638: }
                   6639: 
                   6640: void finish_service_loop()
                   6641: {
                   6642:        if(in_service && service_exit) {
                   6643: #if defined(HAS_I386)
                   6644:                if(m_SF != 0) {
                   6645:                        m_SF = 0;
                   6646:                } else {
                   6647:                        m_SF = 1;
                   6648:                }
                   6649: #else
                   6650:                if(m_SignVal < 0) {
                   6651:                        m_SignVal = 0;
                   6652:                } else {
                   6653:                        m_SignVal = -1;
                   6654:                }
                   6655: #endif
                   6656:                in_service = false;
                   6657:        }
                   6658: }
                   6659: #endif
                   6660: 
1.1.1.19  root     6661: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   6662: {
                   6663:        static unsigned __int64 start_msec_since_midnight = 0;
                   6664:        static unsigned __int64 start_msec_since_hostboot = 0;
                   6665:        
                   6666:        if(start_msec_since_midnight == 0) {
                   6667:                SYSTEMTIME time;
                   6668:                GetLocalTime(&time);
                   6669:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   6670:                start_msec_since_hostboot = cur_msec;
                   6671:        }
                   6672:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   6673:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   6674:        return (UINT32)tick;
                   6675: }
                   6676: 
                   6677: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   6678: {
                   6679:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   6680:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   6681:        
                   6682:        if(prev_tick > next_tick) {
                   6683:                mem[0x470] = 1;
                   6684:        }
                   6685:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   6686: }
                   6687: 
1.1.1.14  root     6688: inline void pcbios_irq0()
                   6689: {
                   6690:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     6691:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     6692: }
                   6693: 
1.1.1.16  root     6694: int pcbios_get_text_vram_address(int page)
1.1       root     6695: {
                   6696:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6697:                return TEXT_VRAM_TOP;
1.1       root     6698:        } else {
1.1.1.14  root     6699:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     6700:        }
                   6701: }
                   6702: 
1.1.1.16  root     6703: int pcbios_get_shadow_buffer_address(int page)
1.1       root     6704: {
1.1.1.14  root     6705:        if(!int_10h_feh_called) {
1.1.1.16  root     6706:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     6707:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6708:                return SHADOW_BUF_TOP;
                   6709:        } else {
1.1.1.14  root     6710:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     6711:        }
                   6712: }
                   6713: 
1.1.1.16  root     6714: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     6715: {
1.1.1.16  root     6716:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     6717: }
                   6718: 
1.1.1.16  root     6719: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     6720: {
1.1.1.14  root     6721:        // clear the existing screen, not just the new one
                   6722:        int clr_height = max(height, scr_height);
                   6723:        
1.1.1.16  root     6724:        if(scr_width != width || scr_height != height) {
                   6725:                change_console_size(width, height);
1.1.1.14  root     6726:        }
                   6727:        mem[0x462] = 0;
                   6728:        *(UINT16 *)(mem + 0x44e) = 0;
                   6729:        
1.1.1.16  root     6730:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   6731:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   6732:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   6733:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1       root     6734:        
1.1.1.23  root     6735:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     6736:        if(clr_screen) {
1.1.1.14  root     6737:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   6738:                        mem[ofs++] = 0x20;
                   6739:                        mem[ofs++] = 0x07;
                   6740:                }
                   6741:                
1.1.1.35  root     6742: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6743:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6744: #endif
1.1.1.14  root     6745:                for(int y = 0; y < clr_height; y++) {
                   6746:                        for(int x = 0; x < scr_width; x++) {
                   6747:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6748:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     6749:                        }
                   6750:                }
                   6751:                SMALL_RECT rect;
1.1.1.14  root     6752:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   6753:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6754:                vram_length_char = vram_last_length_char = 0;
                   6755:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     6756: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6757:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6758: #endif
1.1       root     6759:        }
1.1.1.14  root     6760:        COORD co;
                   6761:        co.X = 0;
                   6762:        co.Y = scr_top;
                   6763:        SetConsoleCursorPosition(hStdout, co);
                   6764:        cursor_moved = true;
                   6765:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     6766: }
                   6767: 
1.1.1.36  root     6768: void pcbios_update_cursor_position()
                   6769: {
                   6770:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6771:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   6772:        if(!restore_console_on_exit) {
                   6773:                scr_top = csbi.srWindow.Top;
                   6774:        }
                   6775:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   6776:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   6777: }
                   6778: 
1.1.1.16  root     6779: inline void pcbios_int_10h_00h()
                   6780: {
                   6781:        switch(REG8(AL) & 0x7f) {
                   6782:        case 0x70: // v-text mode
                   6783:        case 0x71: // extended cga v-text mode
                   6784:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   6785:                break;
                   6786:        default:
                   6787:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   6788:                break;
                   6789:        }
                   6790:        if(REG8(AL) & 0x80) {
                   6791:                mem[0x487] |= 0x80;
                   6792:        } else {
                   6793:                mem[0x487] &= ~0x80;
                   6794:        }
                   6795:        mem[0x449] = REG8(AL) & 0x7f;
                   6796: }
                   6797: 
1.1       root     6798: inline void pcbios_int_10h_01h()
                   6799: {
1.1.1.13  root     6800:        mem[0x460] = REG8(CL);
                   6801:        mem[0x461] = REG8(CH);
1.1.1.14  root     6802:        
1.1.1.23  root     6803:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6804:        CONSOLE_CURSOR_INFO ci;
                   6805:        GetConsoleCursorInfo(hStdout, &ci);
                   6806: //     ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
                   6807: //     if(ci.bVisible) {
                   6808:                int lines = max(8, REG8(CL) + 1);
                   6809:                ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
                   6810: //     }
                   6811:        SetConsoleCursorInfo(hStdout, &ci);
1.1       root     6812: }
                   6813: 
                   6814: inline void pcbios_int_10h_02h()
                   6815: {
1.1.1.14  root     6816:        // continuously setting the cursor effectively stops it blinking
                   6817:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     6818:                COORD co;
                   6819:                co.X = REG8(DL);
1.1.1.14  root     6820:                co.Y = REG8(DH) + scr_top;
                   6821:                
                   6822:                // some programs hide the cursor by moving it off screen
                   6823:                static bool hidden = false;
1.1.1.23  root     6824:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6825:                CONSOLE_CURSOR_INFO ci;
                   6826:                GetConsoleCursorInfo(hStdout, &ci);
                   6827:                
                   6828:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   6829:                        if(ci.bVisible) {
                   6830:                                ci.bVisible = FALSE;
                   6831: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6832:                                hidden = true;
                   6833:                        }
                   6834:                } else if(hidden) {
                   6835:                        if(!ci.bVisible) {
                   6836:                                ci.bVisible = TRUE;
                   6837: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6838:                        }
                   6839:                        hidden = false;
                   6840:                }
1.1       root     6841:        }
1.1.1.14  root     6842:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   6843:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     6844: }
                   6845: 
                   6846: inline void pcbios_int_10h_03h()
                   6847: {
1.1.1.14  root     6848:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6849:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6850:        REG8(CL) = mem[0x460];
                   6851:        REG8(CH) = mem[0x461];
                   6852: }
                   6853: 
                   6854: inline void pcbios_int_10h_05h()
                   6855: {
1.1.1.14  root     6856:        if(REG8(AL) >= vram_pages) {
                   6857:                return;
                   6858:        }
                   6859:        if(mem[0x462] != REG8(AL)) {
                   6860:                vram_flush();
                   6861:                
1.1.1.23  root     6862:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6863:                SMALL_RECT rect;
1.1.1.14  root     6864:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6865:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6866:                
1.1.1.16  root     6867:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     6868:                        for(int x = 0; x < scr_width; x++) {
                   6869:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6870:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6871:                        }
                   6872:                }
1.1.1.16  root     6873:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     6874:                        for(int x = 0; x < scr_width; x++) {
                   6875:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   6876:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     6877:                        }
                   6878:                }
1.1.1.14  root     6879:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6880:                
                   6881:                COORD co;
1.1.1.14  root     6882:                co.X = mem[0x450 + REG8(AL) * 2];
                   6883:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   6884:                if(co.Y < scr_top + scr_height) {
                   6885:                        SetConsoleCursorPosition(hStdout, co);
                   6886:                }
1.1       root     6887:        }
1.1.1.14  root     6888:        mem[0x462] = REG8(AL);
                   6889:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   6890:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     6891:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     6892:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     6893:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     6894:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1       root     6895: }
                   6896: 
                   6897: inline void pcbios_int_10h_06h()
                   6898: {
1.1.1.14  root     6899:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6900:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6901:                return;
                   6902:        }
                   6903:        vram_flush();
                   6904:        
1.1.1.23  root     6905:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6906:        SMALL_RECT rect;
1.1.1.14  root     6907:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6908:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6909:        
                   6910:        int right = min(REG8(DL), scr_width - 1);
                   6911:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6912:        
                   6913:        if(REG8(AL) == 0) {
1.1.1.14  root     6914:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6915:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6916:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6917:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6918:                        }
                   6919:                }
                   6920:        } else {
1.1.1.14  root     6921:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     6922:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6923:                                if(y2 <= bottom) {
                   6924:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6925:                                } else {
1.1.1.14  root     6926:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6927:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6928:                                }
1.1.1.14  root     6929:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6930:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6931:                        }
                   6932:                }
                   6933:        }
1.1.1.14  root     6934:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6935: }
                   6936: 
                   6937: inline void pcbios_int_10h_07h()
                   6938: {
1.1.1.14  root     6939:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6940:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6941:                return;
                   6942:        }
                   6943:        vram_flush();
                   6944:        
1.1.1.23  root     6945:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6946:        SMALL_RECT rect;
1.1.1.14  root     6947:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6948:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6949:        
                   6950:        int right = min(REG8(DL), scr_width - 1);
                   6951:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6952:        
                   6953:        if(REG8(AL) == 0) {
1.1.1.14  root     6954:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6955:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6956:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6957:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6958:                        }
                   6959:                }
                   6960:        } else {
1.1.1.14  root     6961:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     6962:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6963:                                if(y2 >= REG8(CH)) {
                   6964:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6965:                                } else {
1.1.1.14  root     6966:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6967:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6968:                                }
1.1.1.14  root     6969:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6970:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6971:                        }
                   6972:                }
                   6973:        }
1.1.1.14  root     6974:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6975: }
                   6976: 
                   6977: inline void pcbios_int_10h_08h()
                   6978: {
                   6979:        COORD co;
                   6980:        DWORD num;
                   6981:        
1.1.1.14  root     6982:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6983:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6984:        
                   6985:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6986:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6987:                co.Y += scr_top;
                   6988:                vram_flush();
1.1       root     6989:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   6990:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   6991:                REG8(AL) = scr_char[0];
                   6992:                REG8(AH) = scr_attr[0];
                   6993:        } else {
1.1.1.16  root     6994:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     6995:        }
                   6996: }
                   6997: 
                   6998: inline void pcbios_int_10h_09h()
                   6999: {
                   7000:        COORD co;
                   7001:        
1.1.1.14  root     7002:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7003:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7004:        
1.1.1.16  root     7005:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7006:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7007:        
                   7008:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7009: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7010:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7011: #endif
1.1.1.16  root     7012:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7013:                while(dest < end) {
                   7014:                        write_text_vram_char(dest - vram, REG8(AL));
                   7015:                        mem[dest++] = REG8(AL);
                   7016:                        write_text_vram_attr(dest - vram, REG8(BL));
                   7017:                        mem[dest++] = REG8(BL);
1.1       root     7018:                }
1.1.1.35  root     7019: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7020:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7021: #endif
1.1       root     7022:        } else {
1.1.1.14  root     7023:                while(dest < end) {
1.1       root     7024:                        mem[dest++] = REG8(AL);
                   7025:                        mem[dest++] = REG8(BL);
                   7026:                }
                   7027:        }
                   7028: }
                   7029: 
                   7030: inline void pcbios_int_10h_0ah()
                   7031: {
                   7032:        COORD co;
                   7033:        
1.1.1.14  root     7034:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7035:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7036:        
1.1.1.16  root     7037:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7038:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7039:        
                   7040:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7041: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7042:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7043: #endif
1.1.1.16  root     7044:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7045:                while(dest < end) {
                   7046:                        write_text_vram_char(dest - vram, REG8(AL));
                   7047:                        mem[dest++] = REG8(AL);
                   7048:                        dest++;
1.1       root     7049:                }
1.1.1.35  root     7050: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7051:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7052: #endif
1.1       root     7053:        } else {
1.1.1.14  root     7054:                while(dest < end) {
1.1       root     7055:                        mem[dest++] = REG8(AL);
                   7056:                        dest++;
                   7057:                }
                   7058:        }
                   7059: }
                   7060: 
1.1.1.40  root     7061: HDC get_console_window_device_context()
                   7062: {
                   7063:        static HWND hwndFound = 0;
                   7064:        
                   7065:        if(hwndFound == 0) {
                   7066:                // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
                   7067:                char pszNewWindowTitle[1024];
                   7068:                char pszOldWindowTitle[1024];
                   7069:                
                   7070:                GetConsoleTitle(pszOldWindowTitle, 1024);
                   7071:                wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
                   7072:                SetConsoleTitle(pszNewWindowTitle);
                   7073:                Sleep(100);
                   7074:                hwndFound = FindWindow(NULL, pszNewWindowTitle);
                   7075:                SetConsoleTitle(pszOldWindowTitle);
                   7076:        }
                   7077:        return GetDC(hwndFound);
                   7078: }
                   7079: 
                   7080: inline void pcbios_int_10h_0ch()
                   7081: {
                   7082:        HDC hdc = get_console_window_device_context();
                   7083:        
                   7084:        if(hdc != NULL) {
                   7085:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
                   7086:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
                   7087:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
                   7088:                
                   7089:                if(REG8(AL) & 0x80) {
                   7090:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7091:                        if(color != CLR_INVALID) {
                   7092:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7093:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7094:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
                   7095:                        }
                   7096:                }
                   7097:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
                   7098:        }
                   7099: }
                   7100: 
                   7101: inline void pcbios_int_10h_0dh()
                   7102: {
                   7103:        HDC hdc = get_console_window_device_context();
                   7104:        BYTE r = 0;
                   7105:        BYTE g = 0;
                   7106:        BYTE b = 0;
                   7107:        
                   7108:        if(hdc != NULL) {
                   7109:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7110:                if(color != CLR_INVALID) {
                   7111:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7112:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7113:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
                   7114:                }
                   7115:        }
                   7116:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
                   7117: }
                   7118: 
1.1       root     7119: inline void pcbios_int_10h_0eh()
                   7120: {
1.1.1.14  root     7121:        DWORD num;
                   7122:        COORD co;
                   7123:        
                   7124:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7125:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7126:        
                   7127:        if(REG8(AL) == 7) {
                   7128:                //MessageBeep(-1);
                   7129:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   7130:                if(REG8(AL) == 10) {
                   7131:                        vram_flush();
                   7132:                }
1.1.1.23  root     7133:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     7134:                cursor_moved = true;
                   7135:        } else {
1.1.1.16  root     7136:                int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
1.1.1.14  root     7137:                if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7138: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7139:                        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7140: #endif
1.1.1.16  root     7141:                        int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7142:                        write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     7143: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7144:                        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7145: #endif
1.1.1.14  root     7146:                        
1.1.1.23  root     7147:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7148:                        if(++co.X == scr_width) {
                   7149:                                co.X = 0;
                   7150:                                if(++co.Y == scr_height) {
                   7151:                                        vram_flush();
                   7152:                                        WriteConsole(hStdout, "\n", 1, &num, NULL);
                   7153:                                        cursor_moved = true;
                   7154:                                }
                   7155:                        }
                   7156:                        if(!cursor_moved) {
                   7157:                                co.Y += scr_top;
                   7158:                                SetConsoleCursorPosition(hStdout, co);
                   7159:                                cursor_moved = true;
                   7160:                        }
                   7161:                }
                   7162:                mem[dest] = REG8(AL);
                   7163:        }
1.1       root     7164: }
                   7165: 
                   7166: inline void pcbios_int_10h_0fh()
                   7167: {
                   7168:        REG8(AL) = mem[0x449];
                   7169:        REG8(AH) = mem[0x44a];
                   7170:        REG8(BH) = mem[0x462];
                   7171: }
                   7172: 
1.1.1.14  root     7173: inline void pcbios_int_10h_11h()
                   7174: {
                   7175:        switch(REG8(AL)) {
1.1.1.16  root     7176:        case 0x01:
1.1.1.14  root     7177:        case 0x11:
1.1.1.16  root     7178:                pcbios_set_console_size(80, 28, true);
1.1.1.14  root     7179:                break;
1.1.1.16  root     7180:        case 0x02:
1.1.1.14  root     7181:        case 0x12:
1.1.1.16  root     7182:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     7183:                break;
1.1.1.16  root     7184:        case 0x04:
1.1.1.14  root     7185:        case 0x14:
1.1.1.16  root     7186:                pcbios_set_console_size(80, 25, true);
                   7187:                break;
                   7188:        case 0x18:
                   7189:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     7190:                break;
                   7191:        case 0x30:
                   7192:                SREG(ES) = 0;
                   7193:                i386_load_segment_descriptor(ES);
                   7194:                REG16(BP) = 0;
                   7195:                REG16(CX) = mem[0x485];
                   7196:                REG8(DL) = mem[0x484];
                   7197:                break;
                   7198:        }
                   7199: }
                   7200: 
                   7201: inline void pcbios_int_10h_12h()
                   7202: {
1.1.1.16  root     7203:        switch(REG8(BL)) {
                   7204:        case 0x10:
1.1.1.14  root     7205:                REG16(BX) = 0x0003;
                   7206:                REG16(CX) = 0x0009;
1.1.1.16  root     7207:                break;
1.1.1.14  root     7208:        }
                   7209: }
                   7210: 
1.1       root     7211: inline void pcbios_int_10h_13h()
                   7212: {
1.1.1.3   root     7213:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     7214:        COORD co;
                   7215:        DWORD num;
                   7216:        
                   7217:        co.X = REG8(DL);
1.1.1.14  root     7218:        co.Y = REG8(DH) + scr_top;
                   7219:        
                   7220:        vram_flush();
1.1       root     7221:        
                   7222:        switch(REG8(AL)) {
                   7223:        case 0x00:
                   7224:        case 0x01:
                   7225:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7226:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7227:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7228:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7229:                        SetConsoleCursorPosition(hStdout, co);
                   7230:                        
                   7231:                        if(csbi.wAttributes != REG8(BL)) {
                   7232:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   7233:                        }
1.1.1.14  root     7234:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   7235:                        
1.1       root     7236:                        if(csbi.wAttributes != REG8(BL)) {
                   7237:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7238:                        }
                   7239:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     7240:                                if(!restore_console_on_exit) {
                   7241:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7242:                                        scr_top = csbi.srWindow.Top;
                   7243:                                }
1.1.1.14  root     7244:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7245:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7246:                                SetConsoleCursorPosition(hStdout, co);
                   7247:                        } else {
                   7248:                                cursor_moved = true;
                   7249:                        }
                   7250:                } else {
1.1.1.3   root     7251:                        m_CF = 1;
1.1       root     7252:                }
                   7253:                break;
                   7254:        case 0x02:
                   7255:        case 0x03:
                   7256:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7257:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7258:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7259:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7260:                        SetConsoleCursorPosition(hStdout, co);
                   7261:                        
                   7262:                        WORD wAttributes = csbi.wAttributes;
                   7263:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7264:                                if(wAttributes != mem[ofs + 1]) {
                   7265:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7266:                                        wAttributes = mem[ofs + 1];
                   7267:                                }
1.1.1.14  root     7268:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7269:                        }
                   7270:                        if(csbi.wAttributes != wAttributes) {
                   7271:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7272:                        }
                   7273:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7274:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7275:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7276:                                SetConsoleCursorPosition(hStdout, co);
                   7277:                        } else {
                   7278:                                cursor_moved = true;
                   7279:                        }
                   7280:                } else {
1.1.1.3   root     7281:                        m_CF = 1;
1.1       root     7282:                }
                   7283:                break;
                   7284:        case 0x10:
                   7285:        case 0x11:
                   7286:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7287:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7288:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   7289:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7290:                        for(int i = 0; i < num; i++) {
                   7291:                                mem[ofs++] = scr_char[i];
                   7292:                                mem[ofs++] = scr_attr[i];
1.1.1.45  root     7293:                                if(REG8(AL) & 0x01) {
1.1       root     7294:                                        mem[ofs++] = 0;
                   7295:                                        mem[ofs++] = 0;
                   7296:                                }
                   7297:                        }
                   7298:                } else {
1.1.1.16  root     7299:                        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     7300:                                mem[ofs++] = mem[src++];
                   7301:                                mem[ofs++] = mem[src++];
1.1.1.45  root     7302:                                if(REG8(AL) & 0x01) {
1.1       root     7303:                                        mem[ofs++] = 0;
                   7304:                                        mem[ofs++] = 0;
                   7305:                                }
1.1.1.14  root     7306:                                if(++co.X == scr_width) {
                   7307:                                        if(++co.Y == scr_height) {
1.1       root     7308:                                                break;
                   7309:                                        }
                   7310:                                        co.X = 0;
                   7311:                                }
                   7312:                        }
                   7313:                }
                   7314:                break;
1.1.1.45  root     7315:        case 0x12: // ???
                   7316:        case 0x13: // ???
1.1       root     7317:        case 0x20:
                   7318:        case 0x21:
                   7319:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7320:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7321:                        int len = min(REG16(CX), scr_width * scr_height);
                   7322:                        for(int i = 0; i < len; i++) {
1.1       root     7323:                                scr_char[i] = mem[ofs++];
                   7324:                                scr_attr[i] = mem[ofs++];
1.1.1.45  root     7325:                                if(REG8(AL) & 0x01) {
1.1       root     7326:                                        ofs += 2;
                   7327:                                }
                   7328:                        }
1.1.1.14  root     7329:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7330:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7331:                } else {
1.1.1.16  root     7332:                        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     7333:                                mem[dest++] = mem[ofs++];
                   7334:                                mem[dest++] = mem[ofs++];
1.1.1.45  root     7335:                                if(REG8(AL) & 0x01) {
1.1       root     7336:                                        ofs += 2;
                   7337:                                }
1.1.1.14  root     7338:                                if(++co.X == scr_width) {
                   7339:                                        if(++co.Y == scr_height) {
1.1       root     7340:                                                break;
                   7341:                                        }
                   7342:                                        co.X = 0;
                   7343:                                }
                   7344:                        }
                   7345:                }
                   7346:                break;
                   7347:        default:
1.1.1.22  root     7348:                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     7349:                m_CF = 1;
1.1       root     7350:                break;
                   7351:        }
                   7352: }
                   7353: 
1.1.1.30  root     7354: inline void pcbios_int_10h_18h()
                   7355: {
                   7356:        switch(REG8(AL)) {
                   7357:        case 0x00:
                   7358:        case 0x01:
                   7359: //             REG8(AL) = 0x86;
                   7360:                REG8(AL) = 0x00;
                   7361:                break;
                   7362:        default:
                   7363:                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));
                   7364:                m_CF = 1;
                   7365:                break;
                   7366:        }
                   7367: }
                   7368: 
1.1.1.14  root     7369: inline void pcbios_int_10h_1ah()
                   7370: {
                   7371:        switch(REG8(AL)) {
                   7372:        case 0x00:
                   7373:                REG8(AL) = 0x1a;
                   7374:                REG8(BL) = 0x08;
                   7375:                REG8(BH) = 0x00;
                   7376:                break;
                   7377:        default:
1.1.1.22  root     7378:                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     7379:                m_CF = 1;
                   7380:                break;
                   7381:        }
                   7382: }
                   7383: 
1.1       root     7384: inline void pcbios_int_10h_1dh()
                   7385: {
                   7386:        switch(REG8(AL)) {
1.1.1.43  root     7387:        case 0x00:
                   7388:                // DOS/V Shift Status Line Control is not supported
                   7389:                m_CF = 1;
                   7390:                break;
1.1       root     7391:        case 0x01:
                   7392:                break;
                   7393:        case 0x02:
                   7394:                REG16(BX) = 0;
                   7395:                break;
                   7396:        default:
1.1.1.22  root     7397:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   7398:                m_CF = 1;
                   7399:                break;
                   7400:        }
                   7401: }
                   7402: 
                   7403: inline void pcbios_int_10h_4fh()
                   7404: {
                   7405:        switch(REG8(AL)) {
                   7406:        case 0x00:
                   7407:                REG8(AH) = 0x02; // not supported
                   7408:                break;
                   7409:        case 0x01:
                   7410:        case 0x02:
                   7411:        case 0x03:
                   7412:        case 0x04:
                   7413:        case 0x05:
                   7414:        case 0x06:
                   7415:        case 0x07:
                   7416:        case 0x08:
                   7417:        case 0x09:
                   7418:        case 0x0a:
                   7419:        case 0x0b:
                   7420:        case 0x0c:
                   7421:                REG8(AH) = 0x01; // failed
                   7422:                break;
                   7423:        default:
                   7424:                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     7425:                m_CF = 1;
1.1       root     7426:                break;
                   7427:        }
                   7428: }
                   7429: 
                   7430: inline void pcbios_int_10h_82h()
                   7431: {
                   7432:        static UINT8 mode = 0;
                   7433:        
                   7434:        switch(REG8(AL)) {
1.1.1.22  root     7435:        case 0x00:
1.1       root     7436:                if(REG8(BL) != 0xff) {
                   7437:                        mode = REG8(BL);
                   7438:                }
                   7439:                REG8(AL) = mode;
                   7440:                break;
                   7441:        default:
1.1.1.22  root     7442:                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     7443:                m_CF = 1;
1.1       root     7444:                break;
                   7445:        }
                   7446: }
                   7447: 
1.1.1.22  root     7448: inline void pcbios_int_10h_83h()
                   7449: {
                   7450:        static UINT8 mode = 0;
                   7451:        
                   7452:        switch(REG8(AL)) {
                   7453:        case 0x00:
                   7454:                REG16(AX) = 0; // offset???
                   7455:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7456:                i386_load_segment_descriptor(ES);
                   7457:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7458:                break;
                   7459:        default:
                   7460:                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));
                   7461:                m_CF = 1;
                   7462:                break;
                   7463:        }
                   7464: }
                   7465: 
                   7466: inline void pcbios_int_10h_90h()
                   7467: {
                   7468:        REG8(AL) = mem[0x449];
                   7469: }
                   7470: 
                   7471: inline void pcbios_int_10h_91h()
                   7472: {
                   7473:        REG8(AL) = 0x04; // VGA
                   7474: }
                   7475: 
                   7476: inline void pcbios_int_10h_efh()
                   7477: {
                   7478:        REG16(DX) = 0xffff;
                   7479: }
                   7480: 
1.1       root     7481: inline void pcbios_int_10h_feh()
                   7482: {
                   7483:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7484:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7485:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7486:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7487:        }
                   7488:        int_10h_feh_called = true;
                   7489: }
                   7490: 
                   7491: inline void pcbios_int_10h_ffh()
                   7492: {
                   7493:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7494:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7495:                COORD co;
                   7496:                DWORD num;
                   7497:                
1.1.1.14  root     7498:                vram_flush();
                   7499:                
                   7500:                co.X = (REG16(DI) >> 1) % scr_width;
                   7501:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7502:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7503:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7504:                int len;
                   7505:                for(len = 0; ofs < end; len++) {
                   7506:                        scr_char[len] = mem[ofs++];
                   7507:                        scr_attr[len] = mem[ofs++];
                   7508:                }
                   7509:                co.Y += scr_top;
                   7510:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7511:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7512:        }
                   7513:        int_10h_ffh_called = true;
                   7514: }
                   7515: 
1.1.1.42  root     7516: int pcbios_update_drive_param(int drive_num, int force_update)
                   7517: {
                   7518:        if(drive_num >= 0 && drive_num < 26) {
                   7519:                drive_param_t *drive_param = &drive_params[drive_num];
                   7520:                
                   7521:                if(force_update || !drive_param->initialized) {
                   7522:                        drive_param->valid = 0;
                   7523:                        char dev[64];
                   7524:                        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7525:                        
                   7526:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   7527:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7528:                                DWORD dwSize;
                   7529:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
                   7530:                                        drive_param->valid = 1;
                   7531:                                }
                   7532:                                CloseHandle(hFile);
                   7533:                        }
                   7534:                        drive_param->initialized = 1;
                   7535:                }
                   7536:                return(drive_param->valid);
                   7537:        }
                   7538:        return(0);
                   7539: }
                   7540: 
                   7541: inline void pcbios_int_13h_00h()
                   7542: {
                   7543:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7544:        
                   7545:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7546:                REG8(AH) = 0x00; // successful completion
                   7547:        } else {
                   7548:                if(REG8(DL) & 0x80) {
                   7549:                        REG8(AH) = 0x05; // reset failed (hard disk)
                   7550:                } else {
                   7551:                        REG8(AH) = 0x80; // timeout (not ready)
                   7552:                }
                   7553:                m_CF = 1;
                   7554:        }
                   7555: }
                   7556: 
                   7557: inline void pcbios_int_13h_02h()
                   7558: {
                   7559:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7560:        
                   7561:        if(REG8(AL) == 0) {
                   7562:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7563:                m_CF = 1;
                   7564:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7565:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7566:                m_CF = 1;
                   7567:        } else {
                   7568:                drive_param_t *drive_param = &drive_params[drive_num];
                   7569:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7570:                char dev[64];
                   7571:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7572:                
                   7573:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7574:                if(hFile == INVALID_HANDLE_VALUE) {
                   7575:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7576:                        m_CF = 1;
                   7577:                } else {
                   7578:                        UINT32 sector_num = REG8(AL);
                   7579:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7580:                        UINT32 head = REG8(DH);
                   7581:                        UINT32 sector = REG8(CL) & 0x3f;
                   7582:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7583:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7584:                        DWORD dwSize;
                   7585:                        
                   7586: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7587: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7588: //                             m_CF = 1;
                   7589: //                     } else 
                   7590:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7591:                                REG8(AH) = 0x04; // sector not found/read error
                   7592:                                m_CF = 1;
                   7593:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7594:                                REG8(AH) = 0x04; // sector not found/read error
                   7595:                                m_CF = 1;
                   7596:                        } else {
                   7597:                                REG8(AH) = 0x00; // successful completion
                   7598:                        }
                   7599:                        CloseHandle(hFile);
                   7600:                }
                   7601:        }
                   7602: }
                   7603: 
                   7604: inline void pcbios_int_13h_03h()
                   7605: {
                   7606:        // this operation may cause serious damage for drives, so support only floppy disk...
                   7607:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7608:        
                   7609:        if(REG8(AL) == 0) {
                   7610:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7611:                m_CF = 1;
                   7612:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7613:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7614:                m_CF = 1;
                   7615:        } else if(!drive_params[drive_num].is_fdd()) {
                   7616:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7617:                m_CF = 1;
                   7618:        } else {
                   7619:                drive_param_t *drive_param = &drive_params[drive_num];
                   7620:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7621:                char dev[64];
                   7622:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7623:                
                   7624:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7625:                if(hFile == INVALID_HANDLE_VALUE) {
                   7626:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7627:                        m_CF = 1;
                   7628:                } else {
                   7629:                        UINT32 sector_num = REG8(AL);
                   7630:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7631:                        UINT32 head = REG8(DH);
                   7632:                        UINT32 sector = REG8(CL) & 0x3f;
                   7633:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7634:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7635:                        DWORD dwSize;
                   7636:                        
                   7637: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7638: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7639: //                             m_CF = 1;
                   7640: //                     } else 
                   7641:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7642:                                REG8(AH) = 0x04; // sector not found/read error
                   7643:                                m_CF = 1;
                   7644:                        } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7645:                                REG8(AH) = 0x04; // sector not found/read error
                   7646:                                m_CF = 1;
                   7647:                        } else {
                   7648:                                REG8(AH) = 0x00; // successful completion
                   7649:                        }
                   7650:                        CloseHandle(hFile);
                   7651:                }
                   7652:        }
                   7653: }
                   7654: 
                   7655: inline void pcbios_int_13h_04h()
                   7656: {
                   7657:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7658:        
                   7659:        if(REG8(AL) == 0) {
                   7660:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7661:                m_CF = 1;
                   7662:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7663:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7664:                m_CF = 1;
                   7665:        } else {
                   7666:                drive_param_t *drive_param = &drive_params[drive_num];
                   7667:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7668:                char dev[64];
                   7669:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7670:                
                   7671:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7672:                if(hFile == INVALID_HANDLE_VALUE) {
                   7673:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7674:                        m_CF = 1;
                   7675:                } else {
                   7676:                        UINT32 sector_num = REG8(AL);
                   7677:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7678:                        UINT32 head = REG8(DH);
                   7679:                        UINT32 sector = REG8(CL) & 0x3f;
                   7680:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7681:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7682:                        DWORD dwSize;
                   7683:                        UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
                   7684:                        
                   7685: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7686: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7687: //                             m_CF = 1;
                   7688: //                     } else 
                   7689:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7690:                                REG8(AH) = 0x04; // sector not found/read error
                   7691:                                m_CF = 1;
                   7692:                        } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7693:                                REG8(AH) = 0x04; // sector not found/read error
                   7694:                                m_CF = 1;
                   7695:                        } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
                   7696:                                REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
                   7697:                                m_CF = 1;
                   7698:                        } else {
                   7699:                                REG8(AH) = 0x00; // successful completion
                   7700:                        }
                   7701:                        free(tmp_buffer);
                   7702:                        CloseHandle(hFile);
                   7703:                }
                   7704:        }
                   7705: }
                   7706: 
                   7707: inline void pcbios_int_13h_08h()
                   7708: {
                   7709:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7710:        
                   7711:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7712:                drive_param_t *drive_param = &drive_params[drive_num];
                   7713:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7714:                
                   7715:                REG16(AX) = 0x0000;
                   7716:                switch(geo->MediaType) {
                   7717:                case F5_360_512:
                   7718:                case F5_320_512:
                   7719:                case F5_320_1024:
                   7720:                case F5_180_512:
                   7721:                case F5_160_512:
                   7722:                        REG8(BL) = 0x01; // 320K/360K disk
                   7723:                        break;
                   7724:                case F5_1Pt2_512:
                   7725:                case F3_1Pt2_512:
                   7726:                case F3_1Pt23_1024:
                   7727:                case F5_1Pt23_1024:
                   7728:                        REG8(BL) = 0x02; // 1.2M disk
                   7729:                        break;
                   7730:                case F3_720_512:
                   7731:                case F3_640_512:
                   7732:                case F5_640_512:
                   7733:                case F5_720_512:
                   7734:                        REG8(BL) = 0x03; // 720K disk
                   7735:                        break;
                   7736:                case F3_1Pt44_512:
                   7737:                        REG8(BL) = 0x04; // 1.44M disk
                   7738:                        break;
                   7739:                case F3_2Pt88_512:
                   7740:                        REG8(BL) = 0x06; // 2.88M disk
                   7741:                        break;
                   7742:                case RemovableMedia:
                   7743:                        REG8(BL) = 0x10; // ATAPI Removable Media Device
                   7744:                        break;
                   7745:                default:
                   7746:                        REG8(BL) = 0x00; // unknown
                   7747:                        break;
                   7748:                }
                   7749:                if(REG8(DL) & 0x80) {
                   7750:                        switch(GetLogicalDrives() & 0x0c) {
                   7751:                        case 0x00: REG8(DL) = 0x00; break;
                   7752:                        case 0x04:
                   7753:                        case 0x08: REG8(DL) = 0x01; break;
                   7754:                        case 0x0c: REG8(DL) = 0x02; break;
                   7755:                        }
                   7756:                } else {
                   7757:                        switch(GetLogicalDrives() & 0x03) {
                   7758:                        case 0x00: REG8(DL) = 0x00; break;
                   7759:                        case 0x01:
                   7760:                        case 0x02: REG8(DL) = 0x01; break;
                   7761:                        case 0x03: REG8(DL) = 0x02; break;
                   7762:                        }
                   7763:                }
                   7764:                REG8(DH) = drive_param->head_num();
                   7765:                int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
                   7766:                int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
                   7767:                REG8(CH) = cyl & 0xff;
                   7768:                REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
                   7769:        } else {
                   7770:                REG8(AH) = 0x07;
                   7771:                m_CF = 1;
                   7772:        }
                   7773: }
                   7774: 
                   7775: inline void pcbios_int_13h_10h()
                   7776: {
                   7777:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7778:        
                   7779:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7780:                REG8(AH) = 0x00; // successful completion
                   7781:        } else {
                   7782:                if(REG8(DL) & 0x80) {
                   7783:                        REG8(AH) = 0xaa; // drive not ready (hard disk)
                   7784:                } else {
                   7785:                        REG8(AH) = 0x80; // timeout (not ready)
                   7786:                }
                   7787:                m_CF = 1;
                   7788:        }
                   7789: }
                   7790: 
                   7791: inline void pcbios_int_13h_15h()
                   7792: {
                   7793:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7794:        
                   7795:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7796:                if(REG8(DL) & 0x80) {
                   7797:                        REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
                   7798:                } else {
                   7799:                        REG8(AH) = 0x03; // hard disk
                   7800:                }
                   7801:        } else {
                   7802:                REG8(AH) = 0x00; // no such drive
                   7803:        }
                   7804: }
                   7805: 
1.1.1.43  root     7806: inline void pcbios_int_13h_41h()
                   7807: {
                   7808:        if(REG16(BX) == 0x55aa) {
                   7809:                // IBM/MS INT 13 Extensions is not installed
                   7810:                REG8(AH) = 0x01;
                   7811:                m_CF = 1;
                   7812:        } else {
                   7813:                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));
                   7814:                REG8(AH) = 0x01;
                   7815:                m_CF = 1;
                   7816:        }
                   7817: }
                   7818: 
1.1.1.25  root     7819: inline void pcbios_int_14h_00h()
                   7820: {
1.1.1.29  root     7821:        if(REG16(DX) < 4) {
1.1.1.25  root     7822:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   7823:                UINT8 selector = sio_read(REG16(DX), 3);
                   7824:                selector &= ~0x3f;
                   7825:                selector |= REG8(AL) & 0x1f;
                   7826:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   7827:                sio_write(REG16(DX), 3, selector | 0x80);
                   7828:                sio_write(REG16(DX), 0, divisor & 0xff);
                   7829:                sio_write(REG16(DX), 1, divisor >> 8);
                   7830:                sio_write(REG16(DX), 3, selector);
                   7831:                REG8(AH) = sio_read(REG16(DX), 5);
                   7832:                REG8(AL) = sio_read(REG16(DX), 6);
                   7833:        } else {
                   7834:                REG8(AH) = 0x80;
                   7835:        }
                   7836: }
                   7837: 
                   7838: inline void pcbios_int_14h_01h()
                   7839: {
1.1.1.29  root     7840:        if(REG16(DX) < 4) {
1.1.1.25  root     7841:                UINT8 selector = sio_read(REG16(DX), 3);
                   7842:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7843:                sio_write(REG16(DX), 0, REG8(AL));
                   7844:                sio_write(REG16(DX), 3, selector);
                   7845:                REG8(AH) = sio_read(REG16(DX), 5);
                   7846:        } else {
                   7847:                REG8(AH) = 0x80;
                   7848:        }
                   7849: }
                   7850: 
                   7851: inline void pcbios_int_14h_02h()
                   7852: {
1.1.1.29  root     7853:        if(REG16(DX) < 4) {
1.1.1.25  root     7854:                UINT8 selector = sio_read(REG16(DX), 3);
                   7855:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7856:                REG8(AL) = sio_read(REG16(DX), 0);
                   7857:                sio_write(REG16(DX), 3, selector);
                   7858:                REG8(AH) = sio_read(REG16(DX), 5);
                   7859:        } else {
                   7860:                REG8(AH) = 0x80;
                   7861:        }
                   7862: }
                   7863: 
                   7864: inline void pcbios_int_14h_03h()
                   7865: {
1.1.1.29  root     7866:        if(REG16(DX) < 4) {
1.1.1.25  root     7867:                REG8(AH) = sio_read(REG16(DX), 5);
                   7868:                REG8(AL) = sio_read(REG16(DX), 6);
                   7869:        } else {
                   7870:                REG8(AH) = 0x80;
                   7871:        }
                   7872: }
                   7873: 
                   7874: inline void pcbios_int_14h_04h()
                   7875: {
1.1.1.29  root     7876:        if(REG16(DX) < 4) {
1.1.1.25  root     7877:                UINT8 selector = sio_read(REG16(DX), 3);
                   7878:                if(REG8(CH) <= 0x03) {
                   7879:                        selector = (selector & ~0x03) | REG8(CH);
                   7880:                }
                   7881:                if(REG8(BL) == 0x00) {
                   7882:                        selector &= ~0x04;
                   7883:                } else if(REG8(BL) == 0x01) {
                   7884:                        selector |= 0x04;
                   7885:                }
                   7886:                if(REG8(BH) == 0x00) {
                   7887:                        selector = (selector & ~0x38) | 0x00;
                   7888:                } else if(REG8(BH) == 0x01) {
                   7889:                        selector = (selector & ~0x38) | 0x08;
                   7890:                } else if(REG8(BH) == 0x02) {
                   7891:                        selector = (selector & ~0x38) | 0x18;
                   7892:                } else if(REG8(BH) == 0x03) {
                   7893:                        selector = (selector & ~0x38) | 0x28;
                   7894:                } else if(REG8(BH) == 0x04) {
                   7895:                        selector = (selector & ~0x38) | 0x38;
                   7896:                }
                   7897:                if(REG8(AL) == 0x00) {
                   7898:                        selector |= 0x40;
                   7899:                } else if(REG8(AL) == 0x01) {
                   7900:                        selector &= ~0x40;
                   7901:                }
                   7902:                if(REG8(CL) <= 0x0b) {
                   7903:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   7904:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   7905:                        sio_write(REG16(DX), 3, selector | 0x80);
                   7906:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   7907:                        sio_write(REG16(DX), 1, divisor >> 8);
                   7908:                }
                   7909:                sio_write(REG16(DX), 3, selector);
                   7910:                REG8(AH) = sio_read(REG16(DX), 5);
                   7911:                REG8(AL) = sio_read(REG16(DX), 6);
                   7912:        } else {
                   7913:                REG8(AH) = 0x80;
                   7914:        }
                   7915: }
                   7916: 
                   7917: inline void pcbios_int_14h_05h()
                   7918: {
1.1.1.29  root     7919:        if(REG16(DX) < 4) {
1.1.1.25  root     7920:                if(REG8(AL) == 0x00) {
                   7921:                        REG8(BL) = sio_read(REG16(DX), 4);
                   7922:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7923:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7924:                } else if(REG8(AL) == 0x01) {
                   7925:                        sio_write(REG16(DX), 4, REG8(BL));
                   7926:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7927:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7928:                } else {
                   7929:                        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));
                   7930:                }
                   7931:        } else {
                   7932:                REG8(AH) = 0x80;
                   7933:        }
                   7934: }
                   7935: 
1.1.1.14  root     7936: inline void pcbios_int_15h_10h()
                   7937: {
1.1.1.22  root     7938:        switch(REG8(AL)) {
                   7939:        case 0x00:
1.1.1.14  root     7940:                Sleep(10);
1.1.1.35  root     7941:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     7942:                break;
                   7943:        default:
                   7944:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.14  root     7945:                REG8(AH) = 0x86;
                   7946:                m_CF = 1;
                   7947:        }
                   7948: }
                   7949: 
1.1       root     7950: inline void pcbios_int_15h_23h()
                   7951: {
                   7952:        switch(REG8(AL)) {
1.1.1.22  root     7953:        case 0x00:
1.1.1.8   root     7954:                REG8(CL) = cmos_read(0x2d);
                   7955:                REG8(CH) = cmos_read(0x2e);
1.1       root     7956:                break;
1.1.1.22  root     7957:        case 0x01:
1.1.1.8   root     7958:                cmos_write(0x2d, REG8(CL));
                   7959:                cmos_write(0x2e, REG8(CH));
1.1       root     7960:                break;
                   7961:        default:
1.1.1.22  root     7962:                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     7963:                REG8(AH) = 0x86;
1.1.1.3   root     7964:                m_CF = 1;
1.1       root     7965:                break;
                   7966:        }
                   7967: }
                   7968: 
                   7969: inline void pcbios_int_15h_24h()
                   7970: {
                   7971:        switch(REG8(AL)) {
1.1.1.22  root     7972:        case 0x00:
1.1.1.3   root     7973:                i386_set_a20_line(0);
1.1       root     7974:                REG8(AH) = 0;
                   7975:                break;
1.1.1.22  root     7976:        case 0x01:
1.1.1.3   root     7977:                i386_set_a20_line(1);
1.1       root     7978:                REG8(AH) = 0;
                   7979:                break;
1.1.1.22  root     7980:        case 0x02:
1.1       root     7981:                REG8(AH) = 0;
1.1.1.3   root     7982:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     7983:                REG16(CX) = 0;
                   7984:                break;
1.1.1.22  root     7985:        case 0x03:
1.1       root     7986:                REG16(AX) = 0;
                   7987:                REG16(BX) = 0;
                   7988:                break;
1.1.1.22  root     7989:        default:
                   7990:                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));
                   7991:                REG8(AH) = 0x86;
                   7992:                m_CF = 1;
                   7993:                break;
1.1       root     7994:        }
                   7995: }
                   7996: 
                   7997: inline void pcbios_int_15h_49h()
                   7998: {
1.1.1.27  root     7999:        REG8(AH) = 0x00;
                   8000:        REG8(BL) = 0x00; // DOS/V
1.1       root     8001: }
                   8002: 
1.1.1.22  root     8003: inline void pcbios_int_15h_50h()
                   8004: {
                   8005:        switch(REG8(AL)) {
                   8006:        case 0x00:
                   8007:        case 0x01:
                   8008:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   8009:                        REG8(AH) = 0x01; // invalid font type in bh
                   8010:                        m_CF = 1;
1.1.1.27  root     8011:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     8012:                        REG8(AH) = 0x02; // bl not zero
                   8013:                        m_CF = 1;
                   8014:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   8015:                        REG8(AH) = 0x04; // invalid code page
                   8016:                        m_CF = 1;
1.1.1.27  root     8017:                } else if(REG8(AL) == 0x01) {
                   8018:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     8019:                        m_CF = 1;
1.1.1.27  root     8020:                } else {
1.1.1.49  root     8021:                        // dummy font read routine is at fffc:000d
                   8022:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.27  root     8023:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     8024:                        REG16(BX) = 0x000d;
1.1.1.27  root     8025:                        REG8(AH) = 0x00; // success
1.1.1.22  root     8026:                }
                   8027:                break;
                   8028:        default:
                   8029:                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));
                   8030:                REG8(AH) = 0x86;
                   8031:                m_CF = 1;
                   8032:                break;
                   8033:        }
                   8034: }
                   8035: 
1.1.1.30  root     8036: inline void pcbios_int_15h_53h()
                   8037: {
                   8038:        switch(REG8(AL)) {
                   8039:        case 0x00:
                   8040:                // APM is not installed
                   8041:                REG8(AH) = 0x86;
                   8042:                m_CF = 1;
                   8043:                break;
                   8044:        default:
                   8045:                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));
                   8046:                REG8(AH) = 0x86;
                   8047:                m_CF = 1;
                   8048:                break;
                   8049:        }
                   8050: }
                   8051: 
1.1.1.43  root     8052: inline void pcbios_int_15h_84h()
                   8053: {
                   8054:        // joystick support (from DOSBox)
                   8055:        switch(REG16(DX)) {
                   8056:        case 0x00:
                   8057:                REG16(AX) = 0x00f0;
                   8058:                REG16(DX) = 0x0201;
                   8059:                m_CF = 1;
                   8060:                break;
                   8061:        case 0x01:
                   8062:                REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   8063:                m_CF = 1;
                   8064:                break;
                   8065:        default:
                   8066:                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));
                   8067:                REG8(AH) = 0x86;
                   8068:                m_CF = 1;
                   8069:                break;
                   8070:        }
                   8071: }
1.1.1.35  root     8072: 
                   8073: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     8074: {
                   8075:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     8076:        UINT32 msec = usec / 1000;
                   8077:        
1.1.1.35  root     8078:        while(msec && !m_halted) {
1.1.1.14  root     8079:                UINT32 tmp = min(msec, 100);
                   8080:                if(msec - tmp < 10) {
                   8081:                        tmp = msec;
                   8082:                }
                   8083:                Sleep(tmp);
                   8084:                msec -= tmp;
                   8085:        }
1.1.1.35  root     8086:        
                   8087: #ifdef USE_SERVICE_THREAD
                   8088:        service_exit = true;
                   8089: #endif
                   8090:        return(0);
                   8091: }
                   8092: 
                   8093: inline void pcbios_int_15h_86h()
                   8094: {
                   8095:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   8096: #ifdef USE_SERVICE_THREAD
                   8097:                start_service_loop(pcbios_int_15h_86h_thread);
                   8098: #else
                   8099:                pcbios_int_15h_86h_thread(NULL);
                   8100:                REQUEST_HARDWRE_UPDATE();
                   8101: #endif
                   8102:        }
1.1       root     8103: }
                   8104: 
                   8105: inline void pcbios_int_15h_87h()
                   8106: {
                   8107:        // copy extended memory (from DOSBox)
                   8108:        int len = REG16(CX) * 2;
1.1.1.3   root     8109:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     8110:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   8111:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   8112:        memcpy(mem + dst, mem + src, len);
                   8113:        REG16(AX) = 0x00;
                   8114: }
                   8115: 
                   8116: inline void pcbios_int_15h_88h()
                   8117: {
1.1.1.17  root     8118:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     8119: }
                   8120: 
                   8121: inline void pcbios_int_15h_89h()
                   8122: {
1.1.1.21  root     8123: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     8124:        // switch to protected mode (from DOSBox)
                   8125:        write_io_byte(0x20, 0x10);
                   8126:        write_io_byte(0x21, REG8(BH));
                   8127:        write_io_byte(0x21, 0x00);
                   8128:        write_io_byte(0xa0, 0x10);
                   8129:        write_io_byte(0xa1, REG8(BL));
                   8130:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     8131:        i386_set_a20_line(1);
                   8132:        int ofs = SREG_BASE(ES) + REG16(SI);
                   8133:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   8134:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   8135:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   8136:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   8137: #if defined(HAS_I386)
                   8138:        m_cr[0] |= 1;
                   8139: #else
                   8140:        m_msw |= 1;
                   8141: #endif
                   8142:        SREG(DS) = 0x18;
                   8143:        SREG(ES) = 0x20;
                   8144:        SREG(SS) = 0x28;
                   8145:        i386_load_segment_descriptor(DS);
                   8146:        i386_load_segment_descriptor(ES);
                   8147:        i386_load_segment_descriptor(SS);
1.1.1.21  root     8148:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     8149:        REG16(SP) += 6;
1.1.1.3   root     8150: #if defined(HAS_I386)
1.1.1.21  root     8151:        UINT32 flags = get_flags();
                   8152:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8153:        set_flags(flags);
1.1.1.3   root     8154: #else
1.1.1.21  root     8155:        UINT32 flags = CompressFlags();
                   8156:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8157:        ExpandFlags(flags);
1.1.1.3   root     8158: #endif
1.1       root     8159:        REG16(AX) = 0x00;
1.1.1.21  root     8160:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     8161: #else
1.1.1.21  root     8162:        // i86/i186/v30: protected mode is not supported
1.1       root     8163:        REG8(AH) = 0x86;
1.1.1.3   root     8164:        m_CF = 1;
1.1       root     8165: #endif
                   8166: }
                   8167: 
1.1.1.21  root     8168: inline void pcbios_int_15h_8ah()
                   8169: {
                   8170:        UINT32 size = MAX_MEM - 0x100000;
                   8171:        REG16(AX) = size & 0xffff;
                   8172:        REG16(DX) = size >> 16;
                   8173: }
                   8174: 
1.1.1.3   root     8175: #if defined(HAS_I386)
1.1       root     8176: inline void pcbios_int_15h_c9h()
                   8177: {
                   8178:        REG8(AH) = 0x00;
                   8179:        REG8(CH) = cpu_type;
                   8180:        REG8(CL) = cpu_step;
                   8181: }
1.1.1.3   root     8182: #endif
1.1       root     8183: 
                   8184: inline void pcbios_int_15h_cah()
                   8185: {
                   8186:        switch(REG8(AL)) {
1.1.1.22  root     8187:        case 0x00:
1.1       root     8188:                if(REG8(BL) > 0x3f) {
                   8189:                        REG8(AH) = 0x03;
1.1.1.3   root     8190:                        m_CF = 1;
1.1       root     8191:                } else if(REG8(BL) < 0x0e) {
                   8192:                        REG8(AH) = 0x04;
1.1.1.3   root     8193:                        m_CF = 1;
1.1       root     8194:                } else {
1.1.1.8   root     8195:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     8196:                }
                   8197:                break;
1.1.1.22  root     8198:        case 0x01:
1.1       root     8199:                if(REG8(BL) > 0x3f) {
                   8200:                        REG8(AH) = 0x03;
1.1.1.3   root     8201:                        m_CF = 1;
1.1       root     8202:                } else if(REG8(BL) < 0x0e) {
                   8203:                        REG8(AH) = 0x04;
1.1.1.3   root     8204:                        m_CF = 1;
1.1       root     8205:                } else {
1.1.1.8   root     8206:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     8207:                }
                   8208:                break;
                   8209:        default:
1.1.1.22  root     8210:                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     8211:                REG8(AH) = 0x86;
1.1.1.3   root     8212:                m_CF = 1;
1.1       root     8213:                break;
                   8214:        }
                   8215: }
                   8216: 
1.1.1.22  root     8217: inline void pcbios_int_15h_e8h()
1.1.1.17  root     8218: {
1.1.1.22  root     8219:        switch(REG8(AL)) {
                   8220: #if defined(HAS_I386)
                   8221:        case 0x01:
                   8222:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   8223:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8224:                break;
1.1.1.17  root     8225: #endif
1.1.1.22  root     8226:        default:
                   8227:                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));
                   8228:                REG8(AH) = 0x86;
                   8229:                m_CF = 1;
                   8230:                break;
                   8231:        }
                   8232: }
1.1.1.17  root     8233: 
1.1.1.33  root     8234: void pcbios_update_key_code(bool wait)
1.1       root     8235: {
1.1.1.32  root     8236:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8237: #ifdef USE_SERVICE_THREAD
                   8238:                EnterCriticalSection(&key_buf_crit_sect);
                   8239: #endif
                   8240:                bool empty = key_buf_char->empty();
                   8241: #ifdef USE_SERVICE_THREAD
                   8242:                LeaveCriticalSection(&key_buf_crit_sect);
                   8243: #endif
                   8244:                if(empty) {
1.1.1.32  root     8245:                        if(!update_key_buffer()) {
1.1.1.33  root     8246:                                if(wait) {
1.1.1.32  root     8247:                                        Sleep(10);
                   8248:                                } else {
                   8249:                                        maybe_idle();
                   8250:                                }
1.1.1.14  root     8251:                        }
                   8252:                }
1.1.1.34  root     8253:        }
                   8254:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8255: #ifdef USE_SERVICE_THREAD
                   8256:                EnterCriticalSection(&key_buf_crit_sect);
                   8257: #endif
1.1.1.32  root     8258:                if(key_buf_char->count() != 0) {
1.1.1.41  root     8259:                        int key_char = key_buf_char->read();
                   8260:                        int key_scan = key_buf_scan->read();
                   8261:                        key_code  = key_char << 0;
                   8262:                        key_code |= key_scan << 8;
1.1.1.35  root     8263:                        key_recv  = 0x0000ffff;
1.1.1.41  root     8264:                        // write to bottom of key buffer
                   8265:                        mem[0x43c] = (UINT8)key_char;
                   8266:                        mem[0x43d] = (UINT8)key_scan;
1.1.1.32  root     8267:                }
                   8268:                if(key_buf_char->count() != 0) {
1.1.1.41  root     8269:                        int key_char = key_buf_char->read();
                   8270:                        int key_scan = key_buf_scan->read();
                   8271:                        key_code |= key_char << 16;
                   8272:                        key_code |= key_scan << 24;
1.1.1.33  root     8273:                        key_recv |= 0xffff0000;
1.1.1.41  root     8274:                        // write to bottom of key buffer
                   8275:                        mem[0x43c] = (UINT8)key_char;
                   8276:                        mem[0x43d] = (UINT8)key_scan;
1.1.1.32  root     8277:                }
1.1.1.35  root     8278: #ifdef USE_SERVICE_THREAD
                   8279:                LeaveCriticalSection(&key_buf_crit_sect);
                   8280: #endif
1.1       root     8281:        }
                   8282: }
                   8283: 
1.1.1.35  root     8284: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     8285: {
1.1.1.33  root     8286:        while(key_recv == 0 && !m_halted) {
                   8287:                pcbios_update_key_code(true);
1.1       root     8288:        }
1.1.1.33  root     8289:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8290:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   8291:                        if(REG8(AH) == 0x10) {
                   8292:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   8293:                        } else {
                   8294:                                key_code = ((key_code >> 16) & 0xff00);
                   8295:                        }
                   8296:                        key_recv >>= 16;
1.1       root     8297:                }
                   8298:        }
                   8299:        REG16(AX) = key_code & 0xffff;
                   8300:        key_code >>= 16;
1.1.1.33  root     8301:        key_recv >>= 16;
1.1.1.35  root     8302:        
                   8303: #ifdef USE_SERVICE_THREAD
                   8304:        service_exit = true;
                   8305: #endif
                   8306:        return(0);
                   8307: }
                   8308: 
                   8309: inline void pcbios_int_16h_00h()
                   8310: {
                   8311: #ifdef USE_SERVICE_THREAD
                   8312:        start_service_loop(pcbios_int_16h_00h_thread);
                   8313: #else
                   8314:        pcbios_int_16h_00h_thread(NULL);
                   8315:        REQUEST_HARDWRE_UPDATE();
                   8316: #endif
1.1       root     8317: }
                   8318: 
                   8319: inline void pcbios_int_16h_01h()
                   8320: {
1.1.1.33  root     8321:        if(key_recv == 0) {
                   8322:                pcbios_update_key_code(false);
1.1.1.5   root     8323:        }
1.1.1.33  root     8324:        if(key_recv != 0) {
                   8325:                UINT32 key_code_tmp = key_code;
                   8326:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8327:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   8328:                                if(REG8(AH) == 0x11) {
                   8329:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   8330:                                } else {
                   8331:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   8332:                                }
                   8333:                        }
1.1       root     8334:                }
1.1.1.5   root     8335:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     8336: #if defined(HAS_I386)
1.1.1.33  root     8337:                m_ZF = 0;
                   8338: #else
                   8339:                m_ZeroVal = 1;
                   8340: #endif
                   8341:        } else {
                   8342: #if defined(HAS_I386)
                   8343:                m_ZF = 1;
1.1.1.3   root     8344: #else
1.1.1.33  root     8345:                m_ZeroVal = 0;
1.1.1.3   root     8346: #endif
1.1.1.33  root     8347:        }
1.1       root     8348: }
                   8349: 
                   8350: inline void pcbios_int_16h_02h()
                   8351: {
                   8352:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   8353:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   8354:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   8355:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   8356:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   8357:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   8358:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   8359:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   8360: }
                   8361: 
                   8362: inline void pcbios_int_16h_03h()
                   8363: {
                   8364:        static UINT16 status = 0;
                   8365:        
                   8366:        switch(REG8(AL)) {
                   8367:        case 0x05:
                   8368:                status = REG16(BX);
                   8369:                break;
                   8370:        case 0x06:
                   8371:                REG16(BX) = status;
                   8372:                break;
                   8373:        default:
1.1.1.3   root     8374:                m_CF = 1;
1.1       root     8375:                break;
                   8376:        }
                   8377: }
                   8378: 
                   8379: inline void pcbios_int_16h_05h()
                   8380: {
1.1.1.32  root     8381:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8382: #ifdef USE_SERVICE_THREAD
                   8383:                EnterCriticalSection(&key_buf_crit_sect);
                   8384: #endif
1.1.1.32  root     8385:                key_buf_char->write(REG8(CL));
                   8386:                key_buf_scan->write(REG8(CH));
1.1.1.35  root     8387: #ifdef USE_SERVICE_THREAD
                   8388:                LeaveCriticalSection(&key_buf_crit_sect);
                   8389: #endif
1.1.1.32  root     8390:        }
1.1       root     8391:        REG8(AL) = 0x00;
                   8392: }
                   8393: 
                   8394: inline void pcbios_int_16h_12h()
                   8395: {
                   8396:        pcbios_int_16h_02h();
                   8397:        
                   8398:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   8399:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   8400:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   8401:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   8402:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   8403:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   8404:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   8405:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   8406: }
                   8407: 
                   8408: inline void pcbios_int_16h_13h()
                   8409: {
                   8410:        static UINT16 status = 0;
                   8411:        
                   8412:        switch(REG8(AL)) {
                   8413:        case 0x00:
                   8414:                status = REG16(DX);
                   8415:                break;
                   8416:        case 0x01:
                   8417:                REG16(DX) = status;
                   8418:                break;
                   8419:        default:
1.1.1.22  root     8420:                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     8421:                m_CF = 1;
1.1       root     8422:                break;
                   8423:        }
                   8424: }
                   8425: 
                   8426: inline void pcbios_int_16h_14h()
                   8427: {
                   8428:        static UINT8 status = 0;
                   8429:        
                   8430:        switch(REG8(AL)) {
                   8431:        case 0x00:
                   8432:        case 0x01:
                   8433:                status = REG8(AL);
                   8434:                break;
                   8435:        case 0x02:
                   8436:                REG8(AL) = status;
                   8437:                break;
                   8438:        default:
1.1.1.22  root     8439:                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     8440:                m_CF = 1;
1.1       root     8441:                break;
                   8442:        }
                   8443: }
                   8444: 
1.1.1.24  root     8445: inline void pcbios_int_16h_55h()
                   8446: {
                   8447:        switch(REG8(AL)) {
                   8448:        case 0x00:
                   8449:                // keyboard tsr is not present
                   8450:                break;
                   8451:        case 0xfe:
                   8452:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   8453:                break;
                   8454:        case 0xff:
                   8455:                break;
                   8456:        default:
                   8457:                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));
                   8458:                m_CF = 1;
                   8459:                break;
                   8460:        }
                   8461: }
                   8462: 
1.1.1.30  root     8463: inline void pcbios_int_16h_6fh()
                   8464: {
                   8465:        switch(REG8(AL)) {
                   8466:        case 0x00:
                   8467:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   8468:                break;
                   8469:        default:
                   8470:                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));
                   8471:                m_CF = 1;
                   8472:                break;
                   8473:        }
                   8474: }
                   8475: 
1.1.1.37  root     8476: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   8477: {
                   8478:        UINT8 hi = jis >> 8;
                   8479:        UINT8 lo = jis & 0xff;
                   8480:        
                   8481:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   8482:        hi = (hi - 0x21) / 2 + 0x81;
                   8483:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   8484:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   8485:        
                   8486:        return((hi << 8) + lo);
                   8487: }
                   8488: 
                   8489: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   8490: {
                   8491:        UINT8 hi = sjis >> 8;
                   8492:        UINT8 lo = sjis & 0xff;
                   8493:        
                   8494:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   8495:                return(0x2121);
                   8496:        }
                   8497:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   8498:                return(0x2121);
                   8499:        }
                   8500:        if(hi >= 0xf0 && hi <= 0xf3) {
                   8501:                // gaiji
                   8502:                if(lo >= 0x40 && lo <= 0x7e) {
                   8503:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   8504:                }
                   8505:                if(lo >= 0x80 && lo <= 0x9e) {
                   8506:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   8507:                }
                   8508:                if(lo >= 0x9f && lo <= 0xfc) {
                   8509:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   8510:                }
                   8511:        }
                   8512:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   8513:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   8514:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   8515:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   8516:        
                   8517:        return((hi << 8) + lo);
                   8518: }
                   8519: 
1.1.1.38  root     8520: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   8521: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   8522: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     8523: 
                   8524: void pcbios_printer_out(int c, UINT8 data)
                   8525: {
                   8526:        if(pio[c].conv_mode) {
                   8527:                if(pio[c].sjis_hi != 0) {
                   8528:                        if(!pio[c].jis_mode) {
                   8529:                                printer_out(c, 0x1c);
                   8530:                                printer_out(c, 0x26);
                   8531:                                pio[c].jis_mode = true;
                   8532:                        }
                   8533:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   8534:                        printer_out(c, jis >> 8);
                   8535:                        printer_out(c, jis & 0xff);
                   8536:                        pio[c].sjis_hi = 0;
                   8537:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   8538:                        printer_out(c, data);
                   8539:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8540:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8541:                        }
                   8542:                        pio[c].esc_len++;
                   8543:                        
                   8544:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8545:                        case 0x33: // 1Bh 33h XX
                   8546:                        case 0x4a: // 1Bh 4Ah XX
                   8547:                        case 0x4e: // 1Bh 4Eh XX
                   8548:                        case 0x51: // 1Bh 51h XX
                   8549:                        case 0x55: // 1Bh 55h XX
                   8550:                        case 0x6c: // 1Bh 6Ch XX
                   8551:                        case 0x71: // 1Bh 71h XX
                   8552:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     8553:                                if(pio[c].esc_len == 3) {
                   8554:                                        pio[c].esc_buf[0] = 0x00;
                   8555:                                }
                   8556:                                break;
1.1.1.38  root     8557:                        case 0x24: // 1Bh 24h XX XX
                   8558:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     8559:                                if(pio[c].esc_len == 4) {
                   8560:                                        pio[c].esc_buf[0] = 0x00;
                   8561:                                }
                   8562:                                break;
1.1.1.38  root     8563:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     8564:                                if(pio[c].esc_len >= 3) {
                   8565:                                        switch(pio[c].esc_buf[2]) {
                   8566:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   8567:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   8568:                                                        pio[c].esc_buf[0] = 0x00;
                   8569:                                                }
                   8570:                                                break;
                   8571:                                        case 32: case 33: case 38: case 39: case 40:
                   8572:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   8573:                                                        pio[c].esc_buf[0] = 0x00;
                   8574:                                                }
                   8575:                                                break;
1.1.1.38  root     8576:                                        case 71: case 72: case 73:
                   8577:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   8578:                                                        pio[c].esc_buf[0] = 0x00;
                   8579:                                                }
                   8580:                                                break;
1.1.1.37  root     8581:                                        default:
                   8582:                                                pio[c].esc_buf[0] = 0x00;
                   8583:                                                break;
                   8584:                                        }
                   8585:                                }
                   8586:                                break;
1.1.1.38  root     8587:                        case 0x40: // 1Bh 40h
1.1.1.37  root     8588:                                if(pio[c].jis_mode) {
                   8589:                                        printer_out(c, 0x1c);
                   8590:                                        printer_out(c, 0x2e);
                   8591:                                        pio[c].jis_mode = false;
                   8592:                                }
                   8593:                                pio[c].esc_buf[0] = 0x00;
                   8594:                                break;
1.1.1.38  root     8595:                        case 0x42: // 1Bh 42h data 00h
                   8596:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8597:                                if(pio[c].esc_len >= 3 && data == 0) {
                   8598:                                        pio[c].esc_buf[0] = 0x00;
                   8599:                                }
                   8600:                                break;
1.1.1.38  root     8601:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     8602:                                if(pio[c].esc_len >= 3 && data != 0) {
                   8603:                                        pio[c].esc_buf[0] = 0x00;
                   8604:                                }
                   8605:                                break;
1.1.1.38  root     8606:                        default: // 1Bh XX
1.1.1.37  root     8607:                                pio[c].esc_buf[0] = 0x00;
                   8608:                                break;
                   8609:                        }
                   8610:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   8611:                        printer_out(c, data);
                   8612:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8613:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8614:                        }
                   8615:                        pio[c].esc_len++;
                   8616:                        
                   8617:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8618:                        case 0x21: // 1Ch 21h XX
                   8619:                        case 0x2d: // 1Ch 2Dh XX
                   8620:                        case 0x57: // 1Ch 57h XX
                   8621:                        case 0x6b: // 1Ch 6Bh XX
                   8622:                        case 0x72: // 1Ch 72h XX
                   8623:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     8624:                                if(pio[c].esc_len == 3) {
                   8625:                                        pio[c].esc_buf[0] = 0x00;
                   8626:                                }
                   8627:                                break;
1.1.1.38  root     8628:                        case 0x26: // 1Ch 26h
1.1.1.37  root     8629:                                pio[c].jis_mode = true;
                   8630:                                pio[c].esc_buf[0] = 0x00;
                   8631:                                break;
1.1.1.38  root     8632:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     8633:                                pio[c].jis_mode = false;
                   8634:                                pio[c].esc_buf[0] = 0x00;
                   8635:                                break;
1.1.1.38  root     8636:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     8637:                                if(pio[c].esc_len == 76) {
                   8638:                                        pio[c].esc_buf[0] = 0x00;
                   8639:                                }
                   8640:                                break;
1.1.1.38  root     8641:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8642:                                if(pio[c].esc_len == 6) {
                   8643:                                        pio[c].esc_buf[0] = 0x00;
                   8644:                                }
                   8645:                                break;
1.1.1.38  root     8646:                        case 0x53: // 1Ch 53h XX XX
                   8647:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     8648:                                if(pio[c].esc_len == 4) {
                   8649:                                        pio[c].esc_buf[0] = 0x00;
                   8650:                                }
                   8651:                                break;
1.1.1.38  root     8652:                        default: // 1Ch XX
1.1.1.37  root     8653:                                pio[c].esc_buf[0] = 0x00;
                   8654:                                break;
                   8655:                        }
                   8656:                } else if(data == 0x1b || data == 0x1c) {
                   8657:                        printer_out(c, data);
                   8658:                        pio[c].esc_buf[0] = data;
                   8659:                        pio[c].esc_len = 1;
                   8660:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   8661:                        pio[c].sjis_hi = data;
                   8662:                } else {
                   8663:                        if(pio[c].jis_mode) {
                   8664:                                printer_out(c, 0x1c);
                   8665:                                printer_out(c, 0x2e);
                   8666:                                pio[c].jis_mode = false;
                   8667:                        }
                   8668:                        printer_out(c, data);
                   8669:                }
                   8670:        } else {
                   8671:                if(pio[c].jis_mode) {
                   8672:                        printer_out(c, 0x1c);
                   8673:                        printer_out(c, 0x2e);
                   8674:                        pio[c].jis_mode = false;
                   8675:                }
                   8676:                printer_out(c, data);
                   8677:        }
                   8678: }
                   8679: 
                   8680: inline void pcbios_int_17h_00h()
                   8681: {
                   8682:        if(REG16(DX) < 3) {
                   8683:                pcbios_printer_out(REG16(DX), REG8(AL));
                   8684:                REG8(AH) = 0xd0;
                   8685:        }
                   8686: }
                   8687: 
                   8688: inline void pcbios_int_17h_01h()
                   8689: {
                   8690:        if(REG16(DX) < 3) {
                   8691:                REG8(AH) = 0xd0;
                   8692:        }
                   8693: }
                   8694: 
                   8695: inline void pcbios_int_17h_02h()
                   8696: {
                   8697:        if(REG16(DX) < 3) {
                   8698:                REG8(AH) = 0xd0;
                   8699:        }
                   8700: }
                   8701: 
                   8702: inline void pcbios_int_17h_03h()
                   8703: {
                   8704:        switch(REG8(AL)) {
                   8705:        case 0x00:
                   8706:                if(REG16(DX) < 3) {
                   8707:                        if(pio[REG16(DX)].jis_mode) {
                   8708:                                printer_out(REG16(DX), 0x1c);
                   8709:                                printer_out(REG16(DX), 0x2e);
                   8710:                                pio[REG16(DX)].jis_mode = false;
                   8711:                        }
                   8712:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   8713:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   8714:                        }
                   8715:                        REG16(CX) = 0x0000;
                   8716:                        REG8(AH) = 0xd0;
                   8717:                }
                   8718:                break;
                   8719:        default:
                   8720:                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));
                   8721:                break;
                   8722:        }
                   8723: }
                   8724: 
                   8725: inline void pcbios_int_17h_50h()
                   8726: {
                   8727:        switch(REG8(AL)) {
                   8728:        case 0x00:
                   8729:                if(REG16(DX) < 3) {
                   8730:                        if(REG16(BX) = 0x0001) {
                   8731:                                pio[REG16(DX)].conv_mode = false;
                   8732:                                REG8(AL) = 0x00;
                   8733:                        } else if(REG16(BX) = 0x0051) {
                   8734:                                pio[REG16(DX)].conv_mode = true;
                   8735:                                REG8(AL) = 0x00;
                   8736:                        } else {
                   8737:                                REG8(AL) = 0x01;
                   8738:                        }
                   8739:                } else {
                   8740:                        REG8(AL) = 0x02;
                   8741:                }
                   8742:                break;
                   8743:        case 0x01:
                   8744:                if(REG16(DX) < 3) {
                   8745:                        if(pio[REG16(DX)].conv_mode) {
                   8746:                                REG16(BX) = 0x0051;
                   8747:                        } else {
                   8748:                                REG16(BX) = 0x0001;
                   8749:                        }
                   8750:                        REG8(AL) = 0x00;
                   8751:                } else {
                   8752:                        REG8(AL) = 0x02;
                   8753:                }
                   8754:                break;
                   8755:        default:
                   8756:                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));
                   8757:                break;
                   8758:        }
                   8759: }
                   8760: 
                   8761: inline void pcbios_int_17h_51h()
                   8762: {
                   8763:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   8764:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   8765:        } else {
                   8766:                REG16(DX) = 0x0000;
                   8767:        }
                   8768: }
                   8769: 
                   8770: inline void pcbios_int_17h_52h()
                   8771: {
                   8772:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   8773:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   8774:        } else {
                   8775:                REG16(DX) = 0x0000;
                   8776:        }
                   8777: }
                   8778: 
                   8779: inline void pcbios_int_17h_84h()
                   8780: {
                   8781:        if(REG16(DX) < 3) {
                   8782:                if(pio[REG16(DX)].jis_mode) {
                   8783:                        printer_out(REG16(DX), 0x1c);
                   8784:                        printer_out(REG16(DX), 0x2e);
                   8785:                        pio[REG16(DX)].jis_mode = false;
                   8786:                }
                   8787:                printer_out(REG16(DX), REG8(AL));
                   8788:                REG8(AH) = 0xd0;
                   8789:        }
                   8790: }
                   8791: 
                   8792: inline void pcbios_int_17h_85h()
                   8793: {
                   8794:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   8795: }
                   8796: 
1.1       root     8797: inline void pcbios_int_1ah_00h()
                   8798: {
1.1.1.19  root     8799:        pcbios_update_daily_timer_counter(timeGetTime());
                   8800:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   8801:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   8802:        REG8(AL) = mem[0x470];
                   8803:        mem[0x470] = 0;
1.1       root     8804: }
                   8805: 
                   8806: inline int to_bcd(int t)
                   8807: {
                   8808:        int u = (t % 100) / 10;
                   8809:        return (u << 4) | (t % 10);
                   8810: }
                   8811: 
                   8812: inline void pcbios_int_1ah_02h()
                   8813: {
                   8814:        SYSTEMTIME time;
                   8815:        
                   8816:        GetLocalTime(&time);
                   8817:        REG8(CH) = to_bcd(time.wHour);
                   8818:        REG8(CL) = to_bcd(time.wMinute);
                   8819:        REG8(DH) = to_bcd(time.wSecond);
                   8820:        REG8(DL) = 0x00;
                   8821: }
                   8822: 
                   8823: inline void pcbios_int_1ah_04h()
                   8824: {
                   8825:        SYSTEMTIME time;
                   8826:        
                   8827:        GetLocalTime(&time);
                   8828:        REG8(CH) = to_bcd(time.wYear / 100);
                   8829:        REG8(CL) = to_bcd(time.wYear);
                   8830:        REG8(DH) = to_bcd(time.wMonth);
                   8831:        REG8(DL) = to_bcd(time.wDay);
                   8832: }
                   8833: 
                   8834: inline void pcbios_int_1ah_0ah()
                   8835: {
                   8836:        SYSTEMTIME time;
                   8837:        FILETIME file_time;
                   8838:        WORD dos_date, dos_time;
                   8839:        
                   8840:        GetLocalTime(&time);
                   8841:        SystemTimeToFileTime(&time, &file_time);
                   8842:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   8843:        REG16(CX) = dos_date;
                   8844: }
                   8845: 
                   8846: // msdos system call
                   8847: 
1.1.1.43  root     8848: inline void msdos_int_21h_56h(int lfn);
                   8849: 
1.1       root     8850: inline void msdos_int_21h_00h()
                   8851: {
1.1.1.3   root     8852:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     8853: }
                   8854: 
1.1.1.35  root     8855: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     8856: {
                   8857:        REG8(AL) = msdos_getche();
1.1.1.33  root     8858:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8859:        
1.1.1.35  root     8860: #ifdef USE_SERVICE_THREAD
                   8861:        service_exit = true;
                   8862: #endif
                   8863:        return(0);
                   8864: }
                   8865: 
                   8866: inline void msdos_int_21h_01h()
                   8867: {
                   8868: #ifdef USE_SERVICE_THREAD
1.1.1.50! root     8869:        if(*(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
        !          8870:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
        !          8871:                // msdos_putch() will be used in this service
        !          8872:                // if int 29h is hooked, run this service in main thread to call int 29h
        !          8873:                start_service_loop(msdos_int_21h_01h_thread);
        !          8874:        } else {
        !          8875: #endif
        !          8876:                msdos_int_21h_01h_thread(NULL);
        !          8877:                REQUEST_HARDWRE_UPDATE();
        !          8878: #ifdef USE_SERVICE_THREAD
        !          8879:        }
1.1.1.35  root     8880: #endif
1.1       root     8881: }
                   8882: 
                   8883: inline void msdos_int_21h_02h()
                   8884: {
1.1.1.33  root     8885:        UINT8 data = REG8(DL);
                   8886:        msdos_putch(data);
                   8887:        REG8(AL) = data;
                   8888:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8889: }
                   8890: 
                   8891: inline void msdos_int_21h_03h()
                   8892: {
                   8893:        REG8(AL) = msdos_aux_in();
                   8894: }
                   8895: 
                   8896: inline void msdos_int_21h_04h()
                   8897: {
                   8898:        msdos_aux_out(REG8(DL));
                   8899: }
                   8900: 
                   8901: inline void msdos_int_21h_05h()
                   8902: {
                   8903:        msdos_prn_out(REG8(DL));
                   8904: }
                   8905: 
                   8906: inline void msdos_int_21h_06h()
                   8907: {
                   8908:        if(REG8(DL) == 0xff) {
                   8909:                if(msdos_kbhit()) {
                   8910:                        REG8(AL) = msdos_getch();
1.1.1.3   root     8911: #if defined(HAS_I386)
                   8912:                        m_ZF = 0;
                   8913: #else
                   8914:                        m_ZeroVal = 1;
                   8915: #endif
1.1       root     8916:                } else {
                   8917:                        REG8(AL) = 0;
1.1.1.3   root     8918: #if defined(HAS_I386)
                   8919:                        m_ZF = 1;
                   8920: #else
                   8921:                        m_ZeroVal = 0;
                   8922: #endif
1.1.1.14  root     8923:                        maybe_idle();
1.1       root     8924:                }
                   8925:        } else {
1.1.1.33  root     8926:                UINT8 data = REG8(DL);
                   8927:                msdos_putch(data);
                   8928:                REG8(AL) = data;
1.1       root     8929:        }
                   8930: }
                   8931: 
1.1.1.35  root     8932: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     8933: {
                   8934:        REG8(AL) = msdos_getch();
1.1.1.26  root     8935:        
1.1.1.35  root     8936: #ifdef USE_SERVICE_THREAD
                   8937:        service_exit = true;
                   8938: #endif
                   8939:        return(0);
1.1       root     8940: }
                   8941: 
1.1.1.35  root     8942: inline void msdos_int_21h_07h()
                   8943: {
                   8944: #ifdef USE_SERVICE_THREAD
                   8945:        start_service_loop(msdos_int_21h_07h_thread);
                   8946: #else
                   8947:        msdos_int_21h_07h_thread(NULL);
                   8948:        REQUEST_HARDWRE_UPDATE();
                   8949: #endif
                   8950: }
                   8951: 
                   8952: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     8953: {
                   8954:        REG8(AL) = msdos_getch();
1.1.1.33  root     8955:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8956:        
1.1.1.35  root     8957: #ifdef USE_SERVICE_THREAD
                   8958:        service_exit = true;
                   8959: #endif
                   8960:        return(0);
                   8961: }
                   8962: 
                   8963: inline void msdos_int_21h_08h()
                   8964: {
                   8965: #ifdef USE_SERVICE_THREAD
                   8966:        start_service_loop(msdos_int_21h_08h_thread);
                   8967: #else
                   8968:        msdos_int_21h_08h_thread(NULL);
                   8969:        REQUEST_HARDWRE_UPDATE();
                   8970: #endif
1.1       root     8971: }
                   8972: 
                   8973: inline void msdos_int_21h_09h()
                   8974: {
1.1.1.21  root     8975:        msdos_stdio_reopen();
                   8976:        
1.1.1.20  root     8977:        process_t *process = msdos_process_info_get(current_psp);
                   8978:        int fd = msdos_psp_get_file_table(1, current_psp);
                   8979:        
1.1.1.14  root     8980:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   8981:        int len = 0;
1.1       root     8982:        
1.1.1.14  root     8983:        while(str[len] != '$' && len < 0x10000) {
                   8984:                len++;
                   8985:        }
1.1.1.20  root     8986:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     8987:                // stdout is redirected to file
1.1.1.20  root     8988:                msdos_write(fd, str, len);
1.1       root     8989:        } else {
                   8990:                for(int i = 0; i < len; i++) {
1.1.1.14  root     8991:                        msdos_putch(str[i]);
1.1       root     8992:                }
                   8993:        }
1.1.1.33  root     8994:        REG8(AL) = '$';
                   8995:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8996: }
                   8997: 
1.1.1.35  root     8998: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     8999: {
1.1.1.3   root     9000:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     9001:        int max = mem[ofs] - 1;
                   9002:        UINT8 *buf = mem + ofs + 2;
                   9003:        int chr, p = 0;
                   9004:        
                   9005:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     9006:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     9007:                        p = 0;
1.1.1.33  root     9008:                        msdos_putch(0x03);
                   9009:                        msdos_putch(0x0d);
                   9010:                        msdos_putch(0x0a);
1.1.1.26  root     9011:                        break;
1.1.1.33  root     9012:                } else if(ctrl_break_pressed) {
                   9013:                        // skip this byte
1.1.1.26  root     9014:                } else if(chr == 0x00) {
1.1       root     9015:                        // skip 2nd byte
                   9016:                        msdos_getch();
                   9017:                } else if(chr == 0x08) {
                   9018:                        // back space
                   9019:                        if(p > 0) {
                   9020:                                p--;
1.1.1.20  root     9021:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     9022:                                        msdos_putch(0x08);
                   9023:                                        msdos_putch(0x08);
                   9024:                                        msdos_putch(0x20);
                   9025:                                        msdos_putch(0x20);
                   9026:                                        msdos_putch(0x08);
                   9027:                                        msdos_putch(0x08);
1.1.1.36  root     9028:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9029:                                        p--;
                   9030:                                        msdos_putch(0x08);
                   9031:                                        msdos_putch(0x08);
                   9032:                                        msdos_putch(0x20);
                   9033:                                        msdos_putch(0x20);
                   9034:                                        msdos_putch(0x08);
                   9035:                                        msdos_putch(0x08);
1.1.1.34  root     9036:                                } else {
                   9037:                                        msdos_putch(0x08);
                   9038:                                        msdos_putch(0x20);
                   9039:                                        msdos_putch(0x08);
                   9040:                                }
                   9041:                        }
                   9042:                } else if(chr == 0x1b) {
                   9043:                        // escape
                   9044:                        while(p > 0) {
                   9045:                                p--;
                   9046:                                if(msdos_ctrl_code_check(buf[p])) {
                   9047:                                        msdos_putch(0x08);
                   9048:                                        msdos_putch(0x08);
                   9049:                                        msdos_putch(0x20);
                   9050:                                        msdos_putch(0x20);
                   9051:                                        msdos_putch(0x08);
                   9052:                                        msdos_putch(0x08);
1.1.1.20  root     9053:                                } else {
1.1.1.34  root     9054:                                        msdos_putch(0x08);
                   9055:                                        msdos_putch(0x20);
                   9056:                                        msdos_putch(0x08);
1.1.1.20  root     9057:                                }
1.1       root     9058:                        }
                   9059:                } else if(p < max) {
                   9060:                        buf[p++] = chr;
                   9061:                        msdos_putch(chr);
                   9062:                }
                   9063:        }
                   9064:        buf[p] = 0x0d;
                   9065:        mem[ofs + 1] = p;
1.1.1.33  root     9066:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9067:        
1.1.1.35  root     9068: #ifdef USE_SERVICE_THREAD
                   9069:        service_exit = true;
                   9070: #endif
                   9071:        return(0);
                   9072: }
                   9073: 
                   9074: inline void msdos_int_21h_0ah()
                   9075: {
                   9076:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   9077: #ifdef USE_SERVICE_THREAD
1.1.1.50! root     9078:                if(*(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
        !          9079:                   *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
        !          9080:                        // msdos_putch() will be used in this service
        !          9081:                        // if int 29h is hooked, run this service in main thread to call int 29h
        !          9082:                        start_service_loop(msdos_int_21h_0ah_thread);
        !          9083:                } else {
        !          9084: #endif
        !          9085:                        msdos_int_21h_0ah_thread(NULL);
        !          9086:                        REQUEST_HARDWRE_UPDATE();
        !          9087: #ifdef USE_SERVICE_THREAD
        !          9088:                }
1.1.1.35  root     9089: #endif
                   9090:        }
1.1       root     9091: }
                   9092: 
                   9093: inline void msdos_int_21h_0bh()
                   9094: {
                   9095:        if(msdos_kbhit()) {
                   9096:                REG8(AL) = 0xff;
                   9097:        } else {
                   9098:                REG8(AL) = 0x00;
1.1.1.14  root     9099:                maybe_idle();
1.1       root     9100:        }
1.1.1.33  root     9101:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9102: }
                   9103: 
                   9104: inline void msdos_int_21h_0ch()
                   9105: {
                   9106:        // clear key buffer
1.1.1.21  root     9107:        msdos_stdio_reopen();
                   9108:        
1.1.1.20  root     9109:        process_t *process = msdos_process_info_get(current_psp);
                   9110:        int fd = msdos_psp_get_file_table(0, current_psp);
                   9111:        
                   9112:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9113:                // stdin is redirected to file
                   9114:        } else {
                   9115:                while(msdos_kbhit()) {
                   9116:                        msdos_getch();
                   9117:                }
                   9118:        }
                   9119:        
                   9120:        switch(REG8(AL)) {
                   9121:        case 0x01:
                   9122:                msdos_int_21h_01h();
                   9123:                break;
                   9124:        case 0x06:
                   9125:                msdos_int_21h_06h();
                   9126:                break;
                   9127:        case 0x07:
                   9128:                msdos_int_21h_07h();
                   9129:                break;
                   9130:        case 0x08:
                   9131:                msdos_int_21h_08h();
                   9132:                break;
                   9133:        case 0x0a:
                   9134:                msdos_int_21h_0ah();
                   9135:                break;
                   9136:        default:
1.1.1.48  root     9137:                // the buffer is flushed but no input is attempted
1.1       root     9138:                break;
                   9139:        }
                   9140: }
                   9141: 
                   9142: inline void msdos_int_21h_0dh()
                   9143: {
                   9144: }
                   9145: 
                   9146: inline void msdos_int_21h_0eh()
                   9147: {
                   9148:        if(REG8(DL) < 26) {
                   9149:                _chdrive(REG8(DL) + 1);
                   9150:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9151:                msdos_sda_update(current_psp);
1.1       root     9152:        }
                   9153:        REG8(AL) = 26; // zdrive
                   9154: }
                   9155: 
1.1.1.14  root     9156: inline void msdos_int_21h_0fh()
                   9157: {
                   9158:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9159:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9160:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9161:        HANDLE hFile = CreateFile(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16  root     9162:        
1.1.1.14  root     9163:        if(hFile == INVALID_HANDLE_VALUE) {
                   9164:                REG8(AL) = 0xff;
                   9165:        } else {
                   9166:                REG8(AL) = 0;
                   9167:                fcb->current_block = 0;
                   9168:                fcb->record_size = 128;
                   9169:                fcb->file_size = GetFileSize(hFile, NULL);
                   9170:                fcb->handle = hFile;
                   9171:                fcb->cur_record = 0;
                   9172:        }
                   9173: }
                   9174: 
                   9175: inline void msdos_int_21h_10h()
                   9176: {
                   9177:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9178:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9179:        
                   9180:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9181: }
                   9182: 
1.1       root     9183: inline void msdos_int_21h_11h()
                   9184: {
1.1.1.3   root     9185:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9186:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9187:        
                   9188:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9189:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9190:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9191:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45  root     9192:        const char *path = msdos_fcb_path(fcb);
1.1       root     9193:        WIN32_FIND_DATA fd;
                   9194:        
1.1.1.13  root     9195:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9196:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9197:                FindClose(dtainfo->find_handle);
                   9198:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9199:        }
                   9200:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9201:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9202:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9203:        
1.1.1.14  root     9204:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9205:                dtainfo->allowable_mask &= ~8;
1.1       root     9206:        }
1.1.1.14  root     9207:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   9208:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9209:                      !msdos_find_file_has_8dot3name(&fd)) {
                   9210:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9211:                                FindClose(dtainfo->find_handle);
                   9212:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9213:                                break;
                   9214:                        }
                   9215:                }
                   9216:        }
1.1.1.13  root     9217:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9218:                if(ext_fcb->flag == 0xff) {
                   9219:                        ext_find->flag = 0xff;
                   9220:                        memset(ext_find->reserved, 0, 5);
                   9221:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9222:                }
                   9223:                find->drive = _getdrive();
1.1.1.13  root     9224:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9225:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9226:                find->nt_res = 0;
                   9227:                msdos_find_file_conv_local_time(&fd);
                   9228:                find->create_time_ms = 0;
                   9229:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9230:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9231:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9232:                find->cluster_hi = find->cluster_lo = 0;
                   9233:                find->file_size = fd.nFileSizeLow;
                   9234:                REG8(AL) = 0x00;
1.1.1.14  root     9235:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9236:                if(ext_fcb->flag == 0xff) {
                   9237:                        ext_find->flag = 0xff;
                   9238:                        memset(ext_find->reserved, 0, 5);
                   9239:                        ext_find->attribute = 8;
                   9240:                }
                   9241:                find->drive = _getdrive();
                   9242:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9243:                find->attribute = 8;
                   9244:                find->nt_res = 0;
                   9245:                msdos_find_file_conv_local_time(&fd);
                   9246:                find->create_time_ms = 0;
                   9247:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9248:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9249:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9250:                find->cluster_hi = find->cluster_lo = 0;
                   9251:                find->file_size = 0;
1.1.1.14  root     9252:                dtainfo->allowable_mask &= ~8;
1.1       root     9253:                REG8(AL) = 0x00;
                   9254:        } else {
                   9255:                REG8(AL) = 0xff;
                   9256:        }
                   9257: }
                   9258: 
                   9259: inline void msdos_int_21h_12h()
                   9260: {
1.1.1.3   root     9261:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     9262: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9263:        
                   9264:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9265:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9266:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9267:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9268:        WIN32_FIND_DATA fd;
                   9269:        
1.1.1.13  root     9270:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9271:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9272:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     9273:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9274:                              !msdos_find_file_has_8dot3name(&fd)) {
                   9275:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9276:                                        FindClose(dtainfo->find_handle);
                   9277:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9278:                                        break;
                   9279:                                }
                   9280:                        }
                   9281:                } else {
1.1.1.13  root     9282:                        FindClose(dtainfo->find_handle);
                   9283:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9284:                }
                   9285:        }
1.1.1.13  root     9286:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9287:                if(ext_fcb->flag == 0xff) {
                   9288:                        ext_find->flag = 0xff;
                   9289:                        memset(ext_find->reserved, 0, 5);
                   9290:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9291:                }
                   9292:                find->drive = _getdrive();
1.1.1.13  root     9293:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9294:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9295:                find->nt_res = 0;
                   9296:                msdos_find_file_conv_local_time(&fd);
                   9297:                find->create_time_ms = 0;
                   9298:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9299:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9300:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9301:                find->cluster_hi = find->cluster_lo = 0;
                   9302:                find->file_size = fd.nFileSizeLow;
                   9303:                REG8(AL) = 0x00;
1.1.1.14  root     9304:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9305:                if(ext_fcb->flag == 0xff) {
                   9306:                        ext_find->flag = 0xff;
                   9307:                        memset(ext_find->reserved, 0, 5);
                   9308:                        ext_find->attribute = 8;
                   9309:                }
                   9310:                find->drive = _getdrive();
                   9311:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9312:                find->attribute = 8;
                   9313:                find->nt_res = 0;
                   9314:                msdos_find_file_conv_local_time(&fd);
                   9315:                find->create_time_ms = 0;
                   9316:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9317:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9318:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9319:                find->cluster_hi = find->cluster_lo = 0;
                   9320:                find->file_size = 0;
1.1.1.14  root     9321:                dtainfo->allowable_mask &= ~8;
1.1       root     9322:                REG8(AL) = 0x00;
                   9323:        } else {
                   9324:                REG8(AL) = 0xff;
                   9325:        }
                   9326: }
                   9327: 
                   9328: inline void msdos_int_21h_13h()
                   9329: {
1.1.1.3   root     9330:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     9331:                REG8(AL) = 0xff;
                   9332:        } else {
                   9333:                REG8(AL) = 0x00;
                   9334:        }
                   9335: }
                   9336: 
1.1.1.16  root     9337: inline void msdos_int_21h_14h()
                   9338: {
                   9339:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9340:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9341:        process_t *process = msdos_process_info_get(current_psp);
                   9342:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9343:        DWORD num = 0;
                   9344:        
                   9345:        memset(mem + dta_laddr, 0, fcb->record_size);
                   9346:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9347:                REG8(AL) = 1;
                   9348:        } else {
                   9349:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9350:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9351:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9352:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9353:        }
                   9354: }
                   9355: 
                   9356: inline void msdos_int_21h_15h()
1.1.1.14  root     9357: {
                   9358:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9359:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     9360:        process_t *process = msdos_process_info_get(current_psp);
                   9361:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9362:        DWORD num = 0;
1.1.1.14  root     9363:        
1.1.1.16  root     9364:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9365:                REG8(AL) = 1;
                   9366:        } else {
                   9367:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9368:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9369:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9370:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9371:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9372:        }
                   9373: }
                   9374: 
                   9375: inline void msdos_int_21h_16h()
                   9376: {
                   9377:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9378:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9379:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9380:        HANDLE hFile = CreateFile(path, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, ext_fcb->flag == 0xff ? ext_fcb->attribute : FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16  root     9381:        
1.1.1.14  root     9382:        if(hFile == INVALID_HANDLE_VALUE) {
                   9383:                REG8(AL) = 0xff;
                   9384:        } else {
                   9385:                REG8(AL) = 0;
                   9386:                fcb->current_block = 0;
                   9387:                fcb->record_size = 128;
                   9388:                fcb->file_size = 0;
                   9389:                fcb->handle = hFile;
                   9390:                fcb->cur_record = 0;
                   9391:        }
                   9392: }
                   9393: 
1.1.1.16  root     9394: inline void msdos_int_21h_17h()
                   9395: {
                   9396:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9397:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45  root     9398: //     const char *path_src = msdos_fcb_path(fcb_src);
                   9399:        char path_src[MAX_PATH];
                   9400:        strcpy(path_src, msdos_fcb_path(fcb_src));
                   9401:        
1.1.1.16  root     9402:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   9403:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45  root     9404: //     const char *path_dst = msdos_fcb_path(fcb_dst);
                   9405:        char path_dst[MAX_PATH];
                   9406:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     9407:        
                   9408:        if(rename(path_src, path_dst)) {
                   9409:                REG8(AL) = 0xff;
                   9410:        } else {
                   9411:                REG8(AL) = 0;
                   9412:        }
                   9413: }
                   9414: 
1.1       root     9415: inline void msdos_int_21h_18h()
                   9416: {
                   9417:        REG8(AL) = 0x00;
                   9418: }
                   9419: 
                   9420: inline void msdos_int_21h_19h()
                   9421: {
                   9422:        REG8(AL) = _getdrive() - 1;
                   9423: }
                   9424: 
                   9425: inline void msdos_int_21h_1ah()
                   9426: {
                   9427:        process_t *process = msdos_process_info_get(current_psp);
                   9428:        
                   9429:        process->dta.w.l = REG16(DX);
1.1.1.3   root     9430:        process->dta.w.h = SREG(DS);
1.1.1.23  root     9431:        msdos_sda_update(current_psp);
1.1       root     9432: }
                   9433: 
                   9434: inline void msdos_int_21h_1bh()
                   9435: {
                   9436:        int drive_num = _getdrive() - 1;
                   9437:        UINT16 seg, ofs;
                   9438:        
                   9439:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9440:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9441:                REG8(AL) = dpb->highest_sector_num + 1;
                   9442:                REG16(CX) = dpb->bytes_per_sector;
                   9443:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9444:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9445:        } else {
                   9446:                REG8(AL) = 0xff;
1.1.1.3   root     9447:                m_CF = 1;
1.1       root     9448:        }
                   9449: 
                   9450: }
                   9451: 
                   9452: inline void msdos_int_21h_1ch()
                   9453: {
1.1.1.41  root     9454:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     9455:        UINT16 seg, ofs;
                   9456:        
                   9457:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9458:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9459:                REG8(AL) = dpb->highest_sector_num + 1;
                   9460:                REG16(CX) = dpb->bytes_per_sector;
                   9461:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9462:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9463:        } else {
                   9464:                REG8(AL) = 0xff;
1.1.1.3   root     9465:                m_CF = 1;
1.1       root     9466:        }
                   9467: 
                   9468: }
                   9469: 
                   9470: inline void msdos_int_21h_1dh()
                   9471: {
                   9472:        REG8(AL) = 0;
                   9473: }
                   9474: 
                   9475: inline void msdos_int_21h_1eh()
                   9476: {
                   9477:        REG8(AL) = 0;
                   9478: }
                   9479: 
                   9480: inline void msdos_int_21h_1fh()
                   9481: {
                   9482:        int drive_num = _getdrive() - 1;
                   9483:        UINT16 seg, ofs;
                   9484:        
                   9485:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9486:                REG8(AL) = 0;
1.1.1.3   root     9487:                SREG(DS) = seg;
                   9488:                i386_load_segment_descriptor(DS);
1.1       root     9489:                REG16(BX) = ofs;
                   9490:        } else {
                   9491:                REG8(AL) = 0xff;
1.1.1.3   root     9492:                m_CF = 1;
1.1       root     9493:        }
                   9494: }
                   9495: 
                   9496: inline void msdos_int_21h_20h()
                   9497: {
                   9498:        REG8(AL) = 0;
                   9499: }
                   9500: 
1.1.1.14  root     9501: inline void msdos_int_21h_21h()
                   9502: {
                   9503:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9504:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9505:        
                   9506:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9507:                REG8(AL) = 1;
                   9508:        } else {
                   9509:                process_t *process = msdos_process_info_get(current_psp);
                   9510:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9511:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     9512:                DWORD num = 0;
1.1.1.14  root     9513:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9514:                        REG8(AL) = 1;
                   9515:                } else {
                   9516:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9517:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9518:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     9519:                }
                   9520:        }
                   9521: }
                   9522: 
                   9523: inline void msdos_int_21h_22h()
                   9524: {
                   9525:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9526:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9527:        
                   9528:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9529:                REG8(AL) = 0xff;
                   9530:        } else {
                   9531:                process_t *process = msdos_process_info_get(current_psp);
                   9532:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     9533:                DWORD num = 0;
1.1.1.14  root     9534:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   9535:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9536:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9537:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9538:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     9539:        }
                   9540: }
                   9541: 
1.1.1.16  root     9542: inline void msdos_int_21h_23h()
                   9543: {
                   9544:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9545:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9546:        const char *path = msdos_fcb_path(fcb);
1.1.1.16  root     9547:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9548:        
                   9549:        if(hFile == INVALID_HANDLE_VALUE) {
                   9550:                REG8(AL) = 0xff;
                   9551:        } else {
                   9552:                UINT32 size = GetFileSize(hFile, NULL);
                   9553:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   9554:                REG8(AL) = 0;
                   9555:        }
                   9556: }
                   9557: 
                   9558: inline void msdos_int_21h_24h()
                   9559: {
                   9560:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9561:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9562:        
                   9563:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   9564: }
                   9565: 
1.1       root     9566: inline void msdos_int_21h_25h()
                   9567: {
                   9568:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     9569:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     9570: }
                   9571: 
                   9572: inline void msdos_int_21h_26h()
                   9573: {
                   9574:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   9575:        
                   9576:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
1.1.1.48  root     9577:        psp->first_mcb = REG16(DX) + (PSP_SIZE >> 4);
1.1       root     9578:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   9579:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   9580:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   9581:        psp->parent_psp = 0;
                   9582: }
                   9583: 
1.1.1.16  root     9584: inline void msdos_int_21h_27h()
                   9585: {
                   9586:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9587:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9588:        
                   9589:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9590:                REG8(AL) = 1;
                   9591:        } else {
                   9592:                process_t *process = msdos_process_info_get(current_psp);
                   9593:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9594:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   9595:                DWORD num = 0;
                   9596:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   9597:                        REG8(AL) = 1;
                   9598:                } else {
                   9599:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9600:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9601:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9602:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9603:                }
                   9604:        }
                   9605: }
                   9606: 
                   9607: inline void msdos_int_21h_28h()
                   9608: {
                   9609:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9610:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9611:        
                   9612:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9613:                REG8(AL) = 0xff;
                   9614:        } else {
                   9615:                process_t *process = msdos_process_info_get(current_psp);
                   9616:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9617:                DWORD num = 0;
                   9618:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   9619:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9620:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9621:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9622:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9623:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9624:        }
                   9625: }
                   9626: 
1.1       root     9627: inline void msdos_int_21h_29h()
                   9628: {
1.1.1.20  root     9629:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   9630:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     9631:        UINT8 drv = 0;
                   9632:        char sep_chars[] = ":.;,=+";
                   9633:        char end_chars[] = "\\<>|/\"[]";
                   9634:        char spc_chars[] = " \t";
                   9635:        
1.1.1.20  root     9636:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   9637:        buffer[1023] = 0;
                   9638:        memset(name, 0x20, sizeof(name));
                   9639:        memset(ext, 0x20, sizeof(ext));
                   9640:        
1.1       root     9641:        if(REG8(AL) & 1) {
1.1.1.20  root     9642:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   9643:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     9644:                        ofs++;
                   9645:                }
                   9646:        }
1.1.1.20  root     9647:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     9648:        
1.1.1.24  root     9649:        if(buffer[ofs + 1] == ':') {
                   9650:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   9651:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     9652:                        ofs += 2;
1.1.1.24  root     9653:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9654:                                ofs++;
                   9655:                        }
                   9656:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   9657:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     9658:                        ofs += 2;
1.1.1.24  root     9659:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9660:                                ofs++;
                   9661:                        }
1.1       root     9662:                }
                   9663:        }
1.1.1.20  root     9664:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9665:                UINT8 c = buffer[ofs];
                   9666:                if(is_kanji) {
                   9667:                        is_kanji = 0;
                   9668:                } else if(msdos_lead_byte_check(c)) {
                   9669:                        is_kanji = 1;
                   9670:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9671:                        break;
                   9672:                } else if(c >= 'a' && c <= 'z') {
                   9673:                        c -= 0x20;
                   9674:                }
                   9675:                ofs++;
                   9676:                name[i] = c;
                   9677:        }
1.1.1.20  root     9678:        if(buffer[ofs] == '.') {
1.1       root     9679:                ofs++;
1.1.1.20  root     9680:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9681:                        UINT8 c = buffer[ofs];
                   9682:                        if(is_kanji) {
                   9683:                                is_kanji = 0;
                   9684:                        } else if(msdos_lead_byte_check(c)) {
                   9685:                                is_kanji = 1;
                   9686:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9687:                                break;
                   9688:                        } else if(c >= 'a' && c <= 'z') {
                   9689:                                c -= 0x20;
                   9690:                        }
                   9691:                        ofs++;
                   9692:                        ext[i] = c;
                   9693:                }
                   9694:        }
1.1.1.20  root     9695:        int si = REG16(SI) + ofs;
1.1.1.3   root     9696:        int ds = SREG(DS);
1.1       root     9697:        while(si > 0xffff) {
                   9698:                si -= 0x10;
                   9699:                ds++;
                   9700:        }
                   9701:        REG16(SI) = si;
1.1.1.3   root     9702:        SREG(DS) = ds;
                   9703:        i386_load_segment_descriptor(DS);
1.1       root     9704:        
1.1.1.3   root     9705:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     9706:        if(!(REG8(AL) & 2) || drv != 0) {
                   9707:                fcb[0] = drv;
                   9708:        }
                   9709:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   9710:                memcpy(fcb + 1, name, 8);
                   9711:        }
                   9712:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   9713:                memcpy(fcb + 9, ext, 3);
                   9714:        }
                   9715:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     9716:                if(fcb[i] == '*') {
                   9717:                        found_star = 1;
                   9718:                }
                   9719:                if(found_star) {
                   9720:                        fcb[i] = '?';
                   9721:                }
                   9722:        }
1.1.1.20  root     9723:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     9724:                if(fcb[i] == '*') {
                   9725:                        found_star = 1;
                   9726:                }
                   9727:                if(found_star) {
                   9728:                        fcb[i] = '?';
                   9729:                }
                   9730:        }
                   9731:        
1.1.1.44  root     9732:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     9733:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   9734:                        REG8(AL) = 0x01;
1.1.1.20  root     9735:                } else {
                   9736:                        REG8(AL) = 0x00;
1.1       root     9737:                }
                   9738:        } else {
                   9739:                REG8(AL) = 0xff;
                   9740:        }
                   9741: }
                   9742: 
                   9743: inline void msdos_int_21h_2ah()
                   9744: {
                   9745:        SYSTEMTIME sTime;
                   9746:        
                   9747:        GetLocalTime(&sTime);
                   9748:        REG16(CX) = sTime.wYear;
                   9749:        REG8(DH) = (UINT8)sTime.wMonth;
                   9750:        REG8(DL) = (UINT8)sTime.wDay;
                   9751:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   9752: }
                   9753: 
                   9754: inline void msdos_int_21h_2bh()
                   9755: {
1.1.1.14  root     9756:        REG8(AL) = 0xff;
1.1       root     9757: }
                   9758: 
                   9759: inline void msdos_int_21h_2ch()
                   9760: {
                   9761:        SYSTEMTIME sTime;
                   9762:        
                   9763:        GetLocalTime(&sTime);
                   9764:        REG8(CH) = (UINT8)sTime.wHour;
                   9765:        REG8(CL) = (UINT8)sTime.wMinute;
                   9766:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     9767:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     9768: }
                   9769: 
                   9770: inline void msdos_int_21h_2dh()
                   9771: {
                   9772:        REG8(AL) = 0x00;
                   9773: }
                   9774: 
                   9775: inline void msdos_int_21h_2eh()
                   9776: {
                   9777:        process_t *process = msdos_process_info_get(current_psp);
                   9778:        
                   9779:        process->verify = REG8(AL);
                   9780: }
                   9781: 
                   9782: inline void msdos_int_21h_2fh()
                   9783: {
                   9784:        process_t *process = msdos_process_info_get(current_psp);
                   9785:        
                   9786:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     9787:        SREG(ES) = process->dta.w.h;
                   9788:        i386_load_segment_descriptor(ES);
1.1       root     9789: }
                   9790: 
                   9791: inline void msdos_int_21h_30h()
                   9792: {
                   9793:        // Version Flag / OEM
1.1.1.27  root     9794:        if(REG8(AL) == 0x01) {
1.1.1.29  root     9795: #ifdef SUPPORT_HMA
                   9796:                REG16(BX) = 0x0000;
                   9797: #else
                   9798:                REG16(BX) = 0x1000; // DOS is in HMA
                   9799: #endif
1.1       root     9800:        } else {
1.1.1.27  root     9801:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   9802:                // but this is not correct on Windows 98 SE
                   9803: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   9804:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     9805:        }
1.1.1.27  root     9806:        REG16(CX) = 0x0000;
1.1.1.30  root     9807:        REG8(AL) = dos_major_version;   // 7
                   9808:        REG8(AH) = dos_minor_version;   // 10
1.1       root     9809: }
                   9810: 
                   9811: inline void msdos_int_21h_31h()
                   9812: {
1.1.1.29  root     9813:        try {
                   9814:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9815:        } catch(...) {
                   9816:                // recover the broken mcb
                   9817:                int mcb_seg = current_psp - 1;
                   9818:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     9819:                
1.1.1.29  root     9820:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     9821:                        mcb->mz = 'M';
                   9822:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   9823:                        
1.1.1.29  root     9824:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     9825:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     9826:                        } else {
1.1.1.39  root     9827:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     9828:                        }
                   9829:                } else {
                   9830:                        mcb->mz = 'Z';
1.1.1.30  root     9831:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     9832:                }
                   9833:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9834:        }
1.1       root     9835:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   9836: }
                   9837: 
                   9838: inline void msdos_int_21h_32h()
                   9839: {
                   9840:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   9841:        UINT16 seg, ofs;
                   9842:        
                   9843:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9844:                REG8(AL) = 0;
1.1.1.3   root     9845:                SREG(DS) = seg;
                   9846:                i386_load_segment_descriptor(DS);
1.1       root     9847:                REG16(BX) = ofs;
                   9848:        } else {
                   9849:                REG8(AL) = 0xff;
1.1.1.3   root     9850:                m_CF = 1;
1.1       root     9851:        }
                   9852: }
                   9853: 
                   9854: inline void msdos_int_21h_33h()
                   9855: {
                   9856:        char path[MAX_PATH];
1.1.1.48  root     9857:        char drive = 3; // C:
1.1       root     9858:        
                   9859:        switch(REG8(AL)) {
                   9860:        case 0x00:
1.1.1.33  root     9861:                REG8(DL) = ctrl_break_checking;
1.1       root     9862:                break;
                   9863:        case 0x01:
1.1.1.33  root     9864:                ctrl_break_checking = REG8(DL);
                   9865:                break;
                   9866:        case 0x02:
                   9867:                {
                   9868:                        UINT8 old = ctrl_break_checking;
                   9869:                        ctrl_break_checking = REG8(DL);
                   9870:                        REG8(DL) = old;
                   9871:                }
                   9872:                break;
                   9873:        case 0x03:
                   9874:        case 0x04:
                   9875:                // DOS 4.0+ - Unused
1.1       root     9876:                break;
                   9877:        case 0x05:
1.1.1.48  root     9878:                if(GetSystemDirectory(path, MAX_PATH) != 0) {
                   9879:                        if(path[0] >= 'a' && path[0] <= 'z') {
                   9880:                                drive = path[0] - 'a' + 1;
                   9881:                        } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   9882:                                drive = path[0] - 'A' + 1;
                   9883:                        }
1.1       root     9884:                }
1.1.1.48  root     9885:                REG8(DL) = (UINT8)drive;
1.1       root     9886:                break;
                   9887:        case 0x06:
1.1.1.2   root     9888:                // MS-DOS version (7.10)
1.1       root     9889:                REG8(BL) = 7;
1.1.1.2   root     9890:                REG8(BH) = 10;
1.1       root     9891:                REG8(DL) = 0;
1.1.1.29  root     9892: #ifdef SUPPORT_HMA
                   9893:                REG8(DH) = 0x00;
                   9894: #else
                   9895:                REG8(DH) = 0x10; // DOS is in HMA
                   9896: #endif
1.1       root     9897:                break;
1.1.1.6   root     9898:        case 0x07:
                   9899:                if(REG8(DL) == 0) {
                   9900:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   9901:                } else if(REG8(DL) == 1) {
                   9902:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   9903:                }
                   9904:                break;
1.1       root     9905:        default:
1.1.1.22  root     9906:                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     9907: //             REG16(AX) = 0x01;
                   9908: //             m_CF = 1;
                   9909:                REG8(AL) = 0xff;
1.1       root     9910:                break;
                   9911:        }
                   9912: }
                   9913: 
1.1.1.23  root     9914: inline void msdos_int_21h_34h()
                   9915: {
                   9916:        SREG(ES) = SDA_TOP >> 4;
                   9917:        i386_load_segment_descriptor(ES);
                   9918:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   9919: }
                   9920: 
1.1       root     9921: inline void msdos_int_21h_35h()
                   9922: {
                   9923:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     9924:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   9925:        i386_load_segment_descriptor(ES);
1.1       root     9926: }
                   9927: 
                   9928: inline void msdos_int_21h_36h()
                   9929: {
                   9930:        struct _diskfree_t df = {0};
                   9931:        
                   9932:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   9933:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   9934:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     9935:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   9936:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     9937:        } else {
                   9938:                REG16(AX) = 0xffff;
                   9939:        }
                   9940: }
                   9941: 
                   9942: inline void msdos_int_21h_37h()
                   9943: {
1.1.1.22  root     9944:        static UINT8 dev_flag = 0xff;
1.1       root     9945:        
                   9946:        switch(REG8(AL)) {
                   9947:        case 0x00:
1.1.1.22  root     9948:                {
                   9949:                        process_t *process = msdos_process_info_get(current_psp);
                   9950:                        REG8(AL) = 0x00;
                   9951:                        REG8(DL) = process->switchar;
                   9952:                }
1.1       root     9953:                break;
                   9954:        case 0x01:
1.1.1.22  root     9955:                {
                   9956:                        process_t *process = msdos_process_info_get(current_psp);
                   9957:                        REG8(AL) = 0x00;
                   9958:                        process->switchar = REG8(DL);
1.1.1.23  root     9959:                        msdos_sda_update(current_psp);
1.1.1.22  root     9960:                }
                   9961:                break;
                   9962:        case 0x02:
                   9963:                REG8(DL) = dev_flag;
                   9964:                break;
                   9965:        case 0x03:
                   9966:                dev_flag = REG8(DL);
                   9967:                break;
                   9968:        case 0xd0:
                   9969:        case 0xd1:
                   9970:        case 0xd2:
                   9971:        case 0xd3:
                   9972:        case 0xd4:
                   9973:        case 0xd5:
                   9974:        case 0xd6:
                   9975:        case 0xd7:
                   9976:        case 0xdc:
                   9977:        case 0xdd:
                   9978:        case 0xde:
                   9979:        case 0xdf:
1.1.1.48  root     9980:                // DIET v1.43e
                   9981: //             REG16(AX) = 1;
                   9982:                REG8(AL) = 0xff;
1.1       root     9983:                break;
                   9984:        default:
1.1.1.22  root     9985:                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     9986: //             REG16(AX) = 1;
                   9987:                REG8(AL) = 0xff;
1.1       root     9988:                break;
                   9989:        }
                   9990: }
                   9991: 
1.1.1.42  root     9992: int get_country_info(country_info_t *ci, LCID locale)
1.1.1.17  root     9993: {
                   9994:        char LCdata[80];
                   9995:        
1.1.1.19  root     9996:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.42  root     9997:        GetLocaleInfo(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     9998:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.42  root     9999:        GetLocaleInfo(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10000:        ci->currency_format = *LCdata - '0';
1.1.1.42  root     10001:        GetLocaleInfo(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10002:        ci->date_format = *LCdata - '0';
1.1.1.42  root     10003:        GetLocaleInfo(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10004:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.42  root     10005:        GetLocaleInfo(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10006:        *ci->date_sep = *LCdata;
1.1.1.42  root     10007:        GetLocaleInfo(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     10008:        *ci->dec_sep = *LCdata;
1.1.1.42  root     10009:        GetLocaleInfo(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     10010:        *ci->list_sep = *LCdata;
1.1.1.42  root     10011:        GetLocaleInfo(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     10012:        *ci->thou_sep = *LCdata;
1.1.1.42  root     10013:        GetLocaleInfo(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     10014:        *ci->time_sep = *LCdata;
1.1.1.42  root     10015:        GetLocaleInfo(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     10016:        if(strchr(LCdata, 'H') != NULL) {
                   10017:                ci->time_format = 1;
                   10018:        }
1.1.1.49  root     10019:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffc:000a
                   10020:        ci->case_map.w.h = DUMMY_TOP >> 4;
1.1.1.42  root     10021:        GetLocaleInfo(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     10022:        return atoi(LCdata);
                   10023: }
                   10024: 
1.1.1.42  root     10025: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   10026: {
                   10027:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   10028: }
                   10029: 
                   10030: int get_country_info(country_info_t *ci)
                   10031: {
                   10032:        return get_country_info(ci, LOCALE_USER_DEFAULT);
                   10033: }
                   10034: 
1.1.1.43  root     10035: void set_country_info(country_info_t *ci, int size)
                   10036: {
                   10037:        char LCdata[80];
                   10038:        
                   10039:        if(size >= 0x00 + 2) {
                   10040:                memset(LCdata, 0, sizeof(LCdata));
                   10041:                *LCdata = '0' + ci->date_format;
                   10042:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
                   10043:        }
                   10044:        if(size >= 0x02 + 5) {
                   10045:                memset(LCdata, 0, sizeof(LCdata));
                   10046:                memcpy(LCdata, &ci->currency_symbol, 4);
                   10047:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
                   10048:        }
                   10049:        if(size >= 0x07 + 2) {
                   10050:                memset(LCdata, 0, sizeof(LCdata));
                   10051:                *LCdata = *ci->thou_sep;
                   10052:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
                   10053:        }
                   10054:        if(size >= 0x09 + 2) {
                   10055:                memset(LCdata, 0, sizeof(LCdata));
                   10056:                *LCdata = *ci->dec_sep;
                   10057:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
                   10058:        }
                   10059:        if(size >= 0x0b + 2) {
                   10060:                memset(LCdata, 0, sizeof(LCdata));
                   10061:                *LCdata = *ci->date_sep;
                   10062:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
                   10063:        }
                   10064:        if(size >= 0x0d + 2) {
                   10065:                memset(LCdata, 0, sizeof(LCdata));
                   10066:                *LCdata = *ci->time_sep;
                   10067:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
                   10068:        }
                   10069:        if(size >= 0x0f + 1) {
                   10070:                memset(LCdata, 0, sizeof(LCdata));
                   10071:                *LCdata = '0' + ci->currency_format;
                   10072:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
                   10073:        }
                   10074:        if(size >= 0x10 + 1) {
                   10075:                sprintf(LCdata, "%d", ci->currency_dec_digits);
                   10076:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
                   10077:        }
                   10078:        if(size >= 0x11 + 1) {
                   10079:                // FIXME: is time format always H/h:mm:ss ???
                   10080:                if(ci->time_format & 1) {
                   10081:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   10082:                } else {
                   10083:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   10084:                }
                   10085:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
                   10086:        }
                   10087:        if(size >= 0x12 + 4) {
                   10088:                // 12h  DWORD   address of case map routine
                   10089:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   10090:        }
                   10091:        if(size >= 0x16 + 2) {
                   10092:                memset(LCdata, 0, sizeof(LCdata));
                   10093:                *LCdata = *ci->list_sep;
                   10094:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
                   10095:        }
                   10096: }
                   10097: 
1.1.1.42  root     10098: #ifndef SUBLANG_SWAHILI
                   10099:        #define SUBLANG_SWAHILI 0x01
                   10100: #endif
                   10101: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10102:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10103: #endif
                   10104: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10105:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10106: #endif
                   10107: #ifndef LANG_BANGLA
                   10108:        #define LANG_BANGLA 0x45
                   10109: #endif
                   10110: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10111:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10112: #endif
                   10113: 
                   10114: static const struct {
                   10115:        int code;
                   10116:        USHORT usPrimaryLanguage;
                   10117:        USHORT usSubLanguage;
                   10118: } country_table[] = {
                   10119:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10120:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10121:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10122:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10123:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10124:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10125: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10126: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10127: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10128: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10129: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10130:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10131:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10132: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10133:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10134: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10135:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10136:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10137:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10138:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10139:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10140:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10141:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10142: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10143: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10144:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10145:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10146:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10147:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10148:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10149: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10150: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10151: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10152:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10153: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10154: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10155: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10156: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10157:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10158:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10159:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10160: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10161:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10162:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10163:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10164:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10165:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10166:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10167:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10168:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10169:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10170:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10171:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10172:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10173:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10174: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10175:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10176:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10177:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10178:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10179:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10180:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10181:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10182:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10183:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10184:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10185: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10186:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10187: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10188:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10189:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10190:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10191:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10192:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10193:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10194:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10195:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10196: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10197:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10198: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10199: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10200:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   10201: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   10202:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   10203:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   10204:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   10205:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   10206:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   10207:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   10208:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   10209: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   10210: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   10211:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   10212: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   10213:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   10214:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   10215:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   10216:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   10217: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   10218: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   10219: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   10220:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   10221:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   10222:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   10223:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   10224:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   10225:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   10226:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   10227:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   10228:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   10229:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   10230:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   10231:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   10232:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   10233:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   10234:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   10235:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   10236:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   10237:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   10238:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   10239:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   10240:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     10241: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     10242:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   10243: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   10244:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   10245:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   10246:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   10247:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   10248:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   10249:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   10250:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   10251:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   10252:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   10253:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   10254:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   10255:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   10256:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   10257:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   10258:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   10259:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   10260:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   10261:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   10262:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   10263:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   10264:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   10265:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   10266:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   10267:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   10268:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   10269: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   10270:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   10271:        {-1, 0, 0},
                   10272: };
                   10273: 
1.1.1.14  root     10274: inline void msdos_int_21h_38h()
                   10275: {
                   10276:        switch(REG8(AL)) {
                   10277:        case 0x00:
1.1.1.19  root     10278:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     10279:                break;
                   10280:        default:
1.1.1.42  root     10281:                for(int i = 0;; i++) {
                   10282:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   10283:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   10284:                                break;
                   10285:                        } else if(country_table[i].code == -1) {
                   10286: //                             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));
                   10287: //                             REG16(AX) = 2;
                   10288: //                             m_CF = 1;
1.1.1.48  root     10289:                                // get current coutry info
1.1.1.42  root     10290:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   10291:                                break;
                   10292:                        }
                   10293:                }
1.1.1.14  root     10294:                break;
                   10295:        }
                   10296: }
                   10297: 
1.1       root     10298: inline void msdos_int_21h_39h(int lfn)
                   10299: {
1.1.1.3   root     10300:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10301:                REG16(AX) = errno;
1.1.1.3   root     10302:                m_CF = 1;
1.1       root     10303:        }
                   10304: }
                   10305: 
                   10306: inline void msdos_int_21h_3ah(int lfn)
                   10307: {
1.1.1.3   root     10308:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10309:                REG16(AX) = errno;
1.1.1.3   root     10310:                m_CF = 1;
1.1       root     10311:        }
                   10312: }
                   10313: 
                   10314: inline void msdos_int_21h_3bh(int lfn)
                   10315: {
1.1.1.45  root     10316:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     10317:        
                   10318:        if(_chdir(path)) {
1.1.1.17  root     10319:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     10320:                m_CF = 1;
1.1.1.44  root     10321:        } else {
                   10322:                int drv = _getdrive() - 1;
                   10323:                if(path[1] == ':') {
                   10324:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   10325:                                drv = path[0] - 'A';
                   10326:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   10327:                                drv = path[0] - 'a';
                   10328:                        }
                   10329:                }
                   10330:                msdos_cds_update(drv, path);
1.1       root     10331:        }
                   10332: }
                   10333: 
                   10334: inline void msdos_int_21h_3ch()
                   10335: {
1.1.1.45  root     10336:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10337:        int attr = GetFileAttributes(path);
1.1.1.37  root     10338:        int fd = -1;
                   10339:        int sio_port = 0;
                   10340:        int lpt_port = 0;
1.1       root     10341:        
1.1.1.45  root     10342:        if(msdos_is_device_path(path)) {
                   10343:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     10344:        } else {
                   10345:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10346:        }
                   10347:        if(fd != -1) {
                   10348:                if(attr == -1) {
                   10349:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   10350:                }
                   10351:                SetFileAttributes(path, attr);
                   10352:                REG16(AX) = fd;
1.1.1.45  root     10353:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10354:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10355:        } else {
                   10356:                REG16(AX) = errno;
1.1.1.3   root     10357:                m_CF = 1;
1.1       root     10358:        }
                   10359: }
                   10360: 
                   10361: inline void msdos_int_21h_3dh()
                   10362: {
1.1.1.45  root     10363:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10364:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     10365:        int fd = -1;
                   10366:        int sio_port = 0;
                   10367:        int lpt_port = 0;
1.1       root     10368:        
                   10369:        if(mode < 0x03) {
1.1.1.45  root     10370:                if(msdos_is_device_path(path)) {
                   10371:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     10372:                } else {
1.1.1.13  root     10373:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     10374:                }
1.1       root     10375:                if(fd != -1) {
                   10376:                        REG16(AX) = fd;
1.1.1.45  root     10377:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10378:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10379:                } else {
                   10380:                        REG16(AX) = errno;
1.1.1.3   root     10381:                        m_CF = 1;
1.1       root     10382:                }
                   10383:        } else {
                   10384:                REG16(AX) = 0x0c;
1.1.1.3   root     10385:                m_CF = 1;
1.1       root     10386:        }
                   10387: }
                   10388: 
                   10389: inline void msdos_int_21h_3eh()
                   10390: {
                   10391:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10392:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10393:        
1.1.1.20  root     10394:        if(fd < process->max_files && file_handler[fd].valid) {
                   10395:                _close(fd);
                   10396:                msdos_file_handler_close(fd);
                   10397:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     10398:        } else {
                   10399:                REG16(AX) = 0x06;
1.1.1.3   root     10400:                m_CF = 1;
1.1       root     10401:        }
                   10402: }
                   10403: 
1.1.1.35  root     10404: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   10405: {
                   10406:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   10407:        int max = REG16(CX);
                   10408:        int p = 0;
                   10409:        
                   10410:        while(max > p) {
                   10411:                int chr = msdos_getch();
                   10412:                
                   10413:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   10414:                        p = 0;
                   10415:                        buf[p++] = 0x0d;
                   10416:                        if(max > p) {
                   10417:                                buf[p++] = 0x0a;
                   10418:                        }
                   10419:                        msdos_putch(0x03);
                   10420:                        msdos_putch(0x0d);
                   10421:                        msdos_putch(0x0a);
                   10422:                        break;
                   10423:                } else if(ctrl_break_pressed) {
                   10424:                        // skip this byte
                   10425:                } else if(chr == 0x00) {
                   10426:                        // skip 2nd byte
                   10427:                        msdos_getch();
                   10428:                } else if(chr == 0x0d) {
                   10429:                        // carriage return
                   10430:                        buf[p++] = 0x0d;
                   10431:                        if(max > p) {
                   10432:                                buf[p++] = 0x0a;
                   10433:                        }
                   10434:                        msdos_putch('\n');
                   10435:                        break;
                   10436:                } else if(chr == 0x08) {
                   10437:                        // back space
                   10438:                        if(p > 0) {
                   10439:                                p--;
                   10440:                                if(msdos_ctrl_code_check(buf[p])) {
                   10441:                                        msdos_putch(0x08);
                   10442:                                        msdos_putch(0x08);
                   10443:                                        msdos_putch(0x20);
                   10444:                                        msdos_putch(0x20);
                   10445:                                        msdos_putch(0x08);
                   10446:                                        msdos_putch(0x08);
1.1.1.36  root     10447:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   10448:                                        p--;
                   10449:                                        msdos_putch(0x08);
                   10450:                                        msdos_putch(0x08);
                   10451:                                        msdos_putch(0x20);
                   10452:                                        msdos_putch(0x20);
                   10453:                                        msdos_putch(0x08);
                   10454:                                        msdos_putch(0x08);
1.1.1.35  root     10455:                                } else {
                   10456:                                        msdos_putch(0x08);
                   10457:                                        msdos_putch(0x20);
                   10458:                                        msdos_putch(0x08);
                   10459:                                }
                   10460:                        }
                   10461:                } else if(chr == 0x1b) {
                   10462:                        // escape
                   10463:                        while(p > 0) {
                   10464:                                p--;
                   10465:                                if(msdos_ctrl_code_check(buf[p])) {
                   10466:                                        msdos_putch(0x08);
                   10467:                                        msdos_putch(0x08);
                   10468:                                        msdos_putch(0x20);
                   10469:                                        msdos_putch(0x20);
                   10470:                                        msdos_putch(0x08);
                   10471:                                        msdos_putch(0x08);
                   10472:                                } else {
                   10473:                                        msdos_putch(0x08);
                   10474:                                        msdos_putch(0x20);
                   10475:                                        msdos_putch(0x08);
                   10476:                                }
                   10477:                        }
                   10478:                } else {
                   10479:                        buf[p++] = chr;
                   10480:                        msdos_putch(chr);
                   10481:                }
                   10482:        }
                   10483:        REG16(AX) = p;
                   10484:        
                   10485: #ifdef USE_SERVICE_THREAD
                   10486:        service_exit = true;
                   10487: #endif
                   10488:        return(0);
                   10489: }
                   10490: 
1.1       root     10491: inline void msdos_int_21h_3fh()
                   10492: {
                   10493:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10494:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10495:        
1.1.1.20  root     10496:        if(fd < process->max_files && file_handler[fd].valid) {
                   10497:                if(file_mode[file_handler[fd].mode].in) {
                   10498:                        if(file_handler[fd].atty) {
1.1       root     10499:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     10500:                                if(REG16(CX) != 0) {
                   10501: #ifdef USE_SERVICE_THREAD
1.1.1.50! root     10502:                                        if(*(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
        !          10503:                                           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
        !          10504:                                                // msdos_putch() will be used in this service
        !          10505:                                                // if int 29h is hooked, run this service in main thread to call int 29h
        !          10506:                                                start_service_loop(msdos_int_21h_3fh_thread);
        !          10507:                                        } else {
        !          10508: #endif
        !          10509:                                                msdos_int_21h_3fh_thread(NULL);
        !          10510:                                                REQUEST_HARDWRE_UPDATE();
        !          10511: #ifdef USE_SERVICE_THREAD
        !          10512:                                        }
1.1.1.35  root     10513: #endif
                   10514:                                } else {
                   10515:                                        REG16(AX) = 0;
1.1       root     10516:                                }
                   10517:                        } else {
1.1.1.37  root     10518:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10519:                        }
                   10520:                } else {
                   10521:                        REG16(AX) = 0x05;
1.1.1.3   root     10522:                        m_CF = 1;
1.1       root     10523:                }
                   10524:        } else {
                   10525:                REG16(AX) = 0x06;
1.1.1.3   root     10526:                m_CF = 1;
1.1       root     10527:        }
                   10528: }
                   10529: 
                   10530: inline void msdos_int_21h_40h()
                   10531: {
                   10532:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10533:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10534:        
1.1.1.20  root     10535:        if(fd < process->max_files && file_handler[fd].valid) {
                   10536:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     10537:                        if(REG16(CX)) {
1.1.1.20  root     10538:                                if(file_handler[fd].atty) {
1.1       root     10539:                                        // BX is stdout/stderr or is redirected to stdout
                   10540:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     10541:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     10542:                                        }
                   10543:                                        REG16(AX) = REG16(CX);
                   10544:                                } else {
1.1.1.20  root     10545:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10546:                                }
                   10547:                        } else {
1.1.1.20  root     10548:                                UINT32 pos = _tell(fd);
                   10549:                                _lseek(fd, 0, SEEK_END);
                   10550:                                UINT32 size = _tell(fd);
1.1.1.12  root     10551:                                if(pos < size) {
1.1.1.20  root     10552:                                        _lseek(fd, pos, SEEK_SET);
                   10553:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     10554:                                } else {
                   10555:                                        for(UINT32 i = size; i < pos; i++) {
                   10556:                                                UINT8 tmp = 0;
1.1.1.23  root     10557:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     10558:                                        }
1.1.1.20  root     10559:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     10560:                                }
1.1.1.23  root     10561:                                REG16(AX) = 0;
1.1       root     10562:                        }
                   10563:                } else {
                   10564:                        REG16(AX) = 0x05;
1.1.1.3   root     10565:                        m_CF = 1;
1.1       root     10566:                }
                   10567:        } else {
                   10568:                REG16(AX) = 0x06;
1.1.1.3   root     10569:                m_CF = 1;
1.1       root     10570:        }
                   10571: }
                   10572: 
                   10573: inline void msdos_int_21h_41h(int lfn)
                   10574: {
1.1.1.3   root     10575:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10576:                REG16(AX) = errno;
1.1.1.3   root     10577:                m_CF = 1;
1.1       root     10578:        }
                   10579: }
                   10580: 
                   10581: inline void msdos_int_21h_42h()
                   10582: {
                   10583:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10584:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10585:        
1.1.1.20  root     10586:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     10587:                if(REG8(AL) < 0x03) {
1.1.1.35  root     10588:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     10589:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   10590:                        UINT32 pos = _tell(fd);
1.1       root     10591:                        REG16(AX) = pos & 0xffff;
                   10592:                        REG16(DX) = (pos >> 16);
                   10593:                } else {
                   10594:                        REG16(AX) = 0x01;
1.1.1.3   root     10595:                        m_CF = 1;
1.1       root     10596:                }
                   10597:        } else {
                   10598:                REG16(AX) = 0x06;
1.1.1.3   root     10599:                m_CF = 1;
1.1       root     10600:        }
                   10601: }
                   10602: 
                   10603: inline void msdos_int_21h_43h(int lfn)
                   10604: {
1.1.1.45  root     10605:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     10606:        int attr;
                   10607:        
1.1.1.14  root     10608:        if(!lfn && REG8(AL) > 2) {
                   10609:                REG16(AX) = 0x01;
                   10610:                m_CF = 1;
                   10611:                return;
                   10612:        }
                   10613:        switch(REG8(lfn ? BL : AL)) {
1.1       root     10614:        case 0x00:
                   10615:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     10616:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   10617:                } else {
                   10618:                        REG16(AX) = (UINT16)GetLastError();
                   10619:                        m_CF = 1;
                   10620:                }
                   10621:                break;
                   10622:        case 0x01:
                   10623:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   10624:                        REG16(AX) = (UINT16)GetLastError();
                   10625:                        m_CF = 1;
                   10626:                }
                   10627:                break;
                   10628:        case 0x02:
                   10629:                {
1.1.1.45  root     10630:                        DWORD compressed_size = GetCompressedFileSize(path, NULL), file_size = 0;
                   10631:                        if(compressed_size != INVALID_FILE_SIZE) {
                   10632:                                if(compressed_size != 0) {
                   10633:                                        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10634:                                        if(hFile != INVALID_HANDLE_VALUE) {
                   10635:                                                file_size = GetFileSize(hFile, NULL);
                   10636:                                                CloseHandle(hFile);
                   10637:                                        }
                   10638:                                        if(compressed_size == file_size) {
                   10639:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   10640:                                                // this isn't correct if the file is in the NTFS MFT
                   10641:                                                if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   10642:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   10643:                                                }
1.1.1.14  root     10644:                                        }
                   10645:                                }
1.1.1.45  root     10646:                                REG16(AX) = LOWORD(compressed_size);
                   10647:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     10648:                        } else {
                   10649:                                REG16(AX) = (UINT16)GetLastError();
                   10650:                                m_CF = 1;
1.1       root     10651:                        }
1.1.1.14  root     10652:                }
                   10653:                break;
                   10654:        case 0x03:
                   10655:        case 0x05:
                   10656:        case 0x07:
1.1.1.48  root     10657:                if(lfn) {
1.1.1.14  root     10658:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10659:                        if(hFile != INVALID_HANDLE_VALUE) {
                   10660:                                FILETIME local, time;
                   10661:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   10662:                                if(REG8(BL) == 7) {
                   10663:                                        ULARGE_INTEGER hund;
                   10664:                                        hund.LowPart = local.dwLowDateTime;
                   10665:                                        hund.HighPart = local.dwHighDateTime;
                   10666:                                        hund.QuadPart += REG16(SI) * 100000;
                   10667:                                        local.dwLowDateTime = hund.LowPart;
                   10668:                                        local.dwHighDateTime = hund.HighPart;
                   10669:                                }
                   10670:                                LocalFileTimeToFileTime(&local, &time);
                   10671:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   10672:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   10673:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   10674:                                        REG16(AX) = (UINT16)GetLastError();
                   10675:                                        m_CF = 1;
                   10676:                                }
                   10677:                                CloseHandle(hFile);
                   10678:                        } else {
                   10679:                                REG16(AX) = (UINT16)GetLastError();
                   10680:                                m_CF = 1;
1.1       root     10681:                        }
1.1.1.48  root     10682:                } else {
                   10683:                        // 214303 DR DOS 3.41+ internal - Set Access Rights And Password
                   10684:                        // 214305 DR DOS 5.0-6.0 internal - Set Extended File Attributes
                   10685:                        // 214307 DR DOS 6.0 - Set File Owner
                   10686: //                     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));
                   10687:                        REG16(AX) = 0x01;
                   10688:                        m_CF = 1;
1.1.1.14  root     10689:                }
                   10690:                break;
                   10691:        case 0x04:
                   10692:        case 0x06:
                   10693:        case 0x08:
1.1.1.48  root     10694:                if(lfn) {
1.1.1.14  root     10695:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   10696:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   10697:                                FILETIME *time, local;
                   10698:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   10699:                                                   0x06 ? &fad.ftLastAccessTime :
                   10700:                                                          &fad.ftCreationTime;
                   10701:                                FileTimeToLocalFileTime(time, &local);
                   10702:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   10703:                                if(REG8(BL) == 0x08) {
                   10704:                                        ULARGE_INTEGER hund;
                   10705:                                        hund.LowPart = local.dwLowDateTime;
                   10706:                                        hund.HighPart = local.dwHighDateTime;
                   10707:                                        hund.QuadPart /= 100000;
                   10708:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   10709:                                }
                   10710:                        } else {
                   10711:                                REG16(AX) = (UINT16)GetLastError();
                   10712:                                m_CF = 1;
1.1       root     10713:                        }
1.1.1.48  root     10714:                } else {
                   10715:                        // 214304 DR DOS 5.0-6.0 internal - Get Encrypted Password
                   10716:                        // 214306 DR DOS 6.0 - Get File Owner
                   10717: //                     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));
                   10718:                        REG16(AX) = 0x01;
                   10719:                        m_CF = 1;
1.1.1.14  root     10720:                }
                   10721:                break;
1.1.1.43  root     10722:        case 0xff:
1.1.1.48  root     10723:                if(!lfn && REG16(BP) == 0x5053) {
1.1.1.43  root     10724:                        if(REG8(CL) == 0x39) {
                   10725:                                msdos_int_21h_39h(1);
                   10726:                                break;
                   10727:                        } else if(REG8(CL) == 0x56) {
                   10728:                                msdos_int_21h_56h(1);
                   10729:                                break;
                   10730:                        }
                   10731:                }
1.1.1.14  root     10732:        default:
1.1.1.22  root     10733:                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     10734:                REG16(AX) = lfn ? 0x7100 : 0x01;
1.1.1.14  root     10735:                m_CF = 1;
                   10736:                break;
                   10737:        }
                   10738: }
                   10739: 
                   10740: inline void msdos_int_21h_44h()
                   10741: {
1.1.1.22  root     10742:        static UINT16 iteration_count = 0;
                   10743:        
1.1.1.44  root     10744:        process_t *process;
                   10745:        int fd, drv;
1.1.1.14  root     10746:        
                   10747:        switch(REG8(AL)) {
                   10748:        case 0x00:
                   10749:        case 0x01:
                   10750:        case 0x02:
                   10751:        case 0x03:
                   10752:        case 0x04:
                   10753:        case 0x05:
                   10754:        case 0x06:
                   10755:        case 0x07:
1.1.1.44  root     10756:                process = msdos_process_info_get(current_psp);
                   10757:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     10758:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   10759:                        REG16(AX) = 0x06;
                   10760:                        m_CF = 1;
                   10761:                        return;
1.1.1.14  root     10762:                }
                   10763:                break;
                   10764:        case 0x08:
                   10765:        case 0x09:
1.1.1.44  root     10766:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   10767:                if(!msdos_is_valid_drive(drv)) {
                   10768:                        // invalid drive
1.1.1.14  root     10769:                        REG16(AX) = 0x0f;
                   10770:                        m_CF = 1;
                   10771:                        return;
1.1       root     10772:                }
                   10773:                break;
                   10774:        }
                   10775:        switch(REG8(AL)) {
1.1.1.48  root     10776:        case 0x00: // Get Device Information
1.1.1.20  root     10777:                REG16(DX) = file_handler[fd].info;
1.1       root     10778:                break;
1.1.1.48  root     10779:        case 0x01: // Set Device Information
1.1.1.45  root     10780:                if(REG8(DH) != 0) {
                   10781: //                     REG16(AX) = 0x0d; // data invalid
                   10782: //                     m_CF = 1;
                   10783:                        file_handler[fd].info = REG16(DX);
                   10784:                } else {
                   10785:                        file_handler[fd].info &= 0xff00;
                   10786:                        file_handler[fd].info |= REG8(DL);
                   10787:                }
1.1       root     10788:                break;
1.1.1.48  root     10789:        case 0x02: // Read From Character Device Control Channel
1.1.1.45  root     10790:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
                   10791:                        // from DOSBox
                   10792:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
                   10793:                        case 0x00:
                   10794:                                if(REG16(CX) >= 6) {
                   10795:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
                   10796:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
                   10797:                                        REG16(AX) = 6; // number of bytes actually read
                   10798:                                } else {
                   10799:                                        REG16(AX) = 0x0d; // data invalid
                   10800:                                        m_CF = 1;
                   10801:                                }
                   10802:                                break;
                   10803:                        case 0x01:
                   10804:                                if(REG16(CX) >= 6) {
                   10805:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
                   10806:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
                   10807:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
                   10808:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
                   10809:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
                   10810:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
                   10811:                                                        int page = (addr - EMS_TOP) / 0x4000;
                   10812:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
                   10813:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   10814:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
                   10815:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
                   10816:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
                   10817:                                                } else {
                   10818:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
                   10819:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   10820:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
                   10821:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
                   10822:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
                   10823:                                                }
                   10824:                                        }
                   10825:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
                   10826:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
                   10827:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
                   10828:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
                   10829:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
                   10830:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
                   10831:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
                   10832:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
                   10833:                                        
                   10834:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
                   10835:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
                   10836:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
                   10837:                                        REG16(AX) = 6; // number of bytes actually read
                   10838:                                } else {
                   10839:                                        REG16(AX) = 0x0d; // data invalid
                   10840:                                        m_CF = 1;
                   10841:                                }
                   10842:                                break;
                   10843:                        case 0x02:
                   10844:                                if(REG16(CX) >= 2) {
                   10845:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
                   10846:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
                   10847:                                        REG16(AX) = 2; // number of bytes actually read
                   10848:                                } else {
                   10849:                                        REG16(AX) = 0x0d; // data invalid
                   10850:                                        m_CF = 1;
                   10851:                                }
                   10852:                                break;
                   10853:                        case 0x03:
                   10854:                                if(REG16(CX) >= 4) {
                   10855:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
                   10856:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
                   10857:                                        REG16(AX) = 4; // number of bytes actually read
                   10858:                                } else {
                   10859:                                        REG16(AX) = 0x0d; // data invalid
                   10860:                                        m_CF = 1;
                   10861:                                }
                   10862:                                break;
                   10863:                        default:
                   10864:                                REG16(AX) = 0x01; // function number invalid
                   10865:                                m_CF = 1;
                   10866:                        }
                   10867:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
                   10868:                        if(REG16(CX) >= 5) {
                   10869:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
                   10870:                                REG16(AX) = 5; // number of bytes actually read
                   10871:                        } else {
                   10872:                                REG16(AX) = 0x0d; // data invalid
                   10873:                                m_CF = 1;
                   10874:                        }
                   10875:                } else {
                   10876: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
                   10877: //                     REG16(AX) = REG16(CX);
                   10878:                        REG16(AX) = 0x05; // access denied
                   10879:                        m_CF = 1;
                   10880:                }
                   10881:                break;
1.1.1.48  root     10882:        case 0x03: // Write To Character Device Control Channel
1.1.1.45  root     10883: //             REG16(AX) = 0x05;
                   10884: //             m_CF = 1;
                   10885:                REG16(AX) = 0x00; // success
                   10886:                break;
1.1.1.48  root     10887:        case 0x04: // Read From Block Device Control Channel
                   10888:        case 0x05: // Write To Block Device Control Channel
1.1       root     10889:                REG16(AX) = 0x05;
1.1.1.3   root     10890:                m_CF = 1;
1.1       root     10891:                break;
1.1.1.48  root     10892:        case 0x06: // Get Input Status
1.1.1.20  root     10893:                if(file_mode[file_handler[fd].mode].in) {
                   10894:                        if(file_handler[fd].atty) {
1.1.1.14  root     10895:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     10896:                        } else {
1.1.1.20  root     10897:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     10898:                        }
1.1.1.14  root     10899:                } else {
                   10900:                        REG8(AL) = 0x00;
1.1       root     10901:                }
                   10902:                break;
1.1.1.48  root     10903:        case 0x07: // Get Output Status
1.1.1.20  root     10904:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     10905:                        REG8(AL) = 0xff;
                   10906:                } else {
                   10907:                        REG8(AL) = 0x00;
1.1       root     10908:                }
                   10909:                break;
1.1.1.48  root     10910:        case 0x08: // Check If Block Device Removable
1.1.1.44  root     10911:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     10912:                        // removable drive
                   10913:                        REG16(AX) = 0x00;
1.1       root     10914:                } else {
1.1.1.14  root     10915:                        // fixed drive
                   10916:                        REG16(AX) = 0x01;
1.1       root     10917:                }
                   10918:                break;
1.1.1.48  root     10919:        case 0x09: // Check If Block Device Remote
1.1.1.44  root     10920:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     10921:                        // remote drive
                   10922:                        REG16(DX) = 0x1000;
1.1.1.44  root     10923:                } else if(msdos_is_subst_drive(drv)) {
                   10924:                        // subst drive
                   10925:                        REG16(DX) = 0x8000;
1.1       root     10926:                } else {
1.1.1.14  root     10927:                        // local drive
1.1.1.44  root     10928:                        REG16(DX) = 0x0000;
1.1       root     10929:                }
                   10930:                break;
1.1.1.48  root     10931:        case 0x0a: // Check If Handle Is Remote
1.1.1.45  root     10932:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
                   10933:                        REG16(DX) = 0x8000;
                   10934:                } else {
                   10935:                        REG16(DX) = 0x0000;
                   10936:                }
1.1.1.21  root     10937:                break;
1.1.1.48  root     10938:        case 0x0b: // Set Sharing Retry Count
1.1       root     10939:                break;
1.1.1.48  root     10940:        case 0x0c: // Generic Character Device Request
1.1.1.22  root     10941:                if(REG8(CL) == 0x45) {
                   10942:                        // set iteration (retry) count
                   10943:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   10944:                } else if(REG8(CL) == 0x4a) {
                   10945:                        // select code page
                   10946:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   10947:                        msdos_nls_tables_update();
1.1.1.44  root     10948:                } else if(REG8(CL) == 0x4c) {
                   10949:                        // start code-page preparation
                   10950:                        int ids[3] = {437, 0, 0}; // 437: US English
                   10951:                        int count = 1, offset = 0;
                   10952:                        if(active_code_page != 437) {
                   10953:                                ids[count++] = active_code_page;
                   10954:                        }
                   10955:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   10956:                                ids[count++] = system_code_page;
                   10957:                        }
                   10958:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   10959:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   10960:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   10961:                        for(int i = 0; i < count; i++) {
                   10962:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   10963:                        }
                   10964:                } else if(REG8(CL) == 0x4d) {
                   10965:                        // end code-page preparation
                   10966:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   10967:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.50! root     10968:                } else if(REG8(CL) == 0x5f) {
        !          10969:                        // set display information
        !          10970:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
        !          10971:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
        !          10972:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
        !          10973:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
        !          10974:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
        !          10975:                                
        !          10976:                                if(cur_width != new_width || cur_height != new_height) {
        !          10977:                                        pcbios_set_console_size(new_width, new_height, true);
        !          10978:                                }
        !          10979:                        }
1.1.1.22  root     10980:                } else if(REG8(CL) == 0x65) {
                   10981:                        // get iteration (retry) count
                   10982:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   10983:                } else if(REG8(CL) == 0x6a) {
                   10984:                        // query selected code page
                   10985:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   10986:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   10987:                        
                   10988:                        CPINFO info;
                   10989:                        GetCPInfo(active_code_page, &info);
                   10990:                        
                   10991:                        if(info.MaxCharSize != 1) {
                   10992:                                for(int i = 0;; i++) {
                   10993:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   10994:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   10995:                                        
                   10996:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   10997:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   10998:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   10999:                                        
                   11000:                                        if(lo == 0 && hi == 0) {
                   11001:                                                break;
                   11002:                                        }
                   11003:                                }
                   11004:                        }
1.1.1.44  root     11005:                } else if(REG8(CL) == 0x6b) {
                   11006:                        // query prepare list
                   11007:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11008:                        int count = 1, offset = 0;
                   11009:                        if(active_code_page != 437) {
                   11010:                                ids[count++] = active_code_page;
                   11011:                        }
                   11012:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11013:                                ids[count++] = system_code_page;
                   11014:                        }
                   11015:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   11016:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11017:                        for(int i = 0; i < count; i++) {
                   11018:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11019:                        }
                   11020:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11021:                        for(int i = 0; i < count; i++) {
                   11022:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11023:                        }
1.1.1.22  root     11024:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     11025:                        // get display information
1.1.1.50! root     11026:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
        !          11027:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
        !          11028:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
        !          11029:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
        !          11030:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
        !          11031:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
        !          11032:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
        !          11033:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
        !          11034:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
        !          11035:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
        !          11036:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.22  root     11037:                } else {
                   11038:                        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));
                   11039:                        REG16(AX) = 0x01; // invalid function
                   11040:                        m_CF = 1;
                   11041:                }
                   11042:                break;
1.1.1.48  root     11043:        case 0x0d: // Generic Block Device Request
1.1.1.22  root     11044:                if(REG8(CL) == 0x40) {
                   11045:                        // set device parameters
1.1.1.48  root     11046: //             } else if(REG8(CL) == 0x41) {
                   11047: //                     // write logical device track
                   11048: //             } else if(REG8(CL) == 0x42) {
                   11049: //                     // format and verify logical device track
1.1.1.22  root     11050:                } else if(REG8(CL) == 0x46) {
                   11051:                        // set volume serial number
1.1.1.48  root     11052:                } else if(REG8(CL) == 0x47) {
                   11053:                        // set access flag
                   11054: //             } else if(REG8(CL) == 0x48) {
                   11055: //                     // set media lock state
                   11056: //             } else if(REG8(CL) == 0x49) {
                   11057: //                     // eject media in drive
1.1.1.22  root     11058:                } else if(REG8(CL) == 0x4a) {
                   11059:                        // lock logical volume
                   11060:                } else if(REG8(CL) == 0x4b) {
                   11061:                        // lock physical volume
                   11062:                } else if(REG8(CL) == 0x60) {
                   11063:                        // get device parameters
1.1.1.42  root     11064:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11065:                        
1.1.1.42  root     11066:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11067:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11068:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11069:                                
                   11070:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   11071:                                switch(geo->MediaType) {
                   11072:                                case F5_360_512:
                   11073:                                case F5_320_512:
                   11074:                                case F5_320_1024:
                   11075:                                case F5_180_512:
                   11076:                                case F5_160_512:
                   11077:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   11078:                                        break;
                   11079:                                case F5_1Pt2_512:
                   11080:                                case F3_1Pt2_512:
                   11081:                                case F3_1Pt23_1024:
                   11082:                                case F5_1Pt23_1024:
                   11083:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   11084:                                        break;
                   11085:                                case F3_720_512:
                   11086:                                case F3_640_512:
                   11087:                                case F5_640_512:
                   11088:                                case F5_720_512:
                   11089:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   11090:                                        break;
                   11091:                                case F8_256_128:
                   11092:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   11093:                                        break;
                   11094:                                case FixedMedia:
                   11095:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11096:                                        break;
                   11097:                                case F3_1Pt44_512:
                   11098:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11099:                                        break;
                   11100:                                case F3_2Pt88_512:
                   11101:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   11102:                                        break;
                   11103:                                default:
                   11104:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11105: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11106:                                        break;
1.1.1.22  root     11107:                                }
1.1.1.42  root     11108:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   11109:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   11110:                                switch(geo->MediaType) {
                   11111:                                case F5_360_512:
                   11112:                                case F5_320_512:
                   11113:                                case F5_320_1024:
                   11114:                                case F5_180_512:
                   11115:                                case F5_160_512:
                   11116:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   11117:                                        break;
                   11118:                                default:
                   11119:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   11120:                                        break;
                   11121:                                }
                   11122:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   11123:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   11124:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   11125:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   11126:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   11127:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   11128:                                switch(geo->MediaType) {
                   11129:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   11130:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   11131:                                        break;
                   11132:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   11133:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   11134:                                        break;
                   11135:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   11136:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11137:                                        break;
                   11138:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11139:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11140:                                        break;
                   11141:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11142:                                case F3_1Pt2_512:
                   11143:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11144:                                case F5_720_512:
                   11145:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11146:                                        break;
                   11147:                                case FixedMedia:        // hard disk
                   11148:                                case RemovableMedia:
                   11149:                                case Unknown:
                   11150:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11151:                                        break;
                   11152:                                default:
                   11153:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11154:                                        break;
                   11155:                                }
                   11156:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11157:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11158:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11159:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11160:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11161:                                // 21h  BYTE    device type
                   11162:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11163:                        } else {
                   11164:                                REG16(AX) = 0x0f; // invalid drive
                   11165:                                m_CF = 1;
                   11166:                        }
1.1.1.48  root     11167: //             } else if(REG8(CL) == 0x61) {
                   11168: //                     // read logical device track
                   11169: //             } else if(REG8(CL) == 0x62) {
                   11170: //                     // verify logical device track
1.1.1.22  root     11171:                } else if(REG8(CL) == 0x66) {
                   11172:                        // get volume serial number
                   11173:                        char path[] = "A:\\";
                   11174:                        char volume_label[MAX_PATH];
                   11175:                        DWORD serial_number = 0;
                   11176:                        char file_system[MAX_PATH];
                   11177:                        
                   11178:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11179:                        
                   11180:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   11181:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11182:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11183:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11184:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11185:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11186:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11187:                        } else {
                   11188:                                REG16(AX) = 0x0f; // invalid drive
                   11189:                                m_CF = 1;
                   11190:                        }
                   11191:                } else if(REG8(CL) == 0x67) {
                   11192:                        // get access flag
                   11193:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11194:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11195:                } else if(REG8(CL) == 0x68) {
                   11196:                        // sense media type
1.1.1.42  root     11197:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11198:                        
1.1.1.42  root     11199:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11200:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11201:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11202:                                
                   11203:                                switch(geo->MediaType) {
                   11204:                                case F3_720_512:
                   11205:                                case F5_720_512:
                   11206:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11207:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   11208:                                        break;
                   11209:                                case F3_1Pt44_512:
                   11210:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11211:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   11212:                                        break;
                   11213:                                case F3_2Pt88_512:
                   11214:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11215:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   11216:                                        break;
                   11217:                                default:
                   11218:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   11219:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   11220:                                        break;
1.1.1.22  root     11221:                                }
                   11222:                        } else {
                   11223:                                REG16(AX) = 0x0f; // invalid drive
                   11224:                                m_CF = 1;
                   11225:                        }
                   11226:                } else if(REG8(CL) == 0x6a) {
                   11227:                        // unlock logical volume
                   11228:                } else if(REG8(CL) == 0x6b) {
                   11229:                        // unlock physical volume
1.1.1.48  root     11230: //             } else if(REG8(CL) == 0x6c) {
                   11231: //                     // get lock flag
                   11232: //             } else if(REG8(CL) == 0x6d) {
                   11233: //                     // enumerate open files
                   11234: //             } else if(REG8(CL) == 0x6e) {
                   11235: //                     // find swap file
                   11236: //             } else if(REG8(CL) == 0x6f) {
                   11237: //                     // get drive map information
                   11238: //             } else if(REG8(CL) == 0x70) {
                   11239: //                     // get current lock state
                   11240: //             } else if(REG8(CL) == 0x71) {
                   11241: //                     // get first cluster
1.1.1.22  root     11242:                } else {
                   11243:                        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));
                   11244:                        REG16(AX) = 0x01; // invalid function
                   11245:                        m_CF = 1;
                   11246:                }
                   11247:                break;
1.1.1.48  root     11248:        case 0x0e: // Get Lgical Drive Map
1.1.1.44  root     11249:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11250:                        REG16(AX) = 0x0f; // invalid drive
                   11251:                        m_CF = 1;
                   11252:                } else {
                   11253:                        REG8(AL) = 0;
1.1.1.22  root     11254:                }
                   11255:                break;
1.1.1.48  root     11256:        case 0x0f: // Set Logical Drive Map
1.1.1.44  root     11257:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11258:                        REG16(AX) = 0x0f; // invalid drive
                   11259:                        m_CF = 1;
1.1.1.22  root     11260:                }
                   11261:                break;
1.1.1.48  root     11262:        case 0x10: // Query Generic IOCTRL Capability (Handle)
1.1.1.22  root     11263:                switch(REG8(CL)) {
                   11264:                case 0x45:
                   11265:                case 0x4a:
1.1.1.48  root     11266:                case 0x4c:
                   11267:                case 0x4d:
1.1.1.22  root     11268:                case 0x65:
                   11269:                case 0x6a:
1.1.1.48  root     11270:                case 0x6b:
1.1.1.22  root     11271:                case 0x7f:
                   11272:                        REG16(AX) = 0x0000; // supported
                   11273:                        break;
                   11274:                default:
                   11275:                        REG8(AL) = 0x01; // ioctl capability not available
                   11276:                        m_CF = 1;
                   11277:                        break;
                   11278:                }
                   11279:                break;
1.1.1.48  root     11280:        case 0x11: // Query Generic IOCTRL Capability (Drive)
1.1.1.22  root     11281:                switch(REG8(CL)) {
                   11282:                case 0x40:
                   11283:                case 0x46:
                   11284:                case 0x4a:
                   11285:                case 0x4b:
                   11286:                case 0x60:
                   11287:                case 0x66:
                   11288:                case 0x67:
                   11289:                case 0x68:
                   11290:                case 0x6a:
                   11291:                case 0x6b:
1.1.1.48  root     11292:                        if(REG8(CH) == 0x00 || REG8(CH) == 0x01 || REG8(CH) == 0x03 || REG8(CH) == 0x05) {
                   11293:                                // CH = 00h     Unknown
                   11294:                                // CH = 01h     COMn:
                   11295:                                // CH = 03h     CON
                   11296:                                // CH = 05h     LPTn:
                   11297:                                REG16(AX) = 0x0000; // supported
                   11298:                                break;
                   11299:                        }
1.1.1.22  root     11300:                default:
                   11301:                        REG8(AL) = 0x01; // ioctl capability not available
                   11302:                        m_CF = 1;
                   11303:                        break;
                   11304:                }
                   11305:                break;
1.1.1.48  root     11306:        case 0x12: // DR DOS 5.0-6.0 - Determine DOS Type
                   11307:        case 0x14: // DR DOS 5.0-6.0 - Set Global Password
                   11308:        case 0x16: // DR DOS 5.0-6.0 - History Buffer, Share, And Hiload Control
                   11309:        case 0x51: // Concurrent DOS v3.2+ - Installation Check
                   11310:        case 0x52: // DR DOS 3.41+ - Determine DOS tTpe/Get DR DOS Version
                   11311:        case 0x54: // DR DOS 3.41+ - Set Global Password
                   11312:        case 0x56: // DR DOS 5.0+ - History Buffer Control
                   11313:        case 0x57: // DR DOS 5.0-6.0 - Share/Hiload Control
                   11314:        case 0x58: // DR DOS 5.0+ internal - Get Pointer To Internal Variable Table
                   11315:        case 0x59: // DR Multiuser DOS 5.0 - API
                   11316:                REG16(AX) = 0x01; // this is not DR-DOS
1.1.1.22  root     11317:                m_CF = 1;
                   11318:                break;
1.1       root     11319:        default:
1.1.1.22  root     11320:                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     11321:                REG16(AX) = 0x01;
1.1.1.3   root     11322:                m_CF = 1;
1.1       root     11323:                break;
                   11324:        }
                   11325: }
                   11326: 
                   11327: inline void msdos_int_21h_45h()
                   11328: {
                   11329:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11330:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11331:        
1.1.1.20  root     11332:        if(fd < process->max_files && file_handler[fd].valid) {
                   11333:                int dup_fd = _dup(fd);
                   11334:                if(dup_fd != -1) {
                   11335:                        REG16(AX) = dup_fd;
                   11336:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11337: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11338:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11339:                } else {
                   11340:                        REG16(AX) = errno;
1.1.1.3   root     11341:                        m_CF = 1;
1.1       root     11342:                }
                   11343:        } else {
                   11344:                REG16(AX) = 0x06;
1.1.1.3   root     11345:                m_CF = 1;
1.1       root     11346:        }
                   11347: }
                   11348: 
                   11349: inline void msdos_int_21h_46h()
                   11350: {
                   11351:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11352:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11353:        int dup_fd = REG16(CX);
                   11354:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     11355:        
1.1.1.20  root     11356:        if(REG16(BX) == REG16(CX)) {
                   11357:                REG16(AX) = 0x06;
                   11358:                m_CF = 1;
                   11359:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   11360:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   11361:                        _close(tmp_fd);
                   11362:                        msdos_file_handler_close(tmp_fd);
                   11363:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   11364:                }
                   11365:                if(_dup2(fd, dup_fd) != -1) {
                   11366:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11367: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11368:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11369:                } else {
                   11370:                        REG16(AX) = errno;
1.1.1.3   root     11371:                        m_CF = 1;
1.1       root     11372:                }
                   11373:        } else {
                   11374:                REG16(AX) = 0x06;
1.1.1.3   root     11375:                m_CF = 1;
1.1       root     11376:        }
                   11377: }
                   11378: 
                   11379: inline void msdos_int_21h_47h(int lfn)
                   11380: {
                   11381:        char path[MAX_PATH];
                   11382:        
                   11383:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45  root     11384:                if(!lfn) {
                   11385:                        strcpy(path, msdos_short_path(path));
                   11386:                }
1.1       root     11387:                if(path[1] == ':') {
                   11388:                        // the returned path does not include a drive or the initial backslash
1.1.1.45  root     11389:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     11390:                } else {
1.1.1.45  root     11391:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     11392:                }
                   11393:        } else {
                   11394:                REG16(AX) = errno;
1.1.1.3   root     11395:                m_CF = 1;
1.1       root     11396:        }
                   11397: }
                   11398: 
                   11399: inline void msdos_int_21h_48h()
                   11400: {
1.1.1.19  root     11401:        int seg, umb_linked;
1.1       root     11402:        
1.1.1.8   root     11403:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     11404:                // unlink umb not to allocate memory in umb
                   11405:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   11406:                        msdos_mem_unlink_umb();
                   11407:                }
1.1.1.8   root     11408:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11409:                        REG16(AX) = seg;
                   11410:                } else {
                   11411:                        REG16(AX) = 0x08;
                   11412:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   11413:                        m_CF = 1;
                   11414:                }
1.1.1.19  root     11415:                if(umb_linked != 0) {
                   11416:                        msdos_mem_link_umb();
                   11417:                }
1.1.1.8   root     11418:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   11419:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11420:                        REG16(AX) = seg;
                   11421:                } else {
                   11422:                        REG16(AX) = 0x08;
                   11423:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   11424:                        m_CF = 1;
                   11425:                }
                   11426:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   11427:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11428:                        REG16(AX) = seg;
                   11429:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11430:                        REG16(AX) = seg;
                   11431:                } else {
                   11432:                        REG16(AX) = 0x08;
                   11433:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   11434:                        m_CF = 1;
                   11435:                }
1.1       root     11436:        }
                   11437: }
                   11438: 
                   11439: inline void msdos_int_21h_49h()
                   11440: {
1.1.1.14  root     11441:        int mcb_seg = SREG(ES) - 1;
                   11442:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   11443:        
                   11444:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11445:                msdos_mem_free(SREG(ES));
                   11446:        } else {
1.1.1.33  root     11447:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     11448:                m_CF = 1;
                   11449:        }
1.1       root     11450: }
                   11451: 
                   11452: inline void msdos_int_21h_4ah()
                   11453: {
1.1.1.14  root     11454:        int mcb_seg = SREG(ES) - 1;
                   11455:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     11456:        int max_paragraphs;
                   11457:        
1.1.1.14  root     11458:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11459:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   11460:                        REG16(AX) = 0x08;
                   11461:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   11462:                        m_CF = 1;
                   11463:                }
                   11464:        } else {
1.1.1.33  root     11465:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     11466:                m_CF = 1;
1.1       root     11467:        }
                   11468: }
                   11469: 
                   11470: inline void msdos_int_21h_4bh()
                   11471: {
1.1.1.3   root     11472:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11473:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     11474:        
                   11475:        switch(REG8(AL)) {
                   11476:        case 0x00:
                   11477:        case 0x01:
                   11478:                if(msdos_process_exec(command, param, REG8(AL))) {
                   11479:                        REG16(AX) = 0x02;
1.1.1.3   root     11480:                        m_CF = 1;
1.1       root     11481:                }
                   11482:                break;
1.1.1.14  root     11483:        case 0x03:
                   11484:                {
                   11485:                        int fd;
                   11486:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   11487:                                REG16(AX) = 0x02;
                   11488:                                m_CF = 1;
                   11489:                                break;
                   11490:                        }
                   11491:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   11492:                        _close(fd);
                   11493:                        
                   11494:                        UINT16 *overlay = (UINT16 *)param;
                   11495:                        
                   11496:                        // check exe header
                   11497:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   11498:                        int header_size = 0;
                   11499:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   11500:                                header_size = header->header_size * 16;
                   11501:                                // relocation
                   11502:                                int start_seg = overlay[1];
                   11503:                                for(int i = 0; i < header->relocations; i++) {
                   11504:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   11505:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   11506:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   11507:                                }
                   11508:                        }
                   11509:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   11510:                }
                   11511:                break;
1.1.1.48  root     11512:        case 0x04:
                   11513:                // Load And Execute In Background (European MS-DOS 4.0 only)
                   11514: //     case 0x05:
                   11515: //             // DOS 5+ - Set Execution State
                   11516:        case 0x80:
                   11517:                // DR DOS v3.41 - Run Already-Loaded Kernel File
                   11518:        case 0xf0:
                   11519:        case 0xf1:
                   11520:                // DIET v1.10+
1.1.1.43  root     11521:        case 0xfd:
                   11522:        case 0xfe:
                   11523:                // unknown function called in FreeCOM
                   11524:                REG16(AX) = 0x01;
                   11525:                m_CF = 1;
                   11526:                break;
1.1       root     11527:        default:
1.1.1.22  root     11528:                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     11529:                REG16(AX) = 0x01;
1.1.1.3   root     11530:                m_CF = 1;
1.1       root     11531:                break;
                   11532:        }
                   11533: }
                   11534: 
                   11535: inline void msdos_int_21h_4ch()
                   11536: {
                   11537:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   11538: }
                   11539: 
                   11540: inline void msdos_int_21h_4dh()
                   11541: {
                   11542:        REG16(AX) = retval;
                   11543: }
                   11544: 
                   11545: inline void msdos_int_21h_4eh()
                   11546: {
                   11547:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11548:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11549:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45  root     11550:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11551:        WIN32_FIND_DATA fd;
                   11552:        
1.1.1.14  root     11553:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   11554:        find->find_magic = FIND_MAGIC;
                   11555:        find->dta_index = dtainfo - dtalist;
1.1       root     11556:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     11557:        dtainfo->allowable_mask = REG8(CL);
                   11558:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     11559:        
1.1.1.14  root     11560:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   11561:                dtainfo->allowable_mask &= ~8;
1.1       root     11562:        }
1.1.1.14  root     11563:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   11564:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11565:                      !msdos_find_file_has_8dot3name(&fd)) {
                   11566:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11567:                                FindClose(dtainfo->find_handle);
                   11568:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11569:                                break;
                   11570:                        }
                   11571:                }
                   11572:        }
1.1.1.13  root     11573:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11574:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11575:                msdos_find_file_conv_local_time(&fd);
                   11576:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11577:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11578:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11579:                REG16(AX) = 0;
1.1.1.14  root     11580:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11581:                find->attrib = 8;
                   11582:                find->size = 0;
                   11583:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11584:                dtainfo->allowable_mask &= ~8;
1.1       root     11585:                REG16(AX) = 0;
                   11586:        } else {
                   11587:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     11588:                m_CF = 1;
1.1       root     11589:        }
                   11590: }
                   11591: 
                   11592: inline void msdos_int_21h_4fh()
                   11593: {
                   11594:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11595:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11596:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     11597:        WIN32_FIND_DATA fd;
                   11598:        
1.1.1.14  root     11599:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   11600:                REG16(AX) = 0x12;
                   11601:                m_CF = 1;
                   11602:                return;
                   11603:        }
                   11604:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     11605:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11606:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     11607:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11608:                              !msdos_find_file_has_8dot3name(&fd)) {
                   11609:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11610:                                        FindClose(dtainfo->find_handle);
                   11611:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11612:                                        break;
                   11613:                                }
                   11614:                        }
                   11615:                } else {
1.1.1.13  root     11616:                        FindClose(dtainfo->find_handle);
                   11617:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11618:                }
                   11619:        }
1.1.1.13  root     11620:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11621:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11622:                msdos_find_file_conv_local_time(&fd);
                   11623:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11624:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11625:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11626:                REG16(AX) = 0;
1.1.1.14  root     11627:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11628:                find->attrib = 8;
                   11629:                find->size = 0;
                   11630:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11631:                dtainfo->allowable_mask &= ~8;
1.1       root     11632:                REG16(AX) = 0;
                   11633:        } else {
                   11634:                REG16(AX) = 0x12;
1.1.1.3   root     11635:                m_CF = 1;
1.1       root     11636:        }
                   11637: }
                   11638: 
                   11639: inline void msdos_int_21h_50h()
                   11640: {
1.1.1.8   root     11641:        if(current_psp != REG16(BX)) {
                   11642:                process_t *process = msdos_process_info_get(current_psp);
                   11643:                if(process != NULL) {
                   11644:                        process->psp = REG16(BX);
                   11645:                }
                   11646:                current_psp = REG16(BX);
1.1.1.23  root     11647:                msdos_sda_update(current_psp);
1.1.1.8   root     11648:        }
1.1       root     11649: }
                   11650: 
                   11651: inline void msdos_int_21h_51h()
                   11652: {
                   11653:        REG16(BX) = current_psp;
                   11654: }
                   11655: 
                   11656: inline void msdos_int_21h_52h()
                   11657: {
1.1.1.25  root     11658:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     11659:        i386_load_segment_descriptor(ES);
1.1.1.25  root     11660:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     11661: }
                   11662: 
1.1.1.43  root     11663: inline void msdos_int_21h_53h()
                   11664: {
                   11665:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   11666:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   11667:        
                   11668:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   11669:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   11670:        dpb->shift_count = 0;
                   11671:        dpb->reserved_sectors = 0;
                   11672:        dpb->fat_num = bpb->fat_num;
                   11673:        dpb->root_entries = bpb->root_entries;
                   11674:        dpb->first_data_sector = 0;
                   11675:        if(bpb->sectors_per_cluster != 0) {
                   11676:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   11677:        } else {
                   11678:                dpb->highest_cluster_num = 0;
                   11679:        }
                   11680:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   11681:        dpb->first_dir_sector = 0;
                   11682:        dpb->device_driver_header = 0;
                   11683:        dpb->media_type = bpb->media_type;
                   11684:        dpb->drive_accessed = 0;
                   11685:        dpb->next_dpb_ofs = 0xffff;
                   11686:        dpb->next_dpb_seg = 0xffff;
                   11687:        dpb->first_free_cluster = 0;
                   11688:        dpb->free_clusters = 0xffff;
                   11689: }
                   11690: 
1.1       root     11691: inline void msdos_int_21h_54h()
                   11692: {
                   11693:        process_t *process = msdos_process_info_get(current_psp);
                   11694:        
                   11695:        REG8(AL) = process->verify;
                   11696: }
                   11697: 
                   11698: inline void msdos_int_21h_55h()
                   11699: {
                   11700:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   11701:        
                   11702:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   11703:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   11704:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   11705:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   11706:        psp->parent_psp = current_psp;
                   11707: }
                   11708: 
                   11709: inline void msdos_int_21h_56h(int lfn)
                   11710: {
                   11711:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     11712:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   11713:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     11714:        
                   11715:        if(rename(src, dst)) {
                   11716:                REG16(AX) = errno;
1.1.1.3   root     11717:                m_CF = 1;
1.1       root     11718:        }
                   11719: }
                   11720: 
                   11721: inline void msdos_int_21h_57h()
                   11722: {
                   11723:        FILETIME time, local;
1.1.1.14  root     11724:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     11725:        HANDLE hHandle;
1.1       root     11726:        
1.1.1.21  root     11727:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     11728:                REG16(AX) = (UINT16)GetLastError();
                   11729:                m_CF = 1;
                   11730:                return;
                   11731:        }
                   11732:        ctime = atime = mtime = NULL;
                   11733:        
1.1       root     11734:        switch(REG8(AL)) {
                   11735:        case 0x00:
1.1.1.6   root     11736:        case 0x01:
1.1.1.14  root     11737:                mtime = &time;
1.1.1.6   root     11738:                break;
                   11739:        case 0x04:
                   11740:        case 0x05:
1.1.1.14  root     11741:                atime = &time;
1.1       root     11742:                break;
1.1.1.6   root     11743:        case 0x06:
                   11744:        case 0x07:
1.1.1.14  root     11745:                ctime = &time;
                   11746:                break;
                   11747:        default:
1.1.1.22  root     11748:                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     11749:                REG16(AX) = 0x01;
                   11750:                m_CF = 1;
                   11751:                return;
                   11752:        }
                   11753:        if(REG8(AL) & 1) {
1.1       root     11754:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   11755:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     11756:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     11757:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11758:                        m_CF = 1;
1.1       root     11759:                }
1.1.1.14  root     11760:        } else {
1.1.1.21  root     11761:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     11762:                        // assume a device and use the current time
                   11763:                        GetSystemTimeAsFileTime(&time);
                   11764:                }
                   11765:                FileTimeToLocalFileTime(&time, &local);
                   11766:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     11767:        }
                   11768: }
                   11769: 
                   11770: inline void msdos_int_21h_58h()
                   11771: {
                   11772:        switch(REG8(AL)) {
                   11773:        case 0x00:
1.1.1.7   root     11774:                REG16(AX) = malloc_strategy;
                   11775:                break;
                   11776:        case 0x01:
1.1.1.24  root     11777: //             switch(REG16(BX)) {
                   11778:                switch(REG8(BL)) {
1.1.1.7   root     11779:                case 0x0000:
                   11780:                case 0x0001:
                   11781:                case 0x0002:
                   11782:                case 0x0040:
                   11783:                case 0x0041:
                   11784:                case 0x0042:
                   11785:                case 0x0080:
                   11786:                case 0x0081:
                   11787:                case 0x0082:
                   11788:                        malloc_strategy = REG16(BX);
1.1.1.23  root     11789:                        msdos_sda_update(current_psp);
1.1.1.7   root     11790:                        break;
                   11791:                default:
1.1.1.22  root     11792:                        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     11793:                        REG16(AX) = 0x01;
                   11794:                        m_CF = 1;
                   11795:                        break;
                   11796:                }
                   11797:                break;
                   11798:        case 0x02:
1.1.1.19  root     11799:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     11800:                break;
                   11801:        case 0x03:
1.1.1.24  root     11802: //             switch(REG16(BX)) {
                   11803:                switch(REG8(BL)) {
1.1.1.7   root     11804:                case 0x0000:
1.1.1.19  root     11805:                        msdos_mem_unlink_umb();
                   11806:                        break;
1.1.1.7   root     11807:                case 0x0001:
1.1.1.19  root     11808:                        msdos_mem_link_umb();
1.1.1.7   root     11809:                        break;
                   11810:                default:
1.1.1.22  root     11811:                        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     11812:                        REG16(AX) = 0x01;
                   11813:                        m_CF = 1;
                   11814:                        break;
                   11815:                }
1.1       root     11816:                break;
                   11817:        default:
1.1.1.22  root     11818:                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     11819:                REG16(AX) = 0x01;
1.1.1.3   root     11820:                m_CF = 1;
1.1       root     11821:                break;
                   11822:        }
                   11823: }
                   11824: 
                   11825: inline void msdos_int_21h_59h()
                   11826: {
1.1.1.47  root     11827:        if(REG16(BX) == 0x0000) {
                   11828:                sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   11829:                
                   11830:                REG16(AX) = sda->extended_error_code;
                   11831:                REG8(BH)  = sda->error_class;
                   11832:                REG8(BL)  = sda->suggested_action;
                   11833:                REG8(CH)  = sda->locus_of_last_error;
                   11834:                // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
                   11835:                if(sda->int21h_5d0ah_called != 0) {
                   11836:                        REG8(CL)  = sda->int21h_5d0ah_cl;
                   11837:                        REG16(DX) = sda->int21h_5d0ah_dx;
                   11838: //                     REG16(SI) = sda->int21h_5d0ah_si;
                   11839:                        REG16(DI) = sda->last_error_pointer.w.l;
                   11840: //                     SREG(DS)  = sda->int21h_5d0ah_ds;
                   11841: //                     i386_load_segment_descriptor(DS);
                   11842:                        SREG(ES)  = sda->last_error_pointer.w.h;
                   11843:                        i386_load_segment_descriptor(ES);
                   11844:                }
                   11845:                sda->int21h_5d0ah_called = 0;
                   11846: //     } else if(REG16(BX) == 0x0001) {
                   11847: //             // European MS-DOS 4.0 - Get Hard Error Information
                   11848:        } else {
                   11849:                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));
                   11850:                REG16(AX) = 0x01;
                   11851:                m_CF = 1;
                   11852:        }
1.1       root     11853: }
                   11854: 
                   11855: inline void msdos_int_21h_5ah()
                   11856: {
1.1.1.3   root     11857:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11858:        int len = strlen(path);
                   11859:        char tmp[MAX_PATH];
                   11860:        
                   11861:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   11862:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11863:                
                   11864:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11865:                REG16(AX) = fd;
                   11866:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11867:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11868:                
                   11869:                strcpy(path, tmp);
                   11870:                int dx = REG16(DX) + len;
1.1.1.3   root     11871:                int ds = SREG(DS);
1.1       root     11872:                while(dx > 0xffff) {
                   11873:                        dx -= 0x10;
                   11874:                        ds++;
                   11875:                }
                   11876:                REG16(DX) = dx;
1.1.1.3   root     11877:                SREG(DS) = ds;
                   11878:                i386_load_segment_descriptor(DS);
1.1       root     11879:        } else {
                   11880:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11881:                m_CF = 1;
1.1       root     11882:        }
                   11883: }
                   11884: 
                   11885: inline void msdos_int_21h_5bh()
                   11886: {
1.1.1.45  root     11887:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11888:        
1.1.1.45  root     11889: //     if(msdos_is_existing_file(path)) {
                   11890:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     11891:                // already exists
                   11892:                REG16(AX) = 0x50;
1.1.1.3   root     11893:                m_CF = 1;
1.1       root     11894:        } else {
                   11895:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11896:                
                   11897:                if(fd != -1) {
                   11898:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11899:                        REG16(AX) = fd;
                   11900:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11901:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11902:                } else {
                   11903:                        REG16(AX) = errno;
1.1.1.3   root     11904:                        m_CF = 1;
1.1       root     11905:                }
                   11906:        }
                   11907: }
                   11908: 
                   11909: inline void msdos_int_21h_5ch()
                   11910: {
                   11911:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11912:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11913:        
1.1.1.20  root     11914:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     11915:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     11916:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     11917:                        UINT32 pos = _tell(fd);
                   11918:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   11919:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     11920:                                REG16(AX) = errno;
1.1.1.3   root     11921:                                m_CF = 1;
1.1       root     11922:                        }
1.1.1.20  root     11923:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     11924:                        
1.1       root     11925:                        // some seconds may be passed in _locking()
1.1.1.35  root     11926:                        REQUEST_HARDWRE_UPDATE();
1.1       root     11927:                } else {
                   11928:                        REG16(AX) = 0x01;
1.1.1.3   root     11929:                        m_CF = 1;
1.1       root     11930:                }
                   11931:        } else {
                   11932:                REG16(AX) = 0x06;
1.1.1.3   root     11933:                m_CF = 1;
1.1       root     11934:        }
                   11935: }
                   11936: 
1.1.1.22  root     11937: inline void msdos_int_21h_5dh()
                   11938: {
                   11939:        switch(REG8(AL)) {
1.1.1.45  root     11940:        case 0x00:
                   11941:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
                   11942:                        // current system
                   11943:                        static bool reenter = false;
                   11944:                        if(!reenter) {
                   11945:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
                   11946:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
                   11947:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
                   11948:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
                   11949:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
                   11950:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
                   11951:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
                   11952:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
                   11953:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
                   11954:                                i386_load_segment_descriptor(DS);
                   11955:                                i386_load_segment_descriptor(ES);
                   11956:                                reenter = true;
                   11957:                                try {
                   11958:                                        msdos_syscall(0x21);
                   11959:                                } catch(...) {
                   11960:                                }
                   11961:                                reenter = false;
                   11962:                        }
                   11963:                } else {
                   11964:                        REG16(AX) = 0x49; //  network software not installed
                   11965:                        m_CF = 1;
                   11966:                }
                   11967:                break;
1.1.1.22  root     11968:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     11969:                SREG(DS) = (SDA_TOP >> 4);
                   11970:                i386_load_segment_descriptor(DS);
                   11971:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   11972:                REG16(CX) = 0x80;
                   11973:                REG16(DX) = 0x1a;
                   11974:                break;
1.1.1.45  root     11975:        case 0x07: // get redirected printer mode
                   11976:        case 0x08: // set redirected printer mode
                   11977:        case 0x09: // flush redirected printer output
                   11978:                REG16(AX) = 0x49; //  network software not installed
                   11979:                m_CF = 1;
                   11980:                break;
1.1.1.43  root     11981:        case 0x0a: // set extended error information
                   11982:                {
                   11983:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     11984:                        sda->int21h_5d0ah_called    = 1;
1.1.1.43  root     11985:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47  root     11986:                        // XXX: which one is correct ???
                   11987: #if 1
                   11988:                        // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43  root     11989:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   11990:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47  root     11991:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43  root     11992:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47  root     11993: #else
                   11994:                        // PC DOS 7 Technical Update
                   11995:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
                   11996:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
                   11997:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
                   11998:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
                   11999: #endif
                   12000:                        sda->int21h_5d0ah_dx        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   12001: //                     sda->int21h_5d0ah_si        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43  root     12002:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47  root     12003: //                     sda->int21h_5d0ah_ds        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43  root     12004:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   12005:                }
                   12006:                break;
1.1.1.23  root     12007:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     12008:                REG16(AX) = 0x01;
                   12009:                m_CF = 1;
                   12010:                break;
                   12011:        default:
                   12012:                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));
                   12013:                REG16(AX) = 0x01;
                   12014:                m_CF = 1;
                   12015:                break;
                   12016:        }
                   12017: }
                   12018: 
1.1.1.42  root     12019: inline void msdos_int_21h_5eh()
                   12020: {
                   12021:        switch(REG8(AL)) {
                   12022:        case 0x00:
                   12023:                {
                   12024:                        char name[256] = {0};
                   12025:                        DWORD dwSize = 256;
                   12026:                        
                   12027:                        if(GetComputerName(name, &dwSize)) {
                   12028:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12029:                                for(int i = 0; i < 15; i++) {
                   12030:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   12031:                                }
                   12032:                                dest[15] = '\0';
                   12033:                                REG8(CH) = 0x01; // nonzero valid
                   12034:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   12035:                        } else {
                   12036:                                REG16(AX) = 0x01;
                   12037:                                m_CF = 1;
                   12038:                        }
                   12039:                }
                   12040:                break;
                   12041:        default:
1.1.1.45  root     12042: //             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));
                   12043: //             REG16(AX) = 0x01;
                   12044:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     12045:                m_CF = 1;
                   12046:                break;
                   12047:        }
                   12048: }
                   12049: 
1.1.1.30  root     12050: inline void msdos_int_21h_5fh()
                   12051: {
                   12052:        switch(REG8(AL)) {
1.1.1.42  root     12053:        case 0x05:
1.1.1.44  root     12054:                REG16(BP) = 0;
                   12055:                for(int i = 0; i < 26; i++) {
                   12056:                        if(msdos_is_remote_drive(i)) {
                   12057:                                REG16(BP)++;
1.1.1.42  root     12058:                        }
                   12059:                }
1.1.1.30  root     12060:        case 0x02:
1.1.1.44  root     12061:                for(int i = 0, index = 0; i < 26; i++) {
                   12062:                        if(msdos_is_remote_drive(i)) {
                   12063:                                if(index == REG16(BX)) {
                   12064:                                        char volume[] = "A:";
1.1.1.30  root     12065:                                        volume[0] = 'A' + i;
1.1.1.44  root     12066:                                        DWORD dwSize = 128;
                   12067:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   12068:                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   12069:                                        REG8(BH) = 0x00; // valid
                   12070:                                        REG8(BL) = 0x04; // disk drive
                   12071:                                        REG16(CX) = 0x00; // LANtastic
                   12072:                                        return;
1.1.1.30  root     12073:                                }
1.1.1.44  root     12074:                                index++;
1.1.1.30  root     12075:                        }
                   12076:                }
                   12077:                REG16(AX) = 0x12; // no more files
                   12078:                m_CF = 1;
                   12079:                break;
1.1.1.44  root     12080:        case 0x07:
                   12081:                if(msdos_is_valid_drive(REG8(DL))) {
                   12082:                        msdos_cds_update(REG8(DL));
                   12083:                } else {
                   12084:                        REG16(AX) = 0x0f; // invalid drive
                   12085:                        m_CF = 1;
                   12086:                }
                   12087:                break;
                   12088:        case 0x08:
                   12089:                if(msdos_is_valid_drive(REG8(DL))) {
                   12090:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   12091:                        cds->drive_attrib = 0x0000;
                   12092:                } else {
                   12093:                        REG16(AX) = 0x0f; // invalid drive
                   12094:                        m_CF = 1;
                   12095:                }
                   12096:                break;
1.1.1.30  root     12097:        default:
1.1.1.45  root     12098: //             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));
                   12099: //             REG16(AX) = 0x01;
                   12100:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     12101:                m_CF = 1;
                   12102:                break;
                   12103:        }
                   12104: }
                   12105: 
1.1       root     12106: inline void msdos_int_21h_60h(int lfn)
                   12107: {
1.1.1.45  root     12108:        char full[MAX_PATH];
                   12109:        const char *path = NULL;
1.1.1.14  root     12110:        
1.1       root     12111:        if(lfn) {
1.1.1.14  root     12112:                char *name;
                   12113:                *full = '\0';
1.1.1.3   root     12114:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     12115:                switch(REG8(CL)) {
                   12116:                case 1:
                   12117:                        GetShortPathName(full, full, MAX_PATH);
                   12118:                        my_strupr(full);
                   12119:                        break;
                   12120:                case 2:
                   12121:                        GetLongPathName(full, full, MAX_PATH);
                   12122:                        break;
                   12123:                }
                   12124:                path = full;
                   12125:        } else {
                   12126:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12127:        }
                   12128:        if(*path != '\0') {
                   12129:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     12130:        } else {
1.1.1.14  root     12131:                REG16(AX) = (UINT16)GetLastError();
                   12132:                m_CF = 1;
1.1       root     12133:        }
                   12134: }
                   12135: 
                   12136: inline void msdos_int_21h_61h()
                   12137: {
                   12138:        REG8(AL) = 0;
                   12139: }
                   12140: 
                   12141: inline void msdos_int_21h_62h()
                   12142: {
                   12143:        REG16(BX) = current_psp;
                   12144: }
                   12145: 
                   12146: inline void msdos_int_21h_63h()
                   12147: {
                   12148:        switch(REG8(AL)) {
                   12149:        case 0x00:
1.1.1.3   root     12150:                SREG(DS) = (DBCS_TABLE >> 4);
                   12151:                i386_load_segment_descriptor(DS);
1.1       root     12152:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   12153:                REG8(AL) = 0x00;
                   12154:                break;
1.1.1.22  root     12155:        case 0x01: // set korean input mode
                   12156:        case 0x02: // get korean input mode
                   12157:                REG8(AL) = 0xff; // not supported
                   12158:                break;
1.1       root     12159:        default:
1.1.1.22  root     12160:                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     12161:                REG16(AX) = 0x01;
1.1.1.3   root     12162:                m_CF = 1;
1.1       root     12163:                break;
                   12164:        }
                   12165: }
                   12166: 
1.1.1.25  root     12167: UINT16 get_extended_country_info(UINT8 func)
1.1       root     12168: {
1.1.1.25  root     12169:        switch(func) {
1.1.1.17  root     12170:        case 0x01:
                   12171:                if(REG16(CX) >= 5) {
1.1.1.19  root     12172:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     12173:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   12174:                                REG16(CX) = sizeof(data);
                   12175:                        ZeroMemory(data, sizeof(data));
                   12176:                        data[0] = 0x01;
                   12177:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     12178:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     12179:                        *(UINT16 *)(data + 5) = active_code_page;
                   12180:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     12181: //                     REG16(AX) = active_code_page;
1.1.1.17  root     12182:                } else {
1.1.1.25  root     12183:                        return(0x08); // insufficient memory
1.1.1.17  root     12184:                }
                   12185:                break;
                   12186:        case 0x02:
                   12187:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12188:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   12189:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     12190: //             REG16(AX) = active_code_page;
1.1.1.17  root     12191:                REG16(CX) = 0x05;
                   12192:                break;
1.1.1.23  root     12193:        case 0x03:
                   12194:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12195:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   12196:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     12197: //             REG16(AX) = active_code_page;
1.1.1.23  root     12198:                REG16(CX) = 0x05;
                   12199:                break;
1.1.1.17  root     12200:        case 0x04:
                   12201:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   12202:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   12203:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     12204: //             REG16(AX) = active_code_page;
1.1.1.17  root     12205:                REG16(CX) = 0x05;
                   12206:                break;
                   12207:        case 0x05:
                   12208:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   12209:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   12210:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     12211: //             REG16(AX) = active_code_page;
1.1.1.17  root     12212:                REG16(CX) = 0x05;
                   12213:                break;
                   12214:        case 0x06:
                   12215:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   12216:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   12217:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     12218: //             REG16(AX) = active_code_page;
1.1.1.17  root     12219:                REG16(CX) = 0x05;
                   12220:                break;
1.1       root     12221:        case 0x07:
1.1.1.3   root     12222:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   12223:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   12224:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     12225: //             REG16(AX) = active_code_page;
1.1       root     12226:                REG16(CX) = 0x05;
                   12227:                break;
1.1.1.25  root     12228:        default:
                   12229:                return(0x01); // function number invalid
                   12230:        }
                   12231:        return(0x00);
                   12232: }
                   12233: 
                   12234: inline void msdos_int_21h_65h()
                   12235: {
                   12236:        char tmp[0x10000];
                   12237:        
                   12238:        switch(REG8(AL)) {
1.1.1.43  root     12239:        case 0x00:
                   12240:                if(REG16(CX) >= 7) {
                   12241:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   12242:                        REG16(AX) = system_code_page;
                   12243:                } else {
                   12244:                        REG16(AX) = 0x0c;
                   12245:                        m_CF = 1;
                   12246:                }
                   12247:                break;
1.1.1.25  root     12248:        case 0x01:
                   12249:        case 0x02:
                   12250:        case 0x03:
                   12251:        case 0x04:
                   12252:        case 0x05:
                   12253:        case 0x06:
                   12254:        case 0x07:
                   12255:                {
                   12256:                        UINT16 result = get_extended_country_info(REG8(AL));
                   12257:                        if(result) {
                   12258:                                REG16(AX) = result;
                   12259:                                m_CF = 1;
                   12260:                        } else {
                   12261:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   12262:                        }
                   12263:                }
                   12264:                break;
1.1       root     12265:        case 0x20:
1.1.1.25  root     12266:        case 0xa0:
1.1.1.19  root     12267:                memset(tmp, 0, sizeof(tmp));
                   12268:                tmp[0] = REG8(DL);
1.1       root     12269:                my_strupr(tmp);
                   12270:                REG8(DL) = tmp[0];
                   12271:                break;
                   12272:        case 0x21:
1.1.1.25  root     12273:        case 0xa1:
1.1       root     12274:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12275:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     12276:                my_strupr(tmp);
1.1.1.3   root     12277:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     12278:                break;
                   12279:        case 0x22:
1.1.1.25  root     12280:        case 0xa2:
1.1.1.3   root     12281:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     12282:                break;
1.1.1.25  root     12283:        case 0x23:
                   12284:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45  root     12285:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     12286:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45  root     12287:                        REG16(AX) = 0x00;
                   12288:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   12289:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     12290:                        REG16(AX) = 0x01;
                   12291:                } else {
                   12292:                        REG16(AX) = 0x02;
                   12293:                }
                   12294:                break;
1.1       root     12295:        default:
1.1.1.22  root     12296:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     12297:                REG16(AX) = 0x01;
1.1.1.3   root     12298:                m_CF = 1;
1.1       root     12299:                break;
                   12300:        }
                   12301: }
                   12302: 
                   12303: inline void msdos_int_21h_66h()
                   12304: {
                   12305:        switch(REG8(AL)) {
                   12306:        case 0x01:
                   12307:                REG16(BX) = active_code_page;
                   12308:                REG16(DX) = system_code_page;
                   12309:                break;
                   12310:        case 0x02:
                   12311:                if(active_code_page == REG16(BX)) {
                   12312:                        REG16(AX) = 0xeb41;
                   12313:                } else if(_setmbcp(REG16(BX)) == 0) {
                   12314:                        active_code_page = REG16(BX);
1.1.1.17  root     12315:                        msdos_nls_tables_update();
1.1       root     12316:                        REG16(AX) = 0xeb41;
1.1.1.32  root     12317:                        SetConsoleCP(active_code_page);
                   12318:                        SetConsoleOutputCP(active_code_page);
1.1       root     12319:                } else {
                   12320:                        REG16(AX) = 0x25;
1.1.1.3   root     12321:                        m_CF = 1;
1.1       root     12322:                }
                   12323:                break;
                   12324:        default:
1.1.1.22  root     12325:                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     12326:                REG16(AX) = 0x01;
1.1.1.3   root     12327:                m_CF = 1;
1.1       root     12328:                break;
                   12329:        }
                   12330: }
                   12331: 
                   12332: inline void msdos_int_21h_67h()
                   12333: {
                   12334:        process_t *process = msdos_process_info_get(current_psp);
                   12335:        
                   12336:        if(REG16(BX) <= MAX_FILES) {
                   12337:                process->max_files = max(REG16(BX), 20);
                   12338:        } else {
                   12339:                REG16(AX) = 0x08;
1.1.1.3   root     12340:                m_CF = 1;
1.1       root     12341:        }
                   12342: }
                   12343: 
                   12344: inline void msdos_int_21h_68h()
                   12345: {
                   12346:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12347:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12348:        
1.1.1.20  root     12349:        if(fd < process->max_files && file_handler[fd].valid) {
                   12350:                // fflush(_fdopen(fd, ""));
1.1       root     12351:        } else {
                   12352:                REG16(AX) = 0x06;
1.1.1.3   root     12353:                m_CF = 1;
1.1       root     12354:        }
                   12355: }
                   12356: 
                   12357: inline void msdos_int_21h_69h()
                   12358: {
1.1.1.3   root     12359:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12360:        char path[] = "A:\\";
                   12361:        char volume_label[MAX_PATH];
                   12362:        DWORD serial_number = 0;
                   12363:        char file_system[MAX_PATH];
                   12364:        
                   12365:        if(REG8(BL) == 0) {
                   12366:                path[0] = 'A' + _getdrive() - 1;
                   12367:        } else {
                   12368:                path[0] = 'A' + REG8(BL) - 1;
                   12369:        }
                   12370:        
                   12371:        switch(REG8(AL)) {
                   12372:        case 0x00:
                   12373:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   12374:                        info->info_level = 0;
                   12375:                        info->serial_number = serial_number;
                   12376:                        memset(info->volume_label, 0x20, 11);
                   12377:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   12378:                        memset(info->file_system, 0x20, 8);
                   12379:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   12380:                } else {
                   12381:                        REG16(AX) = errno;
1.1.1.3   root     12382:                        m_CF = 1;
1.1       root     12383:                }
                   12384:                break;
                   12385:        case 0x01:
                   12386:                REG16(AX) = 0x03;
1.1.1.3   root     12387:                m_CF = 1;
1.1.1.45  root     12388:                break;
                   12389:        default:
                   12390:                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));
                   12391:                REG16(AX) = 0x01;
                   12392:                m_CF = 1;
                   12393:                break;
1.1       root     12394:        }
                   12395: }
                   12396: 
                   12397: inline void msdos_int_21h_6ah()
                   12398: {
                   12399:        REG8(AH) = 0x68;
                   12400:        msdos_int_21h_68h();
                   12401: }
                   12402: 
                   12403: inline void msdos_int_21h_6bh()
                   12404: {
1.1.1.45  root     12405:        REG8(AL) = 0x00;
1.1       root     12406: }
                   12407: 
                   12408: inline void msdos_int_21h_6ch(int lfn)
                   12409: {
1.1.1.45  root     12410:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     12411:        int mode = REG8(BL) & 0x03;
                   12412:        
                   12413:        if(mode < 0x03) {
1.1.1.29  root     12414:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12415:                        // file exists
                   12416:                        if(REG8(DL) & 1) {
1.1.1.37  root     12417:                                int fd = -1;
                   12418:                                int sio_port = 0;
                   12419:                                int lpt_port = 0;
1.1       root     12420:                                
1.1.1.45  root     12421:                                if(msdos_is_device_path(path)) {
                   12422:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     12423:                                } else {
1.1.1.13  root     12424:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     12425:                                }
1.1       root     12426:                                if(fd != -1) {
                   12427:                                        REG16(AX) = fd;
                   12428:                                        REG16(CX) = 1;
1.1.1.45  root     12429:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12430:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12431:                                } else {
                   12432:                                        REG16(AX) = errno;
1.1.1.3   root     12433:                                        m_CF = 1;
1.1       root     12434:                                }
                   12435:                        } else if(REG8(DL) & 2) {
                   12436:                                int attr = GetFileAttributes(path);
1.1.1.37  root     12437:                                int fd = -1;
                   12438:                                int sio_port = 0;
                   12439:                                int lpt_port = 0;
1.1       root     12440:                                
1.1.1.45  root     12441:                                if(msdos_is_device_path(path)) {
                   12442:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     12443:                                } else {
                   12444:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12445:                                }
                   12446:                                if(fd != -1) {
                   12447:                                        if(attr == -1) {
                   12448:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   12449:                                        }
                   12450:                                        SetFileAttributes(path, attr);
                   12451:                                        REG16(AX) = fd;
                   12452:                                        REG16(CX) = 3;
1.1.1.45  root     12453:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12454:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12455:                                } else {
                   12456:                                        REG16(AX) = errno;
1.1.1.3   root     12457:                                        m_CF = 1;
1.1       root     12458:                                }
                   12459:                        } else {
                   12460:                                REG16(AX) = 0x50;
1.1.1.3   root     12461:                                m_CF = 1;
1.1       root     12462:                        }
                   12463:                } else {
                   12464:                        // file not exists
                   12465:                        if(REG8(DL) & 0x10) {
                   12466:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12467:                                
                   12468:                                if(fd != -1) {
                   12469:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12470:                                        REG16(AX) = fd;
                   12471:                                        REG16(CX) = 2;
                   12472:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12473:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12474:                                } else {
                   12475:                                        REG16(AX) = errno;
1.1.1.3   root     12476:                                        m_CF = 1;
1.1       root     12477:                                }
                   12478:                        } else {
                   12479:                                REG16(AX) = 0x02;
1.1.1.3   root     12480:                                m_CF = 1;
1.1       root     12481:                        }
                   12482:                }
                   12483:        } else {
                   12484:                REG16(AX) = 0x0c;
1.1.1.3   root     12485:                m_CF = 1;
1.1       root     12486:        }
                   12487: }
                   12488: 
1.1.1.43  root     12489: inline void msdos_int_21h_70h()
                   12490: {
                   12491:        switch(REG8(AL)) {
1.1.1.48  root     12492:        case 0x00: // get ??? info
                   12493:        case 0x01: // set above info
                   12494: //             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));
                   12495:                REG16(AX) = 0x7000;
                   12496:                m_CF = 1;
                   12497:                break;
                   12498:        case 0x02: // set general internationalization info
1.1.1.43  root     12499:                if(REG16(CX) >= 7) {
                   12500:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   12501:                        msdos_nls_tables_update();
                   12502:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   12503:                        REG16(AX) = system_code_page;
                   12504:                } else {
                   12505:                        REG16(AX) = 0x0c;
                   12506:                        m_CF = 1;
                   12507:                }
                   12508:                break;
                   12509:        default:
                   12510:                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     12511:                REG16(AX) = 0x7000;
1.1.1.43  root     12512:                m_CF = 1;
                   12513:                break;
                   12514:        }
                   12515: }
                   12516: 
1.1       root     12517: inline void msdos_int_21h_710dh()
                   12518: {
                   12519:        // reset drive
                   12520: }
                   12521: 
1.1.1.48  root     12522: inline void msdos_int_21h_7141h()
1.1.1.17  root     12523: {
                   12524:        if(REG16(SI) == 0) {
1.1.1.48  root     12525:                msdos_int_21h_41h(1);
1.1.1.17  root     12526:                return;
                   12527:        }
                   12528:        if(REG16(SI) != 1) {
                   12529:                REG16(AX) = 5;
                   12530:                m_CF = 1;
                   12531:        }
                   12532:        /* wild card and matching attributes... */
                   12533:        char tmp[MAX_PATH * 2];
                   12534:        // copy search pathname (and quick check overrun)
                   12535:        ZeroMemory(tmp, sizeof(tmp));
                   12536:        tmp[MAX_PATH - 1] = '\0';
                   12537:        tmp[MAX_PATH] = 1;
                   12538:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   12539:        
                   12540:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   12541:                REG16(AX) = 1;
                   12542:                m_CF = 1;
                   12543:                return;
                   12544:        }
                   12545:        for(char *s = tmp; *s; ++s) {
                   12546:                if(*s == '/') {
                   12547:                        *s = '\\';
                   12548:                }
                   12549:        }
                   12550:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   12551:        if(tmp_name) {
                   12552:                ++tmp_name;
                   12553:        } else {
                   12554:                tmp_name = strchr(tmp, ':');
                   12555:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   12556:        }
                   12557:        
                   12558:        WIN32_FIND_DATAA fd;
                   12559:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   12560:        if(fh == INVALID_HANDLE_VALUE) {
                   12561:                REG16(AX) = 2;
                   12562:                m_CF = 1;
                   12563:                return;
                   12564:        }
                   12565:        do {
                   12566:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   12567:                        strcpy(tmp_name, fd.cFileName);
1.1.1.48  root     12568:                        if(remove(msdos_trimmed_path(tmp, 1))) {
1.1.1.17  root     12569:                                REG16(AX) = 5;
                   12570:                                m_CF = 1;
                   12571:                                break;
                   12572:                        }
                   12573:                }
                   12574:        } while(FindNextFileA(fh, &fd));
                   12575:        if(!m_CF) {
                   12576:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   12577:                        m_CF = 1;
                   12578:                        REG16(AX) = 2;
                   12579:                }
                   12580:        }
                   12581:        FindClose(fh);
                   12582: }
                   12583: 
1.1       root     12584: inline void msdos_int_21h_714eh()
                   12585: {
                   12586:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12587:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   12588:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12589:        WIN32_FIND_DATA fd;
                   12590:        
1.1.1.13  root     12591:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12592:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12593:                FindClose(dtainfo->find_handle);
                   12594:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12595:        }
                   12596:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12597:        dtainfo->allowable_mask = REG8(CL);
                   12598:        dtainfo->required_mask = REG8(CH);
                   12599:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12600:        
1.1.1.14  root     12601:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12602:                dtainfo->allowable_mask &= ~8;
1.1       root     12603:        }
1.1.1.14  root     12604:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   12605:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12606:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12607:                                FindClose(dtainfo->find_handle);
                   12608:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12609:                                break;
                   12610:                        }
                   12611:                }
                   12612:        }
1.1.1.13  root     12613:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12614:                find->attrib = fd.dwFileAttributes;
                   12615:                msdos_find_file_conv_local_time(&fd);
                   12616:                if(REG16(SI) == 0) {
                   12617:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12618:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12619:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12620:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12621:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12622:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12623:                } else {
                   12624:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12625:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12626:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12627:                }
                   12628:                find->size_hi = fd.nFileSizeHigh;
                   12629:                find->size_lo = fd.nFileSizeLow;
                   12630:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12631:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12632:                REG16(AX) = dtainfo - dtalist + 1;
                   12633:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12634:                // volume label
                   12635:                find->attrib = 8;
                   12636:                find->size_hi = find->size_lo = 0;
                   12637:                strcpy(find->full_name, process->volume_label);
                   12638:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12639:                dtainfo->allowable_mask &= ~8;
                   12640:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     12641:        } else {
                   12642:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12643:                m_CF = 1;
1.1       root     12644:        }
                   12645: }
                   12646: 
                   12647: inline void msdos_int_21h_714fh()
                   12648: {
                   12649:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12650:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     12651:        WIN32_FIND_DATA fd;
                   12652:        
1.1.1.14  root     12653:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12654:                REG16(AX) = 6;
1.1.1.13  root     12655:                m_CF = 1;
                   12656:                return;
                   12657:        }
1.1.1.14  root     12658:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12659:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12660:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     12661:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12662:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12663:                                        FindClose(dtainfo->find_handle);
                   12664:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12665:                                        break;
                   12666:                                }
                   12667:                        }
                   12668:                } else {
1.1.1.13  root     12669:                        FindClose(dtainfo->find_handle);
                   12670:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12671:                }
                   12672:        }
1.1.1.13  root     12673:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12674:                find->attrib = fd.dwFileAttributes;
                   12675:                msdos_find_file_conv_local_time(&fd);
                   12676:                if(REG16(SI) == 0) {
                   12677:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12678:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12679:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12680:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12681:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12682:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12683:                } else {
                   12684:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12685:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12686:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12687:                }
                   12688:                find->size_hi = fd.nFileSizeHigh;
                   12689:                find->size_lo = fd.nFileSizeLow;
                   12690:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12691:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12692:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12693:                // volume label
                   12694:                find->attrib = 8;
                   12695:                find->size_hi = find->size_lo = 0;
                   12696:                strcpy(find->full_name, process->volume_label);
                   12697:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12698:                dtainfo->allowable_mask &= ~8;
1.1       root     12699:        } else {
                   12700:                REG16(AX) = 0x12;
1.1.1.3   root     12701:                m_CF = 1;
1.1       root     12702:        }
                   12703: }
                   12704: 
                   12705: inline void msdos_int_21h_71a0h()
                   12706: {
                   12707:        DWORD max_component_len, file_sys_flag;
                   12708:        
1.1.1.14  root     12709:        if(GetVolumeInformation((char *)(mem + SREG_BASE(DS) + REG16(DX)), NULL, 0, NULL, &max_component_len, &file_sys_flag, REG16(CX) == 0 ? NULL : (char *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX))) {
                   12710:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   12711:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     12712:                REG16(CX) = (UINT16)max_component_len;          // 255
                   12713:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   12714:        } else {
                   12715:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12716:                m_CF = 1;
1.1       root     12717:        }
                   12718: }
                   12719: 
                   12720: inline void msdos_int_21h_71a1h()
                   12721: {
1.1.1.14  root     12722:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12723:                REG16(AX) = 6;
1.1.1.13  root     12724:                m_CF = 1;
                   12725:                return;
                   12726:        }
1.1.1.14  root     12727:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12728:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12729:                FindClose(dtainfo->find_handle);
                   12730:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12731:        }
                   12732: }
                   12733: 
                   12734: inline void msdos_int_21h_71a6h()
                   12735: {
                   12736:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12737:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   12738:        
1.1.1.3   root     12739:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12740:        struct _stat64 status;
                   12741:        DWORD serial_number = 0;
                   12742:        
1.1.1.20  root     12743:        if(fd < process->max_files && file_handler[fd].valid) {
                   12744:                if(_fstat64(fd, &status) == 0) {
                   12745:                        if(file_handler[fd].path[1] == ':') {
1.1       root     12746:                                // NOTE: we need to consider the network file path "\\host\share\"
                   12747:                                char volume[] = "A:\\";
1.1.1.20  root     12748:                                volume[0] = file_handler[fd].path[1];
1.1       root     12749:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   12750:                        }
1.1.1.20  root     12751:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     12752:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   12753:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   12754:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   12755:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   12756:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   12757:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   12758:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   12759:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   12760:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   12761:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     12762:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     12763:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     12764:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     12765:                } else {
                   12766:                        REG16(AX) = errno;
1.1.1.3   root     12767:                        m_CF = 1;
1.1       root     12768:                }
                   12769:        } else {
                   12770:                REG16(AX) = 0x06;
1.1.1.3   root     12771:                m_CF = 1;
1.1       root     12772:        }
                   12773: }
                   12774: 
                   12775: inline void msdos_int_21h_71a7h()
                   12776: {
                   12777:        switch(REG8(BL)) {
                   12778:        case 0x00:
1.1.1.3   root     12779:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     12780:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12781:                        m_CF = 1;
1.1       root     12782:                }
                   12783:                break;
                   12784:        case 0x01:
                   12785:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     12786:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     12787:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12788:                        m_CF = 1;
1.1       root     12789:                }
                   12790:                break;
                   12791:        default:
1.1.1.22  root     12792:                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     12793:                REG16(AX) = 0x7100;
1.1.1.3   root     12794:                m_CF = 1;
1.1       root     12795:                break;
                   12796:        }
                   12797: }
                   12798: 
                   12799: inline void msdos_int_21h_71a8h()
                   12800: {
                   12801:        if(REG8(DH) == 0) {
                   12802:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     12803:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     12804:                memset(fcb, 0x20, sizeof(fcb));
                   12805:                int len = strlen(tmp);
1.1.1.21  root     12806:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     12807:                        if(tmp[i] == '.') {
                   12808:                                pos = 8;
                   12809:                        } else {
                   12810:                                if(msdos_lead_byte_check(tmp[i])) {
                   12811:                                        fcb[pos++] = tmp[i++];
                   12812:                                }
                   12813:                                fcb[pos++] = tmp[i];
                   12814:                        }
                   12815:                }
1.1.1.3   root     12816:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     12817:        } else {
1.1.1.3   root     12818:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     12819:        }
                   12820: }
                   12821: 
1.1.1.22  root     12822: inline void msdos_int_21h_71aah()
                   12823: {
                   12824:        char drv[] = "A:", path[MAX_PATH];
                   12825:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12826:        
                   12827:        if(REG8(BL) == 0) {
                   12828:                drv[0] = 'A' + _getdrive() - 1;
                   12829:        } else {
                   12830:                drv[0] = 'A' + REG8(BL) - 1;
                   12831:        }
                   12832:        switch(REG8(BH)) {
                   12833:        case 0x00:
1.1.1.44  root     12834:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12835:                        REG16(AX) = 0x0f; // invalid drive
                   12836:                        m_CF = 1;
                   12837:                } else if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   12838:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     12839:                        m_CF = 1;
                   12840:                }
                   12841:                break;
                   12842:        case 0x01:
1.1.1.44  root     12843:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12844:                        REG16(AX) = 0x0f; // invalid drive
                   12845:                        m_CF = 1;
                   12846:                } else if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     12847:                        REG16(AX) = 0x0f; // invalid drive
                   12848:                        m_CF = 1;
                   12849:                }
                   12850:                break;
                   12851:        case 0x02:
1.1.1.44  root     12852:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12853:                        REG16(AX) = 0x0f; // invalid drive
                   12854:                        m_CF = 1;
                   12855:                } else if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     12856:                        REG16(AX) = 0x0f; // invalid drive
                   12857:                        m_CF = 1;
                   12858:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   12859:                        REG16(AX) = 0x0f; // invalid drive
                   12860:                        m_CF = 1;
                   12861:                } else {
                   12862:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   12863:                }
                   12864:                break;
                   12865:        default:
                   12866:                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     12867:                REG16(AX) = 0x7100;
1.1.1.22  root     12868:                m_CF = 1;
                   12869:                break;
                   12870:        }
                   12871: }
                   12872: 
1.1.1.14  root     12873: inline void msdos_int_21h_7300h()
                   12874: {
1.1.1.44  root     12875:        REG8(AL) = REG8(CL);
                   12876:        REG8(AH) = 0;
1.1.1.14  root     12877: }
                   12878: 
                   12879: inline void msdos_int_21h_7302h()
                   12880: {
                   12881:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   12882:        UINT16 seg, ofs;
                   12883:        
                   12884:        if(REG16(CX) < 0x3f) {
                   12885:                REG8(AL) = 0x18;
                   12886:                m_CF = 1;
                   12887:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   12888:                REG8(AL) = 0xff;
                   12889:                m_CF = 1;
                   12890:        } else {
                   12891:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   12892:        }
                   12893: }
                   12894: 
1.1       root     12895: inline void msdos_int_21h_7303h()
                   12896: {
1.1.1.3   root     12897:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12898:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     12899:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   12900:        
                   12901:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   12902:                info->size_of_structure = sizeof(ext_space_info_t);
                   12903:                info->structure_version = 0;
                   12904:                info->sectors_per_cluster = sectors_per_cluster;
                   12905:                info->bytes_per_sector = bytes_per_sector;
                   12906:                info->available_clusters_on_drive = free_clusters;
                   12907:                info->total_clusters_on_drive = total_clusters;
                   12908:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   12909:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   12910:                info->available_allocation_units = free_clusters;       // ???
                   12911:                info->total_allocation_units = total_clusters;          // ???
                   12912:        } else {
                   12913:                REG16(AX) = errno;
1.1.1.3   root     12914:                m_CF = 1;
1.1       root     12915:        }
                   12916: }
                   12917: 
1.1.1.30  root     12918: inline void msdos_int_21h_dbh()
                   12919: {
                   12920:        // Novell NetWare - Workstation - Get Number of Local Drives
                   12921:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   12922:        REG8(AL) = dos_info->last_drive;
                   12923: }
                   12924: 
                   12925: inline void msdos_int_21h_dch()
                   12926: {
                   12927:        // Novell NetWare - Connection Services - Get Connection Number
                   12928:        REG8(AL) = 0x00;
                   12929: }
                   12930: 
1.1.1.32  root     12931: inline void msdos_int_24h()
                   12932: {
                   12933:        const char *message = NULL;
                   12934:        int key = 0;
                   12935:        
                   12936:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   12937:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   12938:                        if(active_code_page == 932) {
                   12939:                                message = critical_error_table[i].message_japanese;
                   12940:                        }
                   12941:                        if(message == NULL) {
                   12942:                                message = critical_error_table[i].message_english;
                   12943:                        }
                   12944:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   12945:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   12946:                        
                   12947:                        SREG(ES) = WORK_TOP >> 4;
                   12948:                        i386_load_segment_descriptor(ES);
                   12949:                        REG16(DI) = 0x0000;
                   12950:                        break;
                   12951:                }
                   12952:        }
                   12953:        fprintf(stderr, "\n%s", message);
                   12954:        if(!(REG8(AH) & 0x80)) {
                   12955:                if(REG8(AH) & 0x01) {
                   12956:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   12957:                } else {
                   12958:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   12959:                }
                   12960:        }
                   12961:        fprintf(stderr, "\n");
                   12962:        
1.1.1.33  root     12963:        {
1.1.1.32  root     12964:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     12965:        }
1.1.1.32  root     12966:        if(REG8(AH) & 0x10) {
                   12967:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   12968:        }
                   12969:        if(REG8(AH) & 0x20) {
                   12970:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   12971:        }
                   12972:        if(REG8(AH) & 0x08) {
                   12973:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   12974:        }
                   12975:        fprintf(stderr, "? ");
                   12976:        
                   12977:        while(1) {
                   12978:                while(!_kbhit()) {
                   12979:                        Sleep(10);
                   12980:                }
                   12981:                key = _getch();
                   12982:                
                   12983:                if(key == 'I' || key == 'i') {
                   12984:                        if(REG8(AH) & 0x20) {
                   12985:                                REG8(AL) = 0;
                   12986:                                break;
                   12987:                        }
                   12988:                } else if(key == 'R' || key == 'r') {
                   12989:                        if(REG8(AH) & 0x10) {
                   12990:                                REG8(AL) = 1;
                   12991:                                break;
                   12992:                        }
                   12993:                } else if(key == 'A' || key == 'a') {
                   12994:                        REG8(AL) = 2;
                   12995:                        break;
                   12996:                } else if(key == 'F' || key == 'f') {
                   12997:                        if(REG8(AH) & 0x08) {
                   12998:                                REG8(AL) = 3;
                   12999:                                break;
                   13000:                        }
                   13001:                }
                   13002:        }
                   13003:        fprintf(stderr, "%c\n", key);
                   13004: }
                   13005: 
1.1       root     13006: inline void msdos_int_25h()
                   13007: {
                   13008:        UINT16 seg, ofs;
                   13009:        DWORD dwSize;
                   13010:        
1.1.1.3   root     13011: #if defined(HAS_I386)
                   13012:        I386OP(pushf)();
                   13013: #else
                   13014:        PREFIX86(_pushf());
                   13015: #endif
1.1       root     13016:        
                   13017:        if(!(REG8(AL) < 26)) {
                   13018:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13019:                m_CF = 1;
1.1       root     13020:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13021:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13022:                m_CF = 1;
1.1       root     13023:        } else {
                   13024:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13025:                char dev[64];
                   13026:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13027:                
                   13028:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   13029:                if(hFile == INVALID_HANDLE_VALUE) {
                   13030:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13031:                        m_CF = 1;
1.1       root     13032:                } else {
1.1.1.19  root     13033:                        UINT32 top_sector  = REG16(DX);
                   13034:                        UINT16 sector_num  = REG16(CX);
                   13035:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13036:                        
                   13037:                        if(sector_num == 0xffff) {
                   13038:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13039:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13040:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13041:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13042:                                buffer_addr = (seg << 4) + ofs;
                   13043:                        }
                   13044: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13045: //                             REG8(AL) = 0x02; // drive not ready
                   13046: //                             m_CF = 1;
                   13047: //                     } else 
                   13048:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13049:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13050:                                m_CF = 1;
1.1.1.19  root     13051:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13052:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     13053:                                m_CF = 1;
1.1       root     13054:                        }
                   13055:                        CloseHandle(hFile);
                   13056:                }
                   13057:        }
                   13058: }
                   13059: 
                   13060: inline void msdos_int_26h()
                   13061: {
1.1.1.42  root     13062:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     13063:        UINT16 seg, ofs;
                   13064:        DWORD dwSize;
                   13065:        
1.1.1.3   root     13066: #if defined(HAS_I386)
                   13067:        I386OP(pushf)();
                   13068: #else
                   13069:        PREFIX86(_pushf());
                   13070: #endif
1.1       root     13071:        
                   13072:        if(!(REG8(AL) < 26)) {
                   13073:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13074:                m_CF = 1;
1.1       root     13075:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13076:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13077:                m_CF = 1;
1.1       root     13078:        } else {
                   13079:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13080:                char dev[64];
                   13081:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13082:                
                   13083:                if(dpb->media_type == 0xf8) {
                   13084:                        // this drive is not a floppy
1.1.1.6   root     13085: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   13086: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   13087: //                     }
1.1       root     13088:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13089:                        m_CF = 1;
1.1       root     13090:                } else {
                   13091:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   13092:                        if(hFile == INVALID_HANDLE_VALUE) {
                   13093:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13094:                                m_CF = 1;
1.1       root     13095:                        } else {
1.1.1.19  root     13096:                                UINT32 top_sector  = REG16(DX);
                   13097:                                UINT16 sector_num  = REG16(CX);
                   13098:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13099:                                
                   13100:                                if(sector_num == 0xffff) {
                   13101:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13102:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13103:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13104:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13105:                                        buffer_addr = (seg << 4) + ofs;
                   13106:                                }
1.1       root     13107:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13108:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13109:                                        m_CF = 1;
1.1.1.19  root     13110:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13111:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13112:                                        m_CF = 1;
1.1.1.19  root     13113:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13114:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     13115:                                        m_CF = 1;
1.1       root     13116:                                }
                   13117:                                CloseHandle(hFile);
                   13118:                        }
                   13119:                }
                   13120:        }
                   13121: }
                   13122: 
                   13123: inline void msdos_int_27h()
                   13124: {
1.1.1.29  root     13125:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   13126:        try {
                   13127:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13128:        } catch(...) {
                   13129:                // recover the broken mcb
                   13130:                int mcb_seg = SREG(CS) - 1;
                   13131:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     13132:                
1.1.1.29  root     13133:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     13134:                        mcb->mz = 'M';
                   13135:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   13136:                        
1.1.1.29  root     13137:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     13138:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     13139:                        } else {
1.1.1.39  root     13140:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     13141:                        }
                   13142:                } else {
                   13143:                        mcb->mz = 'Z';
1.1.1.30  root     13144:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     13145:                }
                   13146:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13147:        }
1.1.1.3   root     13148:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     13149: }
                   13150: 
                   13151: inline void msdos_int_29h()
                   13152: {
1.1.1.50! root     13153:        msdos_putch_fast(REG8(AL));
1.1       root     13154: }
                   13155: 
                   13156: inline void msdos_int_2eh()
                   13157: {
                   13158:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   13159:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13160:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     13161:        char *token = my_strtok(tmp, " ");
                   13162:        strcpy(command, token);
                   13163:        strcpy(opt, token + strlen(token) + 1);
                   13164:        
                   13165:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   13166:        param->env_seg = 0;
                   13167:        param->cmd_line.w.l = 44;
                   13168:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13169:        param->fcb1.w.l = 24;
                   13170:        param->fcb1.w.h = (WORK_TOP >> 4);
                   13171:        param->fcb2.w.l = 24;
                   13172:        param->fcb2.w.h = (WORK_TOP >> 4);
                   13173:        
                   13174:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   13175:        
                   13176:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   13177:        cmd_line->len = strlen(opt);
                   13178:        strcpy(cmd_line->cmd, opt);
                   13179:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   13180:        
1.1.1.28  root     13181:        try {
                   13182:                if(msdos_process_exec(command, param, 0)) {
                   13183:                        REG16(AX) = 0xffff; // error before processing command
                   13184:                } else {
                   13185:                        // set flag to set retval to ax when the started process is terminated
                   13186:                        process_t *process = msdos_process_info_get(current_psp);
                   13187:                        process->called_by_int2eh = true;
                   13188:                }
                   13189:        } catch(...) {
                   13190:                REG16(AX) = 0xffff; // error before processing command
                   13191:        }
1.1       root     13192: }
                   13193: 
1.1.1.29  root     13194: inline void msdos_int_2fh_05h()
                   13195: {
                   13196:        switch(REG8(AL)) {
                   13197:        case 0x00:
1.1.1.49  root     13198:                // critical error handler is installed
1.1.1.32  root     13199:                REG8(AL) = 0xff;
                   13200:                break;
                   13201:        case 0x01:
                   13202:        case 0x02:
                   13203:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   13204:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   13205:                                const char *message = NULL;
                   13206:                                if(active_code_page == 932) {
                   13207:                                        message = standard_error_table[i].message_japanese;
                   13208:                                }
                   13209:                                if(message == NULL) {
                   13210:                                        message = standard_error_table[i].message_english;
                   13211:                                }
                   13212:                                strcpy((char *)(mem + WORK_TOP), message);
                   13213:                                
                   13214:                                SREG(ES) = WORK_TOP >> 4;
                   13215:                                i386_load_segment_descriptor(ES);
                   13216:                                REG16(DI) = 0x0000;
                   13217:                                REG8(AL) = 0x01;
                   13218:                                break;
                   13219:                        }
                   13220:                }
1.1.1.29  root     13221:                break;
                   13222:        default:
                   13223:                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     13224:                REG16(AX) = 0x01;
1.1.1.29  root     13225:                m_CF = 1;
                   13226:        }
                   13227: }
                   13228: 
1.1.1.44  root     13229: inline void msdos_int_2fh_06h()
                   13230: {
                   13231:        switch(REG8(AL)) {
                   13232:        case 0x00:
                   13233:                // ASSIGN is not installed
1.1.1.49  root     13234: //             REG8(AL) = 0x00;
1.1.1.44  root     13235:                break;
                   13236:        case 0x01:
                   13237:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   13238:                REG16(AX) = 0x01;
                   13239:                m_CF = 1;
                   13240:                break;
                   13241:        default:
                   13242:                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));
                   13243:                REG16(AX) = 0x01;
                   13244:                m_CF = 1;
                   13245:                break;
                   13246:        }
                   13247: }
                   13248: 
1.1.1.22  root     13249: inline void msdos_int_2fh_11h()
                   13250: {
                   13251:        switch(REG8(AL)) {
                   13252:        case 0x00:
1.1.1.29  root     13253:                if(i386_read_stack() == 0xdada) {
                   13254:                        // MSCDEX is not installed
                   13255: //                     REG8(AL) = 0x00;
                   13256:                } else {
                   13257:                        // Network Redirector is not installed
                   13258: //                     REG8(AL) = 0x00;
                   13259:                }
1.1.1.22  root     13260:                break;
                   13261:        default:
1.1.1.43  root     13262: //             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     13263:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     13264:                m_CF = 1;
                   13265:                break;
                   13266:        }
                   13267: }
                   13268: 
1.1.1.21  root     13269: inline void msdos_int_2fh_12h()
                   13270: {
                   13271:        switch(REG8(AL)) {
1.1.1.22  root     13272:        case 0x00:
1.1.1.29  root     13273:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     13274:                REG8(AL) = 0xff;
                   13275:                break;
1.1.1.29  root     13276: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   13277:        case 0x02:
                   13278:                {
                   13279:                        UINT16 stack = i386_read_stack();
                   13280:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   13281:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   13282:                        i386_load_segment_descriptor(ES);
                   13283:                }
                   13284:                break;
1.1.1.30  root     13285:        case 0x03:
                   13286:                SREG(DS) = (DEVICE_TOP >> 4);
                   13287:                i386_load_segment_descriptor(DS);
                   13288:                break;
1.1.1.29  root     13289:        case 0x04:
                   13290:                {
                   13291:                        UINT16 stack = i386_read_stack();
                   13292:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   13293: #if defined(HAS_I386)
                   13294:                        m_ZF = (REG8(AL) == '\\');
                   13295: #else
                   13296:                        m_ZeroVal = (REG8(AL) != '\\');
                   13297: #endif
                   13298:                }
                   13299:                break;
                   13300:        case 0x05:
1.1.1.49  root     13301:                {
                   13302:                        UINT16 c = i386_read_stack();
                   13303:                        if((c >> 0) & 0xff) {
                   13304:                                msdos_putch((c >> 0) & 0xff);
                   13305:                        }
                   13306:                        if((c >> 8) & 0xff) {
                   13307:                                msdos_putch((c >> 8) & 0xff);
                   13308:                        }
                   13309:                }
1.1.1.29  root     13310:                break;
1.1.1.49  root     13311: //     case 0x06: // DOS 3.0+ internal - Invoke Critical Error
1.1.1.29  root     13312: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   13313: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
1.1.1.49  root     13314: //     case 0x09: // DOS 3.0+ internal - Flush and Free Disk Buffer
1.1.1.29  root     13315: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   13316: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   13317: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   13318:        case 0x0d:
                   13319:                {
                   13320:                        SYSTEMTIME time;
                   13321:                        FILETIME file_time;
                   13322:                        WORD dos_date, dos_time;
                   13323:                        GetLocalTime(&time);
                   13324:                        SystemTimeToFileTime(&time, &file_time);
                   13325:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   13326:                        REG16(AX) = dos_date;
                   13327:                        REG16(DX) = dos_time;
                   13328:                }
                   13329:                break;
                   13330: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   13331: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   13332: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   13333:        case 0x11:
                   13334:                {
                   13335:                        char path[MAX_PATH], *p;
                   13336:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13337:                        my_strupr(path);
                   13338:                        while((p = my_strchr(path, '/')) != NULL) {
                   13339:                                *p = '\\';
                   13340:                        }
                   13341:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   13342:                }
                   13343:                break;
                   13344:        case 0x12:
                   13345:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13346:                break;
                   13347:        case 0x13:
                   13348:                {
                   13349:                        char tmp[2] = {0};
                   13350:                        tmp[0] = i386_read_stack();
                   13351:                        my_strupr(tmp);
                   13352:                        REG8(AL) = tmp[0];
                   13353:                }
                   13354:                break;
                   13355:        case 0x14:
                   13356: #if defined(HAS_I386)
                   13357:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   13358: #else
                   13359:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13360: #endif
                   13361:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13362:                break;
                   13363: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     13364:        case 0x16:
                   13365:                if(REG16(BX) < 20) {
                   13366:                        SREG(ES) = SFT_TOP >> 4;
                   13367:                        i386_load_segment_descriptor(ES);
                   13368:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   13369:                        
                   13370:                        // update system file table
                   13371:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   13372:                        if(file_handler[REG16(BX)].valid) {
                   13373:                                int count = 0;
                   13374:                                for(int i = 0; i < 20; i++) {
                   13375:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   13376:                                                count++;
                   13377:                                        }
                   13378:                                }
                   13379:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   13380:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   13381:                                _lseek(REG16(BX), 0, SEEK_END);
                   13382:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   13383:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   13384:                        } else {
                   13385:                                memset(sft, 0, 0x3b);
                   13386:                        }
                   13387:                } else {
                   13388:                        REG16(AX) = 0x06;
                   13389:                        m_CF = 1;
                   13390:                }
                   13391:                break;
1.1.1.49  root     13392:        case 0x17:
                   13393:                {
                   13394:                        UINT16 drive = i386_read_stack();
                   13395:                        if(msdos_is_valid_drive(drive)) {
                   13396:                                msdos_cds_update(drive);
                   13397:                        }
                   13398:                        REG16(SI) = 88 * drive;
                   13399:                        SREG(DS) = (CDS_TOP >> 4);
                   13400:                        i386_load_segment_descriptor(DS);
                   13401:                }
                   13402:                break;
1.1.1.29  root     13403: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   13404: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   13405:        case 0x1a:
                   13406:                {
                   13407:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   13408:                        if(path[1] == ':') {
                   13409:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   13410:                                        REG8(AL) = path[0] - 'a' + 1;
                   13411:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   13412:                                        REG8(AL) = path[0] - 'A' + 1;
                   13413:                                } else {
                   13414:                                        REG8(AL) = 0xff; // invalid
                   13415:                                }
                   13416:                                strcpy(full, path);
                   13417:                                strcpy(path, full + 2);
                   13418:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   13419:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   13420:                                        REG8(AL) = full[0] - 'a' + 1;
                   13421:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   13422:                                        REG8(AL) = full[0] - 'A' + 1;
                   13423:                                } else {
                   13424:                                        REG8(AL) = 0xff; // invalid
                   13425:                                }
                   13426:                        } else {
                   13427:                                REG8(AL) = 0x00; // default
                   13428:                        }
                   13429:                }
                   13430:                break;
                   13431:        case 0x1b:
                   13432:                {
                   13433:                        int year = REG16(CX) + 1980;
                   13434:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   13435:                }
                   13436:                break;
                   13437: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   13438: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   13439:        case 0x1e:
                   13440:                {
                   13441:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   13442:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   13443:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   13444: #if defined(HAS_I386)
                   13445:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   13446: #else
                   13447:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   13448: #endif
                   13449:                        } else {
                   13450: #if defined(HAS_I386)
                   13451:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   13452: #else
                   13453:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   13454: #endif
                   13455:                        }
                   13456:                }
                   13457:                break;
1.1.1.49  root     13458:        case 0x1f:
                   13459:                {
                   13460:                        UINT16 drive = i386_read_stack();
                   13461:                        if(msdos_is_valid_drive(drive)) {
                   13462:                                msdos_cds_update(drive);
                   13463:                        }
                   13464:                        REG16(SI) = 88 * drive;
                   13465:                        SREG(ES) = (CDS_TOP >> 4);
                   13466:                        i386_load_segment_descriptor(ES);
                   13467:                }
                   13468:                break;
1.1.1.21  root     13469:        case 0x20:
                   13470:                {
                   13471:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13472:                        
                   13473:                        if(fd < 20) {
                   13474:                                SREG(ES) = current_psp;
                   13475:                                i386_load_segment_descriptor(ES);
                   13476:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   13477:                        } else {
                   13478:                                REG16(AX) = 0x06;
                   13479:                                m_CF = 1;
                   13480:                        }
                   13481:                }
                   13482:                break;
1.1.1.29  root     13483:        case 0x21:
                   13484:                msdos_int_21h_60h(0);
                   13485:                break;
1.1.1.49  root     13486:        case 0x22:
                   13487:                {
                   13488:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13489:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00) != 0xff) {
                   13490:                                sda->extended_error_code = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00);
                   13491:                        }
                   13492:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01) != 0xff) {
                   13493:                                sda->error_class         = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01);
                   13494:                        }
                   13495:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02) != 0xff) {
                   13496:                                sda->suggested_action    = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02);
                   13497:                        }
                   13498:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03) != 0xff) {
                   13499:                                sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03);
                   13500:                        }
                   13501:                }
                   13502:                break;
1.1.1.29  root     13503: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   13504: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   13505:        case 0x25:
                   13506:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13507:                break;
                   13508:        case 0x26:
                   13509:                REG8(AL) = REG8(CL);
                   13510:                msdos_int_21h_3dh();
                   13511:                break;
                   13512:        case 0x27:
                   13513:                msdos_int_21h_3eh();
                   13514:                break;
                   13515:        case 0x28:
                   13516:                REG16(AX) = REG16(BP);
                   13517:                msdos_int_21h_42h();
                   13518:                break;
                   13519:        case 0x29:
                   13520:                msdos_int_21h_3fh();
                   13521:                break;
                   13522: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   13523:        case 0x2b:
                   13524:                REG16(AX) = REG16(BP);
                   13525:                msdos_int_21h_44h();
                   13526:                break;
                   13527:        case 0x2c:
                   13528:                REG16(BX) = DEVICE_TOP >> 4;
                   13529:                REG16(AX) = 22;
                   13530:                break;
                   13531:        case 0x2d:
                   13532:                {
                   13533:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13534:                        REG16(AX) = sda->extended_error_code;
                   13535:                }
                   13536:                break;
                   13537:        case 0x2e:
                   13538:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     13539:                        SREG(ES) = 0x0001;
                   13540:                        i386_load_segment_descriptor(ES);
                   13541:                        REG16(DI) = 0x00;
                   13542:                } else if(REG8(DL) == 0x08) {
1.1.1.49  root     13543:                        // dummy parameter error message read routine is at fffc:0010
                   13544:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.22  root     13545:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     13546:                        REG16(DI) = 0x0010;
1.1.1.22  root     13547:                }
                   13548:                break;
1.1.1.29  root     13549:        case 0x2f:
                   13550:                if(REG16(DX) != 0) {
1.1.1.30  root     13551:                        dos_major_version = REG8(DL);
                   13552:                        dos_minor_version = REG8(DH);
1.1.1.29  root     13553:                } else {
                   13554:                        REG8(DL) = 7;
                   13555:                        REG8(DH) = 10;
                   13556:                }
                   13557:                break;
                   13558: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   13559: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     13560:        default:
                   13561:                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));
                   13562:                REG16(AX) = 0x01;
                   13563:                m_CF = 1;
                   13564:                break;
                   13565:        }
                   13566: }
                   13567: 
1.1.1.30  root     13568: inline void msdos_int_2fh_13h()
                   13569: {
                   13570:        static UINT16 prevDS = 0, prevDX = 0;
                   13571:        static UINT16 prevES = 0, prevBX = 0;
                   13572:        UINT16 tmp;
                   13573:        
                   13574:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   13575:        i386_load_segment_descriptor(DS);
                   13576:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   13577:        
                   13578:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   13579:        i386_load_segment_descriptor(ES);
                   13580:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   13581: }
                   13582: 
1.1.1.22  root     13583: inline void msdos_int_2fh_14h()
                   13584: {
                   13585:        switch(REG8(AL)) {
                   13586:        case 0x00:
1.1.1.29  root     13587:                // NLSFUNC.COM is installed
                   13588:                REG8(AL) = 0xff;
1.1.1.25  root     13589:                break;
                   13590:        case 0x01:
                   13591:        case 0x03:
                   13592:                REG8(AL) = 0x00;
                   13593:                active_code_page = REG16(BX);
                   13594:                msdos_nls_tables_update();
                   13595:                break;
                   13596:        case 0x02:
                   13597:                REG8(AL) = get_extended_country_info(REG16(BP));
                   13598:                break;
                   13599:        case 0x04:
1.1.1.42  root     13600:                for(int i = 0;; i++) {
                   13601:                        if(country_table[i].code == REG16(DX)) {
                   13602:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   13603:                                break;
                   13604:                        } else if(country_table[i].code == -1) {
                   13605:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13606:                                break;
                   13607:                        }
                   13608:                }
1.1.1.25  root     13609:                REG8(AL) = 0x00;
1.1.1.22  root     13610:                break;
                   13611:        default:
                   13612:                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));
                   13613:                REG16(AX) = 0x01;
                   13614:                m_CF = 1;
                   13615:                break;
                   13616:        }
                   13617: }
                   13618: 
                   13619: inline void msdos_int_2fh_15h()
                   13620: {
                   13621:        switch(REG8(AL)) {
1.1.1.29  root     13622:        case 0x00: // CD-ROM - Installation Check
                   13623:                if(REG16(BX) == 0x0000) {
1.1.1.43  root     13624: #if 0
                   13625:                        // MSCDEX is installed
                   13626:                        REG16(BX) = 0;
                   13627:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     13628:                                if(msdos_is_cdrom_drive(i)) {
                   13629:                                        if(REG16(BX) == 0) {
                   13630:                                                REG16(CX) = i;
1.1.1.43  root     13631:                                        }
1.1.1.44  root     13632:                                        REG16(BX)++;
1.1.1.43  root     13633:                                }
                   13634:                        }
                   13635: #else
1.1.1.29  root     13636:                        // MSCDEX is not installed
                   13637: //                     REG8(AL) = 0x00;
1.1.1.43  root     13638: #endif
1.1.1.29  root     13639:                } else {
                   13640:                        // GRAPHICS.COM is not installed
                   13641: //                     REG8(AL) = 0x00;
                   13642:                }
1.1.1.22  root     13643:                break;
1.1.1.43  root     13644:        case 0x0b:
1.1.1.44  root     13645:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   13646:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   13647:                REG16(BX) = 0xadad;
1.1.1.43  root     13648:                break;
                   13649:        case 0x0d:
1.1.1.44  root     13650:                for(int i = 0, n = 0; i < 26; i++) {
                   13651:                        if(msdos_is_cdrom_drive(i)) {
                   13652:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     13653:                        }
                   13654:                }
                   13655:                break;
1.1.1.22  root     13656:        case 0xff:
1.1.1.29  root     13657:                if(REG16(BX) == 0x0000) {
                   13658:                        // CORELCDX is not installed
                   13659:                } else {
                   13660:                        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));
                   13661:                        REG16(AX) = 0x01;
                   13662:                        m_CF = 1;
                   13663:                }
1.1.1.22  root     13664:                break;
1.1.1.21  root     13665:        default:
1.1.1.22  root     13666:                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     13667:                REG16(AX) = 0x01;
                   13668:                m_CF = 1;
                   13669:                break;
                   13670:        }
                   13671: }
                   13672: 
1.1       root     13673: inline void msdos_int_2fh_16h()
                   13674: {
                   13675:        switch(REG8(AL)) {
                   13676:        case 0x00:
1.1.1.14  root     13677:                if(no_windows) {
1.1.1.29  root     13678:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   13679: //                     REG8(AL) = 0x00;
1.1.1.14  root     13680:                } else {
1.1.1.30  root     13681:                        REG8(AL) = win_major_version;
                   13682:                        REG8(AH) = win_minor_version;
1.1       root     13683:                }
                   13684:                break;
1.1.1.43  root     13685:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     13686:                // from DOSBox
                   13687:                i386_set_a20_line(1);
                   13688:                break;
1.1.1.49  root     13689:        case 0x06: // Windows Enhanced Mode & 286 DOSX Exit Broadcast
1.1.1.43  root     13690:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   13691:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   13692:                break;
                   13693:        case 0x07:
                   13694:                // Virtual Device Call API
                   13695:                break;
1.1.1.22  root     13696:        case 0x0a:
                   13697:                if(!no_windows) {
                   13698:                        REG16(AX) = 0x0000;
1.1.1.30  root     13699:                        REG8(BH) = win_major_version;
                   13700:                        REG8(BL) = win_minor_version;
1.1.1.49  root     13701: //                     REG16(CX) = 0x0002; // standard
1.1.1.22  root     13702:                        REG16(CX) = 0x0003; // enhanced
                   13703:                }
                   13704:                break;
1.1.1.30  root     13705:        case 0x0b:
                   13706:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     13707:        case 0x0e:
                   13708:        case 0x0f:
1.1.1.30  root     13709:        case 0x10:
1.1.1.22  root     13710:        case 0x11:
                   13711:        case 0x12:
                   13712:        case 0x13:
                   13713:        case 0x14:
1.1.1.30  root     13714:        case 0x15:
1.1.1.43  root     13715:        case 0x81:
                   13716:        case 0x82:
1.1.1.44  root     13717:        case 0x84:
1.1.1.49  root     13718:        case 0x85:
1.1.1.33  root     13719:        case 0x86:
1.1.1.22  root     13720:        case 0x87:
1.1.1.30  root     13721:        case 0x89:
1.1.1.33  root     13722:        case 0x8a:
1.1.1.22  root     13723:                // function not supported, do not clear AX
                   13724:                break;
1.1.1.14  root     13725:        case 0x80:
                   13726:                Sleep(10);
1.1.1.35  root     13727:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     13728:                REG8(AL) = 0x00;
1.1.1.14  root     13729:                break;
1.1.1.33  root     13730:        case 0x83:
                   13731:                REG16(BX) = 0x01; // system vm id
                   13732:                break;
1.1.1.22  root     13733:        case 0x8e:
                   13734:                REG16(AX) = 0x00; // failed
                   13735:                break;
1.1.1.20  root     13736:        case 0x8f:
                   13737:                switch(REG8(DH)) {
                   13738:                case 0x01:
1.1.1.49  root     13739: //                     REG16(AX) = 0x0000; // close command selected but not yet acknowledged
                   13740: //                     REG16(AX) = 0x0001; // close command issued and acknowledged
                   13741:                        REG16(AX) = 0x168f; // close command not selected -- application should continue
                   13742:                        break;
                   13743:                default:
                   13744:                        REG16(AX) = 0x0000; // successful
1.1.1.20  root     13745:                        break;
                   13746:                }
                   13747:                break;
1.1       root     13748:        default:
1.1.1.22  root     13749:                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));
                   13750:                REG16(AX) = 0x01;
                   13751:                m_CF = 1;
                   13752:                break;
                   13753:        }
                   13754: }
                   13755: 
                   13756: inline void msdos_int_2fh_19h()
                   13757: {
                   13758:        switch(REG8(AL)) {
                   13759:        case 0x00:
1.1.1.29  root     13760:                // SHELLB.COM is not installed
                   13761: //             REG8(AL) = 0x00;
1.1.1.22  root     13762:                break;
                   13763:        case 0x01:
                   13764:        case 0x02:
                   13765:        case 0x03:
                   13766:        case 0x04:
                   13767:                REG16(AX) = 0x01;
                   13768:                m_CF = 1;
                   13769:                break;
1.1.1.29  root     13770:        case 0x80:
                   13771:                // IBM ROM-DOS v4.0 is not installed
                   13772: //             REG8(AL) = 0x00;
                   13773:                break;
1.1.1.22  root     13774:        default:
                   13775:                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     13776:                REG16(AX) = 0x01;
1.1.1.3   root     13777:                m_CF = 1;
1.1       root     13778:                break;
                   13779:        }
                   13780: }
                   13781: 
                   13782: inline void msdos_int_2fh_1ah()
                   13783: {
                   13784:        switch(REG8(AL)) {
                   13785:        case 0x00:
1.1.1.29  root     13786:                // ANSI.SYS is installed
1.1       root     13787:                REG8(AL) = 0xff;
                   13788:                break;
1.1.1.49  root     13789:        case 0x01:
1.1.1.50! root     13790:                if(REG8(CL) == 0x5f) {
        !          13791:                        // set display information
        !          13792:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
        !          13793:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
        !          13794:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
        !          13795:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
        !          13796:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
        !          13797:                                
        !          13798:                                if(cur_width != new_width || cur_height != new_height) {
        !          13799:                                        pcbios_set_console_size(new_width, new_height, true);
        !          13800:                                }
        !          13801:                        }
        !          13802:                } else if(REG8(CL) == 0x7f) {
1.1.1.49  root     13803:                        // get display information
1.1.1.50! root     13804:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
        !          13805:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
        !          13806:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
        !          13807:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
        !          13808:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
        !          13809:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
        !          13810:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
        !          13811:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
        !          13812:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
        !          13813:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
        !          13814:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.49  root     13815:                } else {
                   13816:                        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));
                   13817:                        REG16(AX) = 0x01;
                   13818:                        m_CF = 1;
                   13819:                }
                   13820:                break;
1.1       root     13821:        default:
1.1.1.22  root     13822:                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));
                   13823:                REG16(AX) = 0x01;
                   13824:                m_CF = 1;
                   13825:                break;
                   13826:        }
                   13827: }
                   13828: 
1.1.1.30  root     13829: inline void msdos_int_2fh_40h()
1.1.1.22  root     13830: {
                   13831:        switch(REG8(AL)) {
                   13832:        case 0x00:
1.1.1.30  root     13833:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   13834:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     13835:                break;
1.1.1.43  root     13836:        case 0x10:
                   13837:                // OS/2 v2.0+ - Installation Check
                   13838:                REG16(AX) = 0x01;
                   13839:                m_CF = 1;
                   13840:                break;
1.1.1.22  root     13841:        default:
                   13842:                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     13843:                REG16(AX) = 0x01;
1.1.1.3   root     13844:                m_CF = 1;
1.1       root     13845:                break;
                   13846:        }
                   13847: }
                   13848: 
                   13849: inline void msdos_int_2fh_43h()
                   13850: {
                   13851:        switch(REG8(AL)) {
                   13852:        case 0x00:
1.1.1.29  root     13853:                // XMS is installed ?
1.1.1.19  root     13854: #ifdef SUPPORT_XMS
                   13855:                if(support_xms) {
                   13856:                        REG8(AL) = 0x80;
1.1.1.44  root     13857:                }
                   13858: #endif
                   13859:                break;
                   13860:        case 0x08:
                   13861: #ifdef SUPPORT_XMS
                   13862:                if(support_xms) {
                   13863:                        REG8(AL) = 0x43;
                   13864:                        REG8(BL) = 0x01; // IBM PC/AT
                   13865:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   13866:                }
1.1.1.19  root     13867: #endif
                   13868:                break;
                   13869:        case 0x10:
                   13870:                SREG(ES) = XMS_TOP >> 4;
                   13871:                i386_load_segment_descriptor(ES);
1.1.1.26  root     13872:                REG16(BX) = 0x15;
1.1       root     13873:                break;
1.1.1.44  root     13874:        case 0xe0:
                   13875:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   13876:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   13877:                        break;
                   13878:                }
1.1       root     13879:        default:
1.1.1.22  root     13880:                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));
                   13881:                REG16(AX) = 0x01;
                   13882:                m_CF = 1;
                   13883:                break;
                   13884:        }
                   13885: }
                   13886: 
                   13887: inline void msdos_int_2fh_46h()
                   13888: {
                   13889:        switch(REG8(AL)) {
                   13890:        case 0x80:
1.1.1.29  root     13891:                // Windows v3.0 is not installed
                   13892: //             REG8(AL) = 0x00;
1.1.1.22  root     13893:                break;
                   13894:        default:
                   13895:                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));
                   13896:                REG16(AX) = 0x01;
                   13897:                m_CF = 1;
                   13898:                break;
                   13899:        }
                   13900: }
                   13901: 
                   13902: inline void msdos_int_2fh_48h()
                   13903: {
                   13904:        switch(REG8(AL)) {
                   13905:        case 0x00:
1.1.1.29  root     13906:                // DOSKEY is not installed
                   13907: //             REG8(AL) = 0x00;
1.1.1.22  root     13908:                break;
                   13909:        case 0x10:
                   13910:                msdos_int_21h_0ah();
                   13911:                REG16(AX) = 0x00;
                   13912:                break;
                   13913:        default:
                   13914:                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     13915:                REG16(AX) = 0x01;
1.1.1.3   root     13916:                m_CF = 1;
1.1       root     13917:                break;
                   13918:        }
                   13919: }
                   13920: 
                   13921: inline void msdos_int_2fh_4ah()
                   13922: {
                   13923:        switch(REG8(AL)) {
1.1.1.29  root     13924: #ifdef SUPPORT_HMA
                   13925:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   13926:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   13927:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13928:                                // restore first free mcb in high memory area
                   13929:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13930:                        }
                   13931:                        int offset = 0xffff;
                   13932:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   13933:                                REG16(DI) = offset + 0x10;
                   13934:                        } else {
                   13935:                                REG16(DI) = 0xffff;
                   13936:                        }
                   13937:                } else {
                   13938:                        // HMA is already used
                   13939:                        REG16(BX) = 0;
                   13940:                        REG16(DI) = 0xffff;
                   13941:                }
                   13942:                SREG(ES) = 0xffff;
                   13943:                i386_load_segment_descriptor(ES);
                   13944:                break;
                   13945:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   13946:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   13947:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13948:                                // restore first free mcb in high memory area
                   13949:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13950:                        }
                   13951:                        int size = REG16(BX), offset;
                   13952:                        if((size % 16) != 0) {
                   13953:                                size &= ~15;
                   13954:                                size += 16;
                   13955:                        }
                   13956:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   13957:                                REG16(BX) = size;
                   13958:                                REG16(DI) = offset + 0x10;
                   13959:                                is_hma_used_by_int_2fh = true;
                   13960:                        } else {
                   13961:                                REG16(BX) = 0;
                   13962:                                REG16(DI) = 0xffff;
                   13963:                        }
                   13964:                } else {
                   13965:                        // HMA is already used
                   13966:                        REG16(BX) = 0;
                   13967:                        REG16(DI) = 0xffff;
                   13968:                }
                   13969:                SREG(ES) = 0xffff;
                   13970:                i386_load_segment_descriptor(ES);
                   13971:                break;
                   13972:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   13973:                if(REG8(DL) == 0x00) {
                   13974:                        if(!is_hma_used_by_xms) {
                   13975:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13976:                                        // restore first free mcb in high memory area
                   13977:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13978:                                        is_hma_used_by_int_2fh = false;
                   13979:                                }
                   13980:                                int size = REG16(BX), offset;
                   13981:                                if((size % 16) != 0) {
                   13982:                                        size &= ~15;
                   13983:                                        size += 16;
                   13984:                                }
                   13985:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   13986: //                                     REG16(BX) = size;
                   13987:                                        SREG(ES) = 0xffff;
                   13988:                                        i386_load_segment_descriptor(ES);
                   13989:                                        REG16(DI) = offset + 0x10;
                   13990:                                        is_hma_used_by_int_2fh = true;
                   13991:                                } else {
                   13992:                                        REG16(DI) = 0xffff;
                   13993:                                }
                   13994:                        } else {
                   13995:                                REG16(DI) = 0xffff;
                   13996:                        }
                   13997:                } else if(REG8(DL) == 0x01) {
                   13998:                        if(!is_hma_used_by_xms) {
                   13999:                                int size = REG16(BX);
                   14000:                                if((size % 16) != 0) {
                   14001:                                        size &= ~15;
                   14002:                                        size += 16;
                   14003:                                }
                   14004:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   14005:                                        // memory block address is not changed
                   14006:                                } else {
                   14007:                                        REG16(DI) = 0xffff;
                   14008:                                }
                   14009:                        } else {
                   14010:                                REG16(DI) = 0xffff;
                   14011:                        }
                   14012:                } else if(REG8(DL) == 0x02) {
                   14013:                        if(!is_hma_used_by_xms) {
                   14014:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14015:                                        // restore first free mcb in high memory area
                   14016:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14017:                                        is_hma_used_by_int_2fh = false;
                   14018:                                } else {
                   14019:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   14020:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   14021:                                                is_hma_used_by_int_2fh = false;
                   14022:                                        }
                   14023:                                }
                   14024:                        }
                   14025:                } else {
                   14026:                        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));
                   14027:                        REG16(AX) = 0x01;
                   14028:                        m_CF = 1;
                   14029:                }
                   14030:                break;
                   14031:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   14032:                if(!is_hma_used_by_xms) {
                   14033:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14034:                                // restore first free mcb in high memory area
                   14035:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14036:                                is_hma_used_by_int_2fh = false;
                   14037:                        }
                   14038:                        REG16(AX) = 0x0000;
                   14039:                        SREG(ES) = 0xffff;
                   14040:                        i386_load_segment_descriptor(ES);
                   14041:                        REG16(DI) = 0x10;
                   14042:                }
                   14043:                break;
                   14044: #else
1.1       root     14045:        case 0x01:
                   14046:        case 0x02:
1.1.1.29  root     14047:                // HMA is already used
1.1.1.27  root     14048:                REG16(BX) = 0x0000;
1.1.1.3   root     14049:                SREG(ES) = 0xffff;
                   14050:                i386_load_segment_descriptor(ES);
1.1       root     14051:                REG16(DI) = 0xffff;
                   14052:                break;
1.1.1.19  root     14053:        case 0x03:
                   14054:                // unable to allocate
                   14055:                REG16(DI) = 0xffff;
                   14056:                break;
                   14057:        case 0x04:
                   14058:                // function not supported, do not clear AX
                   14059:                break;
1.1.1.29  root     14060: #endif
                   14061:        case 0x10:
1.1.1.42  root     14062:                switch(REG16(BX)) {
                   14063:                case 0x0000:
                   14064:                case 0x0001:
                   14065:                case 0x0002:
                   14066:                case 0x0003:
                   14067:                case 0x0004:
                   14068:                case 0x0005:
                   14069:                case 0x0006:
                   14070:                case 0x0007:
                   14071:                case 0x0008:
                   14072:                case 0x000a:
                   14073:                case 0x1234:
                   14074:                        // SMARTDRV v4.00+ is not installed
                   14075:                        break;
                   14076:                default:
1.1.1.29  root     14077:                        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));
                   14078:                        REG16(AX) = 0x01;
                   14079:                        m_CF = 1;
1.1.1.42  root     14080:                        break;
1.1.1.29  root     14081:                }
                   14082:                break;
                   14083:        case 0x11:
1.1.1.42  root     14084:                switch(REG16(BX)) {
                   14085:                case 0x0000:
                   14086:                case 0x0001:
                   14087:                case 0x0002:
                   14088:                case 0x0003:
                   14089:                case 0x0004:
                   14090:                case 0x0005:
                   14091:                case 0x0006:
                   14092:                case 0x0007:
                   14093:                case 0x0008:
                   14094:                case 0x0009:
                   14095:                case 0x000a:
                   14096:                case 0x000b:
                   14097:                case 0xfffe:
                   14098:                case 0xffff:
1.1.1.29  root     14099:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     14100:                        break;
                   14101:                default:
                   14102:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14103:                        REG16(AX) = 0x01;
                   14104:                        m_CF = 1;
                   14105:                        break;
                   14106:                }
                   14107:                break;
                   14108:        case 0x12:
                   14109:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   14110:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   14111:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   14112:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     14113:                } else {
                   14114:                        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));
                   14115:                        REG16(AX) = 0x01;
                   14116:                        m_CF = 1;
                   14117:                }
1.1.1.22  root     14118:                break;
1.1.1.42  root     14119:        case 0x13:
                   14120:                // DBLSPACE.BIN is not installed
                   14121:                break;
1.1.1.22  root     14122:        default:
                   14123:                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));
                   14124:                REG16(AX) = 0x01;
                   14125:                m_CF = 1;
                   14126:                break;
                   14127:        }
                   14128: }
                   14129: 
                   14130: inline void msdos_int_2fh_4bh()
                   14131: {
                   14132:        switch(REG8(AL)) {
1.1.1.24  root     14133:        case 0x01:
1.1.1.22  root     14134:        case 0x02:
1.1.1.29  root     14135:                // Task Switcher is not installed
1.1.1.24  root     14136:                break;
                   14137:        case 0x03:
                   14138:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   14139:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     14140:                break;
1.1.1.30  root     14141:        case 0x04:
                   14142:                REG16(BX) = 0x0000; // free switcher id successfully
                   14143:                break;
1.1.1.43  root     14144:        case 0x05:
                   14145:                REG16(BX) = 0x0000; // no instance data chain
                   14146:                SREG(ES) = 0x0000;
                   14147:                i386_load_segment_descriptor(ES);
                   14148:                break;
1.1       root     14149:        default:
1.1.1.22  root     14150:                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     14151:                REG16(AX) = 0x01;
1.1.1.3   root     14152:                m_CF = 1;
1.1       root     14153:                break;
                   14154:        }
                   14155: }
                   14156: 
1.1.1.44  root     14157: inline void msdos_int_2fh_4dh()
                   14158: {
                   14159:        switch(REG8(AL)) {
                   14160:        case 0x00:
                   14161:                // KKCFUNC is not installed ???
                   14162:                break;
                   14163:        default:
                   14164: //             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));
                   14165:                REG16(AX) = 0x01; // invalid function
                   14166:                m_CF = 1;
                   14167:                break;
                   14168:        }
                   14169: }
                   14170: 
1.1       root     14171: inline void msdos_int_2fh_4fh()
                   14172: {
                   14173:        switch(REG8(AL)) {
                   14174:        case 0x00:
1.1.1.29  root     14175:                // BILING is installed
1.1.1.27  root     14176:                REG16(AX) = 0x0000;
                   14177:                REG8(DL) = 0x01;        // major version
                   14178:                REG8(DH) = 0x00;        // minor version
1.1       root     14179:                break;
                   14180:        case 0x01:
1.1.1.27  root     14181:                REG16(AX) = 0x0000;
1.1       root     14182:                REG16(BX) = active_code_page;
                   14183:                break;
                   14184:        default:
1.1.1.22  root     14185:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14186:                REG16(AX) = 0x01;
                   14187:                m_CF = 1;
                   14188:                break;
                   14189:        }
                   14190: }
                   14191: 
                   14192: inline void msdos_int_2fh_55h()
                   14193: {
                   14194:        switch(REG8(AL)) {
                   14195:        case 0x00:
                   14196:        case 0x01:
                   14197: //             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));
                   14198:                break;
                   14199:        default:
                   14200:                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     14201:                REG16(AX) = 0x01;
1.1.1.3   root     14202:                m_CF = 1;
1.1       root     14203:                break;
                   14204:        }
                   14205: }
                   14206: 
1.1.1.44  root     14207: inline void msdos_int_2fh_56h()
                   14208: {
                   14209:        switch(REG8(AL)) {
                   14210:        case 0x00:
                   14211:                // INTERLNK is not installed
                   14212:                break;
                   14213:        case 0x01:
                   14214:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   14215: //             if(msdos_is_remote_drive(REG8(BH))) {
                   14216: //                     REG8(AL) = 0x00;
                   14217: //             }
                   14218:                break;
                   14219:        default:
                   14220:                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));
                   14221:                REG16(AX) = 0x01;
                   14222:                m_CF = 1;
                   14223:                break;
                   14224:        }
                   14225: }
                   14226: 
1.1.1.24  root     14227: inline void msdos_int_2fh_adh()
                   14228: {
                   14229:        switch(REG8(AL)) {
                   14230:        case 0x00:
1.1.1.29  root     14231:                // DISPLAY.SYS is installed
1.1.1.24  root     14232:                REG8(AL) = 0xff;
                   14233:                REG16(BX) = 0x100; // ???
                   14234:                break;
                   14235:        case 0x01:
                   14236:                active_code_page = REG16(BX);
                   14237:                msdos_nls_tables_update();
                   14238:                REG16(AX) = 0x01;
                   14239:                break;
                   14240:        case 0x02:
                   14241:                REG16(BX) = active_code_page;
                   14242:                break;
                   14243:        case 0x03:
                   14244:                // FIXME
                   14245:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   14246:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   14247:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   14248:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   14249:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   14250:                break;
                   14251:        case 0x80:
1.1.1.49  root     14252:                // KEYB.COM is not installed
                   14253:                break;
1.1.1.24  root     14254:        default:
                   14255:                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));
                   14256:                REG16(AX) = 0x01;
                   14257:                m_CF = 1;
                   14258:                break;
                   14259:        }
                   14260: }
                   14261: 
1.1       root     14262: inline void msdos_int_2fh_aeh()
                   14263: {
                   14264:        switch(REG8(AL)) {
                   14265:        case 0x00:
1.1.1.28  root     14266:                // FIXME: we need to check the given command line
                   14267:                REG8(AL) = 0x00; // the command should be executed as usual
                   14268: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     14269:                break;
                   14270:        case 0x01:
                   14271:                {
                   14272:                        char command[MAX_PATH];
                   14273:                        memset(command, 0, sizeof(command));
1.1.1.3   root     14274:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     14275:                        
                   14276:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   14277:                        param->env_seg = 0;
                   14278:                        param->cmd_line.w.l = 44;
                   14279:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   14280:                        param->fcb1.w.l = 24;
                   14281:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   14282:                        param->fcb2.w.l = 24;
                   14283:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   14284:                        
                   14285:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   14286:                        
                   14287:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     14288:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   14289:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     14290:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   14291:                        
1.1.1.28  root     14292:                        try {
                   14293:                                msdos_process_exec(command, param, 0);
                   14294:                        } catch(...) {
                   14295:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     14296:                        }
                   14297:                }
                   14298:                break;
                   14299:        default:
1.1.1.22  root     14300:                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     14301:                REG16(AX) = 0x01;
1.1.1.3   root     14302:                m_CF = 1;
1.1       root     14303:                break;
                   14304:        }
                   14305: }
                   14306: 
1.1.1.34  root     14307: inline void msdos_int_2fh_b7h()
                   14308: {
                   14309:        switch(REG8(AL)) {
                   14310:        case 0x00:
                   14311:                // APPEND is not installed
                   14312: //             REG8(AL) = 0x00;
                   14313:                break;
1.1.1.44  root     14314:        case 0x06:
                   14315:                REG16(BX) = 0x0000;
                   14316:                break;
1.1.1.34  root     14317:        case 0x07:
1.1.1.43  root     14318:        case 0x11:
1.1.1.34  root     14319:                // COMMAND.COM calls this service without checking APPEND is installed
                   14320:                break;
                   14321:        default:
                   14322:                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));
                   14323:                REG16(AX) = 0x01;
                   14324:                m_CF = 1;
                   14325:                break;
                   14326:        }
                   14327: }
                   14328: 
1.1.1.24  root     14329: inline void msdos_int_33h_0000h()
                   14330: {
                   14331:        REG16(AX) = 0xffff; // hardware/driver installed
                   14332:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14333: }
                   14334: 
                   14335: inline void msdos_int_33h_0001h()
                   14336: {
1.1.1.34  root     14337:        if(mouse.hidden > 0) {
                   14338:                mouse.hidden--;
                   14339:        }
                   14340:        if(mouse.hidden == 0) {
1.1.1.24  root     14341:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   14342:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   14343:                }
                   14344:                pic[1].imr &= ~0x10;    // enable irq12
                   14345:        }
                   14346: }
                   14347: 
                   14348: inline void msdos_int_33h_0002h()
                   14349: {
1.1.1.34  root     14350:        mouse.hidden++;
                   14351: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
                   14352:        pic[1].imr |= 0x10;     // enable irq12
1.1.1.24  root     14353: }
                   14354: 
                   14355: inline void msdos_int_33h_0003h()
                   14356: {
1.1.1.34  root     14357: //     if(mouse.hidden > 0) {
                   14358:                update_console_input();
                   14359: //     }
1.1.1.24  root     14360:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     14361:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   14362:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   14363: }
                   14364: 
                   14365: inline void msdos_int_33h_0004h()
                   14366: {
                   14367:        mouse.position.x = REG16(CX);
                   14368:        mouse.position.x = REG16(DX);
1.1.1.24  root     14369: }
                   14370: 
                   14371: inline void msdos_int_33h_0005h()
                   14372: {
1.1.1.34  root     14373: //     if(mouse.hidden > 0) {
                   14374:                update_console_input();
                   14375: //     }
1.1.1.24  root     14376:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14377:                int idx = REG16(BX);
1.1.1.34  root     14378:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   14379:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   14380:                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     14381:                mouse.buttons[idx].pressed_times = 0;
                   14382:        } else {
                   14383:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14384:        }
                   14385:        REG16(AX) = mouse.get_buttons();
                   14386: }
                   14387: 
                   14388: inline void msdos_int_33h_0006h()
                   14389: {
1.1.1.34  root     14390: //     if(mouse.hidden > 0) {
                   14391:                update_console_input();
                   14392: //     }
1.1.1.24  root     14393:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14394:                int idx = REG16(BX);
1.1.1.34  root     14395:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   14396:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   14397:                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     14398:                mouse.buttons[idx].released_times = 0;
                   14399:        } else {
                   14400:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14401:        }
                   14402:        REG16(AX) = mouse.get_buttons();
                   14403: }
                   14404: 
                   14405: inline void msdos_int_33h_0007h()
                   14406: {
                   14407:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   14408:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   14409: }
                   14410: 
                   14411: inline void msdos_int_33h_0008h()
                   14412: {
                   14413:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   14414:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   14415: }
                   14416: 
                   14417: inline void msdos_int_33h_0009h()
                   14418: {
                   14419:        mouse.hot_spot[0] = REG16(BX);
                   14420:        mouse.hot_spot[1] = REG16(CX);
                   14421: }
                   14422: 
1.1.1.49  root     14423: inline void msdos_int_33h_000ah()
                   14424: {
                   14425:        mouse.screen_mask = REG16(CX);
                   14426:        mouse.cursor_mask = REG16(DX);
                   14427: }
                   14428: 
1.1.1.24  root     14429: inline void msdos_int_33h_000bh()
                   14430: {
1.1.1.34  root     14431: //     if(mouse.hidden > 0) {
                   14432:                update_console_input();
                   14433: //     }
1.1.1.24  root     14434:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14435:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   14436:        mouse.prev_position.x = mouse.position.x;
                   14437:        mouse.prev_position.y = mouse.position.y;
                   14438:        REG16(CX) = dx;
                   14439:        REG16(DX) = dy;
                   14440: }
                   14441: 
                   14442: inline void msdos_int_33h_000ch()
                   14443: {
                   14444:        mouse.call_mask = REG16(CX);
                   14445:        mouse.call_addr.w.l = REG16(DX);
                   14446:        mouse.call_addr.w.h = SREG(ES);
                   14447: }
                   14448: 
                   14449: inline void msdos_int_33h_000fh()
                   14450: {
                   14451:        mouse.mickey.x = REG16(CX);
                   14452:        mouse.mickey.y = REG16(DX);
                   14453: }
                   14454: 
                   14455: inline void msdos_int_33h_0011h()
                   14456: {
                   14457:        REG16(AX) = 0xffff;
                   14458:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14459: }
                   14460: 
                   14461: inline void msdos_int_33h_0014h()
                   14462: {
                   14463:        UINT16 old_mask = mouse.call_mask;
                   14464:        UINT16 old_ofs = mouse.call_addr.w.l;
                   14465:        UINT16 old_seg = mouse.call_addr.w.h;
                   14466:        
                   14467:        mouse.call_mask = REG16(CX);
                   14468:        mouse.call_addr.w.l = REG16(DX);
                   14469:        mouse.call_addr.w.h = SREG(ES);
                   14470:        
                   14471:        REG16(CX) = old_mask;
                   14472:        REG16(DX) = old_ofs;
                   14473:        SREG(ES) = old_seg;
                   14474:        i386_load_segment_descriptor(ES);
                   14475: }
                   14476: 
                   14477: inline void msdos_int_33h_0015h()
                   14478: {
                   14479:        REG16(BX) = sizeof(mouse);
                   14480: }
                   14481: 
                   14482: inline void msdos_int_33h_0016h()
                   14483: {
                   14484:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   14485: }
                   14486: 
                   14487: inline void msdos_int_33h_0017h()
                   14488: {
                   14489:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   14490: }
                   14491: 
1.1.1.43  root     14492: inline void msdos_int_33h_0018h()
                   14493: {
                   14494:        for(int i = 0; i < 8; i++) {
                   14495:                if(REG16(CX) & (1 << i)) {
                   14496:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   14497:                                // event handler already exists
                   14498:                                REG16(AX) = 0xffff;
                   14499:                                break;
                   14500:                        }
                   14501:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   14502:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   14503:                }
                   14504:        }
                   14505: }
                   14506: 
                   14507: inline void msdos_int_33h_0019h()
                   14508: {
                   14509:        UINT16 call_mask = REG16(CX);
                   14510:        
                   14511:        REG16(CX) = 0;
                   14512:        
                   14513:        for(int i = 0; i < 8; i++) {
                   14514:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   14515:                        for(int j = 0; j < 8; j++) {
                   14516:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   14517:                                        REG16(CX) |= (1 << j);
                   14518:                                }
                   14519:                        }
                   14520:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   14521:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   14522:                        break;
                   14523:                }
                   14524:        }
                   14525: }
                   14526: 
1.1.1.24  root     14527: inline void msdos_int_33h_001ah()
                   14528: {
                   14529:        mouse.sensitivity[0] = REG16(BX);
                   14530:        mouse.sensitivity[1] = REG16(CX);
                   14531:        mouse.sensitivity[2] = REG16(DX);
                   14532: }
                   14533: 
                   14534: inline void msdos_int_33h_001bh()
                   14535: {
                   14536:        REG16(BX) = mouse.sensitivity[0];
                   14537:        REG16(CX) = mouse.sensitivity[1];
                   14538:        REG16(DX) = mouse.sensitivity[2];
                   14539: }
                   14540: 
                   14541: inline void msdos_int_33h_001dh()
                   14542: {
                   14543:        mouse.display_page = REG16(BX);
                   14544: }
                   14545: 
                   14546: inline void msdos_int_33h_001eh()
                   14547: {
                   14548:        REG16(BX) = mouse.display_page;
                   14549: }
                   14550: 
1.1.1.34  root     14551: inline void msdos_int_33h_001fh()
                   14552: {
                   14553:        // from DOSBox
                   14554:        REG16(BX) = 0x0000;
                   14555:        SREG(ES) = 0x0000;
                   14556:        i386_load_segment_descriptor(ES);
                   14557:        mouse.enabled = false;
                   14558:        mouse.old_hidden = mouse.hidden;
                   14559:        mouse.hidden = 1;
                   14560: }
                   14561: 
                   14562: inline void msdos_int_33h_0020h()
                   14563: {
                   14564:        // from DOSBox
                   14565:        mouse.enabled = true;
                   14566:        mouse.hidden = mouse.old_hidden;
                   14567: }
                   14568: 
1.1.1.24  root     14569: inline void msdos_int_33h_0021h()
                   14570: {
                   14571:        REG16(AX) = 0xffff;
                   14572:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14573: }
                   14574: 
                   14575: inline void msdos_int_33h_0022h()
                   14576: {
                   14577:        mouse.language = REG16(BX);
                   14578: }
                   14579: 
                   14580: inline void msdos_int_33h_0023h()
                   14581: {
                   14582:        REG16(BX) = mouse.language;
                   14583: }
                   14584: 
                   14585: inline void msdos_int_33h_0024h()
                   14586: {
                   14587:        REG16(BX) = 0x0805; // V8.05
                   14588:        REG16(CX) = 0x0400; // PS/2
                   14589: }
                   14590: 
1.1.1.49  root     14591: inline void msdos_int_33h_0025h()
                   14592: {
                   14593:        REG16(AX) = 0x8000; // driver (not TSR), software text cursor
                   14594: }
                   14595: 
1.1.1.24  root     14596: inline void msdos_int_33h_0026h()
                   14597: {
                   14598:        REG16(BX) = 0x0000;
                   14599:        REG16(CX) = mouse.max_position.x;
                   14600:        REG16(DX) = mouse.max_position.y;
                   14601: }
                   14602: 
1.1.1.49  root     14603: inline void msdos_int_33h_0027h()
                   14604: {
                   14605: //     if(mouse.hidden > 0) {
                   14606:                update_console_input();
                   14607: //     }
                   14608:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14609:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   14610:        mouse.prev_position.x = mouse.position.x;
                   14611:        mouse.prev_position.y = mouse.position.y;
                   14612:        REG16(AX) = mouse.screen_mask;
                   14613:        REG16(BX) = mouse.cursor_mask;
                   14614:        REG16(CX) = dx;
                   14615:        REG16(DX) = dy;
                   14616: }
                   14617: 
                   14618: inline void msdos_int_33h_0028h()
                   14619: {
                   14620:        if(REG16(CX) != 0) {
                   14621:                UINT8 tmp = REG8(AL);
                   14622:                REG8(AL) = REG8(CL);
                   14623:                pcbios_int_10h_00h();
                   14624:                REG8(AL) = tmp;
                   14625:        }
                   14626:        REG8(CL) = 0x00; // successful
                   14627: }
                   14628: 
                   14629: inline void msdos_int_33h_0029h()
                   14630: {
                   14631:        switch(REG16(CX)) {
                   14632:        case 0x0000:
                   14633:                REG16(CX) = 0x0003;
                   14634:                sprintf((char *)(mem + WORK_TOP), "TEXT Mode (80x25)$");
                   14635:                break;
                   14636:        case 0x0003:
                   14637:                REG16(CX) = 0x0070;
                   14638:                sprintf((char *)(mem + WORK_TOP), "V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   14639:                break;
                   14640:        case 0x0070:
                   14641:                REG16(CX) = 0x0071;
                   14642:                sprintf((char *)(mem + WORK_TOP), "Extended CGA V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   14643:                break;
                   14644:        case 0x0071:
                   14645:                REG16(CX) = 0x0073;
                   14646:                sprintf((char *)(mem + WORK_TOP), "Extended CGA TEXT Mode (80x25)$");
                   14647:                break;
                   14648:        default:
                   14649:                REG16(CX) = 0x0000;
                   14650:                break;
                   14651:        }
                   14652:        if(REG16(CX) != 0) {
                   14653:                SREG(DS) = (WORK_TOP >> 4);
                   14654:        } else {
                   14655:                SREG(DS) = 0x0000;
                   14656:        }
                   14657:        i386_load_segment_descriptor(DS);
                   14658:        REG16(DX) = 0x0000;
                   14659: }
                   14660: 
1.1.1.24  root     14661: inline void msdos_int_33h_002ah()
                   14662: {
1.1.1.34  root     14663:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     14664:        REG16(BX) = mouse.hot_spot[0];
                   14665:        REG16(CX) = mouse.hot_spot[1];
                   14666:        REG16(DX) = 4; // PS/2
                   14667: }
                   14668: 
                   14669: inline void msdos_int_33h_0031h()
                   14670: {
                   14671:        REG16(AX) = mouse.min_position.x;
                   14672:        REG16(BX) = mouse.min_position.y;
                   14673:        REG16(CX) = mouse.max_position.x;
                   14674:        REG16(DX) = mouse.max_position.y;
                   14675: }
                   14676: 
                   14677: inline void msdos_int_33h_0032h()
                   14678: {
                   14679:        REG16(AX) = 0;
1.1.1.49  root     14680:        REG16(AX) |= 0x8000; // 0025h
1.1.1.24  root     14681:        REG16(AX) |= 0x4000; // 0026h
1.1.1.49  root     14682:        REG16(AX) |= 0x2000; // 0027h
1.1.1.24  root     14683: //     REG16(AX) |= 0x1000; // 0028h
                   14684: //     REG16(AX) |= 0x0800; // 0029h
                   14685:        REG16(AX) |= 0x0400; // 002ah
                   14686: //     REG16(AX) |= 0x0200; // 002bh
                   14687: //     REG16(AX) |= 0x0100; // 002ch
                   14688: //     REG16(AX) |= 0x0080; // 002dh
                   14689: //     REG16(AX) |= 0x0040; // 002eh
                   14690:        REG16(AX) |= 0x0020; // 002fh
                   14691: //     REG16(AX) |= 0x0010; // 0030h
                   14692:        REG16(AX) |= 0x0008; // 0031h
                   14693:        REG16(AX) |= 0x0004; // 0032h
                   14694: //     REG16(AX) |= 0x0002; // 0033h
                   14695: //     REG16(AX) |= 0x0001; // 0034h
                   14696: }
                   14697: 
1.1.1.49  root     14698: inline void msdos_int_33h_004dh()
                   14699: {
                   14700:        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), "Copyright 2017 MS-DOS Player");
                   14701: }
                   14702: 
                   14703: inline void msdos_int_33h_006dh()
                   14704: {
                   14705:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x08; // V8.05
                   14706:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = 0x05;
                   14707: }
                   14708: 
1.1.1.19  root     14709: inline void msdos_int_67h_40h()
                   14710: {
                   14711:        if(!support_ems) {
                   14712:                REG8(AH) = 0x84;
                   14713:        } else {
                   14714:                REG8(AH) = 0x00;
                   14715:        }
                   14716: }
                   14717: 
                   14718: inline void msdos_int_67h_41h()
                   14719: {
                   14720:        if(!support_ems) {
                   14721:                REG8(AH) = 0x84;
                   14722:        } else {
                   14723:                REG8(AH) = 0x00;
                   14724:                REG16(BX) = EMS_TOP >> 4;
                   14725:        }
                   14726: }
                   14727: 
                   14728: inline void msdos_int_67h_42h()
                   14729: {
                   14730:        if(!support_ems) {
                   14731:                REG8(AH) = 0x84;
                   14732:        } else {
                   14733:                REG8(AH) = 0x00;
                   14734:                REG16(BX) = free_ems_pages;
                   14735:                REG16(DX) = MAX_EMS_PAGES;
                   14736:        }
                   14737: }
                   14738: 
                   14739: inline void msdos_int_67h_43h()
                   14740: {
                   14741:        if(!support_ems) {
                   14742:                REG8(AH) = 0x84;
                   14743:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   14744:                REG8(AH) = 0x87;
                   14745:        } else if(REG16(BX) > free_ems_pages) {
                   14746:                REG8(AH) = 0x88;
                   14747:        } else if(REG16(BX) == 0) {
                   14748:                REG8(AH) = 0x89;
                   14749:        } else {
1.1.1.31  root     14750:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14751:                        if(!ems_handles[i].allocated) {
                   14752:                                ems_allocate_pages(i, REG16(BX));
                   14753:                                REG8(AH) = 0x00;
                   14754:                                REG16(DX) = i;
                   14755:                                return;
                   14756:                        }
                   14757:                }
                   14758:                REG8(AH) = 0x85;
                   14759:        }
                   14760: }
                   14761: 
                   14762: inline void msdos_int_67h_44h()
                   14763: {
                   14764:        if(!support_ems) {
                   14765:                REG8(AH) = 0x84;
1.1.1.31  root     14766:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14767:                REG8(AH) = 0x83;
                   14768:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   14769:                REG8(AH) = 0x8a;
                   14770: //     } else if(!(REG8(AL) < 4)) {
                   14771: //             REG8(AH) = 0x8b;
                   14772:        } else if(REG16(BX) == 0xffff) {
                   14773:                ems_unmap_page(REG8(AL) & 3);
                   14774:                REG8(AH) = 0x00;
                   14775:        } else {
                   14776:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   14777:                REG8(AH) = 0x00;
                   14778:        }
                   14779: }
                   14780: 
                   14781: inline void msdos_int_67h_45h()
                   14782: {
                   14783:        if(!support_ems) {
                   14784:                REG8(AH) = 0x84;
1.1.1.31  root     14785:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14786:                REG8(AH) = 0x83;
                   14787:        } else {
                   14788:                ems_release_pages(REG16(DX));
                   14789:                REG8(AH) = 0x00;
                   14790:        }
                   14791: }
                   14792: 
                   14793: inline void msdos_int_67h_46h()
                   14794: {
                   14795:        if(!support_ems) {
                   14796:                REG8(AH) = 0x84;
                   14797:        } else {
1.1.1.29  root     14798: //             REG16(AX) = 0x0032; // EMS 3.2
                   14799:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     14800:        }
                   14801: }
                   14802: 
                   14803: inline void msdos_int_67h_47h()
                   14804: {
                   14805:        // NOTE: the map data should be stored in the specified ems page, not process data
                   14806:        process_t *process = msdos_process_info_get(current_psp);
                   14807:        
                   14808:        if(!support_ems) {
                   14809:                REG8(AH) = 0x84;
1.1.1.31  root     14810: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14811: //             REG8(AH) = 0x83;
                   14812:        } else if(process->ems_pages_stored) {
                   14813:                REG8(AH) = 0x8d;
                   14814:        } else {
                   14815:                for(int i = 0; i < 4; i++) {
                   14816:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   14817:                        process->ems_pages[i].page   = ems_pages[i].page;
                   14818:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   14819:                }
                   14820:                process->ems_pages_stored = true;
                   14821:                REG8(AH) = 0x00;
                   14822:        }
                   14823: }
                   14824: 
                   14825: inline void msdos_int_67h_48h()
                   14826: {
                   14827:        // NOTE: the map data should be restored from the specified ems page, not process data
                   14828:        process_t *process = msdos_process_info_get(current_psp);
                   14829:        
                   14830:        if(!support_ems) {
                   14831:                REG8(AH) = 0x84;
1.1.1.31  root     14832: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14833: //             REG8(AH) = 0x83;
                   14834:        } else if(!process->ems_pages_stored) {
                   14835:                REG8(AH) = 0x8e;
                   14836:        } else {
                   14837:                for(int i = 0; i < 4; i++) {
                   14838:                        if(process->ems_pages[i].mapped) {
                   14839:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   14840:                        } else {
                   14841:                                ems_unmap_page(i);
                   14842:                        }
                   14843:                }
                   14844:                process->ems_pages_stored = false;
                   14845:                REG8(AH) = 0x00;
                   14846:        }
                   14847: }
                   14848: 
                   14849: inline void msdos_int_67h_4bh()
                   14850: {
                   14851:        if(!support_ems) {
                   14852:                REG8(AH) = 0x84;
                   14853:        } else {
                   14854:                REG8(AH) = 0x00;
                   14855:                REG16(BX) = 0;
1.1.1.31  root     14856:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14857:                        if(ems_handles[i].allocated) {
                   14858:                                REG16(BX)++;
                   14859:                        }
                   14860:                }
                   14861:        }
                   14862: }
                   14863: 
                   14864: inline void msdos_int_67h_4ch()
                   14865: {
                   14866:        if(!support_ems) {
                   14867:                REG8(AH) = 0x84;
1.1.1.31  root     14868:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14869:                REG8(AH) = 0x83;
                   14870:        } else {
                   14871:                REG8(AH) = 0x00;
                   14872:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   14873:        }
                   14874: }
                   14875: 
                   14876: inline void msdos_int_67h_4dh()
                   14877: {
                   14878:        if(!support_ems) {
                   14879:                REG8(AH) = 0x84;
                   14880:        } else {
                   14881:                REG8(AH) = 0x00;
                   14882:                REG16(BX) = 0;
1.1.1.31  root     14883:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14884:                        if(ems_handles[i].allocated) {
                   14885:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   14886:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   14887:                                REG16(BX)++;
                   14888:                        }
                   14889:                }
                   14890:        }
                   14891: }
                   14892: 
1.1.1.20  root     14893: inline void msdos_int_67h_4eh()
                   14894: {
                   14895:        if(!support_ems) {
                   14896:                REG8(AH) = 0x84;
                   14897:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14898:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   14899:                        // save page map
                   14900:                        for(int i = 0; i < 4; i++) {
                   14901:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   14902:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   14903:                        }
                   14904:                }
                   14905:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14906:                        // restore page map
                   14907:                        for(int i = 0; i < 4; i++) {
                   14908:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   14909:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   14910:                                
1.1.1.31  root     14911:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     14912:                                        ems_map_page(i, handle, page);
                   14913:                                } else {
                   14914:                                        ems_unmap_page(i);
                   14915:                                }
                   14916:                        }
                   14917:                }
                   14918:                REG8(AH) = 0x00;
                   14919:        } else if(REG8(AL) == 0x03) {
                   14920:                REG8(AH) = 0x00;
1.1.1.21  root     14921:                REG8(AL) = 4 * 4;
                   14922:        } else {
1.1.1.22  root     14923:                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     14924:                REG8(AH) = 0x8f;
                   14925:        }
                   14926: }
                   14927: 
                   14928: inline void msdos_int_67h_4fh()
                   14929: {
                   14930:        if(!support_ems) {
                   14931:                REG8(AH) = 0x84;
                   14932:        } else if(REG8(AL) == 0x00) {
                   14933:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   14934:                
                   14935:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   14936:                for(int i = 0; i < count; i++) {
                   14937:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   14938:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   14939:                        
                   14940: //                     if(!(physical < 4)) {
                   14941: //                             REG8(AH) = 0x8b;
                   14942: //                             return;
                   14943: //                     }
                   14944:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     14945:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   14946:                        *(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     14947:                }
                   14948:                REG8(AH) = 0x00;
                   14949:        } else if(REG8(AL) == 0x01) {
                   14950:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   14951:                
                   14952:                for(int i = 0; i < count; i++) {
                   14953:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   14954:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   14955:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   14956:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   14957:                        
                   14958: //                     if(!(physical < 4)) {
                   14959: //                             REG8(AH) = 0x8b;
                   14960: //                             return;
                   14961: //                     } else
1.1.1.41  root     14962:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     14963:                                ems_map_page(physical & 3, handle, logical);
                   14964:                        } else {
1.1.1.41  root     14965:                                ems_unmap_page(physical & 3);
1.1.1.21  root     14966:                        }
                   14967:                }
                   14968:                REG8(AH) = 0x00;
                   14969:        } else if(REG8(AL) == 0x02) {
                   14970:                REG8(AH) = 0x00;
                   14971:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     14972:        } else {
1.1.1.22  root     14973:                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     14974:                REG8(AH) = 0x8f;
                   14975:        }
                   14976: }
                   14977: 
                   14978: inline void msdos_int_67h_50h()
                   14979: {
                   14980:        if(!support_ems) {
                   14981:                REG8(AH) = 0x84;
1.1.1.31  root     14982:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     14983:                REG8(AH) = 0x83;
                   14984:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   14985:                for(int i = 0; i < REG16(CX); i++) {
                   14986:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   14987:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   14988:                        
                   14989:                        if(REG8(AL) == 0x01) {
                   14990:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   14991:                        }
                   14992: //                     if(!(physical < 4)) {
                   14993: //                             REG8(AH) = 0x8b;
                   14994: //                             return;
                   14995: //                     } else
                   14996:                        if(logical == 0xffff) {
                   14997:                                ems_unmap_page(physical & 3);
                   14998:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   14999:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15000:                        } else {
                   15001:                                REG8(AH) = 0x8a;
                   15002:                                return;
                   15003:                        }
                   15004:                }
                   15005:                REG8(AH) = 0x00;
                   15006:        } else {
1.1.1.22  root     15007:                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     15008:                REG8(AH) = 0x8f;
                   15009:        }
                   15010: }
                   15011: 
1.1.1.19  root     15012: inline void msdos_int_67h_51h()
                   15013: {
                   15014:        if(!support_ems) {
                   15015:                REG8(AH) = 0x84;
1.1.1.31  root     15016:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15017:                REG8(AH) = 0x83;
                   15018:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15019:                REG8(AH) = 0x87;
                   15020:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   15021:                REG8(AH) = 0x88;
                   15022:        } else {
                   15023:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   15024:                REG8(AH) = 0x00;
                   15025:        }
                   15026: }
                   15027: 
1.1.1.20  root     15028: inline void msdos_int_67h_52h()
                   15029: {
                   15030:        if(!support_ems) {
                   15031:                REG8(AH) = 0x84;
1.1.1.31  root     15032: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15033: //             REG8(AH) = 0x83;
1.1.1.20  root     15034:        } else if(REG8(AL) == 0x00) {
                   15035:                REG8(AL) = 0x00; // handle is volatile
                   15036:                REG8(AH) = 0x00;
                   15037:        } else if(REG8(AL) == 0x01) {
                   15038:                if(REG8(BL) == 0x00) {
                   15039:                        REG8(AH) = 0x00;
                   15040:                } else {
                   15041:                        REG8(AH) = 0x90; // undefined attribute type
                   15042:                }
                   15043:        } else if(REG8(AL) == 0x02) {
                   15044:                REG8(AL) = 0x00; // only volatile handles supported
                   15045:                REG8(AH) = 0x00;
                   15046:        } else {
1.1.1.22  root     15047:                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     15048:                REG8(AH) = 0x8f;
                   15049:        }
                   15050: }
                   15051: 
1.1.1.19  root     15052: inline void msdos_int_67h_53h()
                   15053: {
                   15054:        if(!support_ems) {
                   15055:                REG8(AH) = 0x84;
1.1.1.31  root     15056:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15057:                REG8(AH) = 0x83;
                   15058:        } else if(REG8(AL) == 0x00) {
                   15059:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   15060:                REG8(AH) = 0x00;
                   15061:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     15062:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15063:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15064:                                REG8(AH) = 0xa1;
                   15065:                                return;
                   15066:                        }
                   15067:                }
                   15068:                REG8(AH) = 0x00;
                   15069:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   15070:        } else {
1.1.1.22  root     15071:                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     15072:                REG8(AH) = 0x8f;
1.1.1.19  root     15073:        }
                   15074: }
                   15075: 
                   15076: inline void msdos_int_67h_54h()
                   15077: {
                   15078:        if(!support_ems) {
                   15079:                REG8(AH) = 0x84;
                   15080:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     15081:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15082:                        if(ems_handles[i].allocated) {
                   15083:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   15084:                        } else {
                   15085:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   15086:                        }
                   15087:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   15088:                }
                   15089:                REG8(AH) = 0x00;
                   15090:                REG8(AL) = MAX_EMS_HANDLES;
                   15091:        } else if(REG8(AL) == 0x01) {
                   15092:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     15093:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15094:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15095:                                REG8(AH) = 0x00;
                   15096:                                REG16(DX) = i;
                   15097:                                break;
                   15098:                        }
                   15099:                }
                   15100:        } else if(REG8(AL) == 0x02) {
                   15101:                REG8(AH) = 0x00;
                   15102:                REG16(BX) = MAX_EMS_HANDLES;
                   15103:        } else {
1.1.1.22  root     15104:                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     15105:                REG8(AH) = 0x8f;
                   15106:        }
                   15107: }
                   15108: 
1.1.1.49  root     15109: inline void msdos_int_67h_55h()
                   15110: {
                   15111:        if(!support_ems) {
                   15112:                REG8(AH) = 0x84;
                   15113:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15114:                REG8(AH) = 0x83;
                   15115:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15116:                UINT16 jump_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
                   15117:                UINT16 jump_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
                   15118:                UINT8  entries  = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
                   15119:                UINT16 map_ofs  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   15120:                UINT16 map_seg  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
                   15121:                
                   15122:                for(int i = 0; i < (int)entries; i++) {
                   15123:                        int logical  = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 0);
                   15124:                        int physical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 2);
                   15125:                        
                   15126:                        if(REG8(AL) == 0x01) {
                   15127:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15128:                        }
                   15129: //                     if(!(physical < 4)) {
                   15130: //                             REG8(AH) = 0x8b;
                   15131: //                             return;
                   15132: //                     } else
                   15133:                        if(logical == 0xffff) {
                   15134:                                ems_unmap_page(physical & 3);
                   15135:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15136:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15137:                        } else {
                   15138:                                REG8(AH) = 0x8a;
                   15139:                                return;
                   15140:                        }
                   15141:                }
                   15142:                i386_jmp_far(jump_seg, jump_ofs);
                   15143:                REG8(AH) = 0x00;
                   15144:        } else {
                   15145:                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));
                   15146:                REG8(AH) = 0x8f;
                   15147:        }
                   15148: }
                   15149: 
                   15150: inline void msdos_int_67h_56h()
                   15151: {
                   15152:        if(!support_ems) {
                   15153:                REG8(AH) = 0x84;
                   15154:        } else if(REG8(AL) == 0x02) {
                   15155:                REG16(BX) = (2 + 2) * 4;
                   15156:                REG8(AH) = 0x00;
                   15157:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15158:                REG8(AH) = 0x83;
                   15159:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15160:                UINT16 call_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  0);
                   15161:                UINT16 call_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  2);
                   15162:                UINT8  new_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  4);
                   15163:                UINT16 new_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  5);
                   15164:                UINT16 new_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  7);
                   15165: #if 0
                   15166:                UINT8  old_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  9);
                   15167:                UINT16 old_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 10);
                   15168:                UINT16 old_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 12);
                   15169: #endif
                   15170:                UINT16 handles[4], pages[4];
                   15171:                
                   15172:                // alter page map and call routine is at fffc:001f
                   15173:                if(!(call_seg == 0 && call_ofs == 0)) {
                   15174:                        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   15175:                        mem[DUMMY_TOP + 0x20] = (call_ofs >> 0) & 0xff;
                   15176:                        mem[DUMMY_TOP + 0x21] = (call_ofs >> 8) & 0xff;
                   15177:                        mem[DUMMY_TOP + 0x22] = (call_seg >> 0) & 0xff;
                   15178:                        mem[DUMMY_TOP + 0x23] = (call_seg >> 8) & 0xff;
                   15179:                } else {
                   15180:                        // invalid call addr :-(
                   15181:                        mem[DUMMY_TOP + 0x1f] = 0x90;   // nop
                   15182:                        mem[DUMMY_TOP + 0x20] = 0x90;   // nop
                   15183:                        mem[DUMMY_TOP + 0x21] = 0x90;   // nop
                   15184:                        mem[DUMMY_TOP + 0x22] = 0x90;   // nop
                   15185:                        mem[DUMMY_TOP + 0x23] = 0x90;   // nop
                   15186:                }
                   15187:                // do call far (push cs/ip) in old mapping
                   15188:                i386_call_far(DUMMY_TOP >> 4, 0x001f);
                   15189:                
                   15190:                // get old mapping data
                   15191: #if 0
                   15192:                for(int i = 0; i < (int)old_entries; i++) {
                   15193:                        int logical  = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 0);
                   15194:                        int physical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 2);
                   15195:                        
                   15196:                        if(REG8(AL) == 0x01) {
                   15197:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15198:                        }
                   15199: //                     if(!(physical < 4)) {
                   15200: //                             REG8(AH) = 0x8b;
                   15201: //                             return;
                   15202: //                     } else
                   15203:                        if(logical == 0xffff) {
                   15204:                                ems_unmap_page(physical & 3);
                   15205:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15206:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15207:                        } else {
                   15208:                                REG8(AH) = 0x8a;
                   15209:                                return;
                   15210:                        }
                   15211:                }
                   15212: #endif
                   15213:                for(int i = 0; i < 4; i++) {
                   15214:                        handles[i] = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15215:                        pages  [i] = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15216:                }
                   15217:                
                   15218:                // set new mapping
                   15219:                for(int i = 0; i < (int)new_entries; i++) {
                   15220:                        int logical  = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 0);
                   15221:                        int physical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 2);
                   15222:                        
                   15223:                        if(REG8(AL) == 0x01) {
                   15224:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15225:                        }
                   15226: //                     if(!(physical < 4)) {
                   15227: //                             REG8(AH) = 0x8b;
                   15228: //                             return;
                   15229: //                     } else
                   15230:                        if(logical == 0xffff) {
                   15231:                                ems_unmap_page(physical & 3);
                   15232:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15233:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15234:                        } else {
                   15235:                                REG8(AH) = 0x8a;
                   15236:                                return;
                   15237:                        }
                   15238:                }
                   15239:                
                   15240:                // push old mapping data in new mapping
                   15241:                for(int i = 0; i < 4; i++) {
                   15242:                        i386_push16(handles[i]);
                   15243:                        i386_push16(pages  [i]);
                   15244:                }
                   15245:                REG8(AH) = 0x00;
                   15246:        } else {
                   15247:                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));
                   15248:                REG8(AH) = 0x8f;
                   15249:        }
                   15250: }
                   15251: 
1.1.1.20  root     15252: inline void msdos_int_67h_57h_tmp()
                   15253: {
                   15254:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15255:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15256:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   15257:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   15258:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   15259:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   15260:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15261:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   15262:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   15263:        
1.1.1.32  root     15264:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     15265:        UINT32 src_addr, dest_addr;
                   15266:        UINT32 src_addr_max, dest_addr_max;
                   15267:        
                   15268:        if(src_type == 0) {
                   15269:                src_buffer = mem;
                   15270:                src_addr = (src_seg << 4) + src_ofs;
                   15271:                src_addr_max = MAX_MEM;
                   15272:        } else {
1.1.1.31  root     15273:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     15274:                        REG8(AH) = 0x83;
                   15275:                        return;
                   15276:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   15277:                        REG8(AH) = 0x8a;
                   15278:                        return;
                   15279:                }
1.1.1.32  root     15280:                if(ems_handles[src_handle].buffer != NULL) {
                   15281:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   15282:                }
1.1.1.20  root     15283:                src_addr = src_ofs;
1.1.1.32  root     15284:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     15285:        }
                   15286:        if(dest_type == 0) {
                   15287:                dest_buffer = mem;
                   15288:                dest_addr = (dest_seg << 4) + dest_ofs;
                   15289:                dest_addr_max = MAX_MEM;
                   15290:        } else {
1.1.1.31  root     15291:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     15292:                        REG8(AH) = 0x83;
                   15293:                        return;
                   15294:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   15295:                        REG8(AH) = 0x8a;
                   15296:                        return;
                   15297:                }
1.1.1.32  root     15298:                if(ems_handles[dest_handle].buffer != NULL) {
                   15299:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   15300:                }
1.1.1.20  root     15301:                dest_addr = dest_ofs;
1.1.1.32  root     15302:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     15303:        }
1.1.1.32  root     15304:        if(src_buffer != NULL && dest_buffer != NULL) {
                   15305:                for(int i = 0; i < copy_length; i++) {
                   15306:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   15307:                                if(REG8(AL) == 0x00) {
                   15308:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   15309:                                } else if(REG8(AL) == 0x01) {
                   15310:                                        UINT8 tmp = dest_buffer[dest_addr];
                   15311:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   15312:                                        src_buffer[src_addr++] = tmp;
                   15313:                                }
                   15314:                        } else {
                   15315:                                REG8(AH) = 0x93;
                   15316:                                return;
1.1.1.20  root     15317:                        }
                   15318:                }
1.1.1.32  root     15319:                REG8(AH) = 0x00;
                   15320:        } else {
                   15321:                REG8(AH) = 0x80;
1.1.1.20  root     15322:        }
                   15323: }
                   15324: 
                   15325: inline void msdos_int_67h_57h()
                   15326: {
                   15327:        if(!support_ems) {
                   15328:                REG8(AH) = 0x84;
                   15329:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15330:                struct {
                   15331:                        UINT16 handle;
                   15332:                        UINT16 page;
                   15333:                        bool mapped;
                   15334:                } tmp_pages[4];
                   15335:                
                   15336:                // unmap pages to copy memory data to ems buffer
                   15337:                for(int i = 0; i < 4; i++) {
                   15338:                        tmp_pages[i].handle = ems_pages[i].handle;
                   15339:                        tmp_pages[i].page   = ems_pages[i].page;
                   15340:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   15341:                        ems_unmap_page(i);
                   15342:                }
                   15343:                
                   15344:                // run move/exchange operation
                   15345:                msdos_int_67h_57h_tmp();
                   15346:                
                   15347:                // restore unmapped pages
                   15348:                for(int i = 0; i < 4; i++) {
                   15349:                        if(tmp_pages[i].mapped) {
                   15350:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   15351:                        }
                   15352:                }
                   15353:        } else {
1.1.1.22  root     15354:                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     15355:                REG8(AH) = 0x8f;
                   15356:        }
                   15357: }
                   15358: 
                   15359: inline void msdos_int_67h_58h()
                   15360: {
                   15361:        if(!support_ems) {
                   15362:                REG8(AH) = 0x84;
                   15363:        } else if(REG8(AL) == 0x00) {
                   15364:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     15365:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   15366:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     15367:                }
                   15368:                REG8(AH) = 0x00;
                   15369:                REG16(CX) = 4;
                   15370:        } else if(REG8(AL) == 0x01) {
                   15371:                REG8(AH) = 0x00;
                   15372:                REG16(CX) = 4;
                   15373:        } else {
1.1.1.22  root     15374:                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     15375:                REG8(AH) = 0x8f;
                   15376:        }
                   15377: }
                   15378: 
1.1.1.42  root     15379: inline void msdos_int_67h_59h()
                   15380: {
                   15381:        if(!support_ems) {
                   15382:                REG8(AH) = 0x84;
                   15383:        } else if(REG8(AL) == 0x00) {
1.1.1.49  root     15384:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1024;
                   15385:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 0;
                   15386:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 4 * 4;
                   15387:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = 0;
                   15388:                REG8(AH) = 0x00;
                   15389: //             REG8(AH) = 0xa4; // access denied by operating system
1.1.1.42  root     15390:        } else if(REG8(AL) == 0x01) {
                   15391:                REG8(AH) = 0x00;
                   15392:                REG16(BX) = free_ems_pages;
                   15393:                REG16(DX) = MAX_EMS_PAGES;
                   15394:        } else {
                   15395:                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));
                   15396:                REG8(AH) = 0x8f;
                   15397:        }
                   15398: }
                   15399: 
1.1.1.20  root     15400: inline void msdos_int_67h_5ah()
                   15401: {
                   15402:        if(!support_ems) {
1.1.1.19  root     15403:                REG8(AH) = 0x84;
1.1.1.20  root     15404:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15405:                REG8(AH) = 0x87;
                   15406:        } else if(REG16(BX) > free_ems_pages) {
                   15407:                REG8(AH) = 0x88;
                   15408: //     } else if(REG16(BX) == 0) {
                   15409: //             REG8(AH) = 0x89;
                   15410:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     15411:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     15412:                        if(!ems_handles[i].allocated) {
                   15413:                                ems_allocate_pages(i, REG16(BX));
                   15414:                                REG8(AH) = 0x00;
                   15415:                                REG16(DX) = i;
                   15416:                                return;
                   15417:                        }
                   15418:                }
                   15419:                REG8(AH) = 0x85;
                   15420:        } else {
1.1.1.22  root     15421:                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     15422:                REG8(AH) = 0x8f;
1.1.1.19  root     15423:        }
                   15424: }
                   15425: 
1.1.1.49  root     15426: inline void msdos_int_67h_5bh()
                   15427: {
                   15428:        static UINT8  stored_bl = 0x00;
                   15429:        static UINT16 stored_es = 0x0000;
                   15430:        static UINT16 stored_di = 0x0000;
                   15431:        
                   15432:        if(!support_ems) {
                   15433:                REG8(AH) = 0x84;
                   15434:        } else if(REG8(AL) == 0x00) {
                   15435:                if(stored_bl == 0x00) {
                   15436:                        if(!(stored_es == 0 && stored_di == 0)) {
                   15437:                                for(int i = 0; i < 4; i++) {
                   15438:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15439:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15440:                                }
                   15441:                        }
                   15442:                        SREG(ES) = stored_es;
                   15443:                        i386_load_segment_descriptor(ES);
                   15444:                        REG16(DI) = stored_di;
                   15445:                } else {
                   15446:                        REG8(BL) = stored_bl;
                   15447:                }
                   15448:                REG8(AH) = 0x00;
                   15449:        } else if(REG8(AL) == 0x01) {
                   15450:                if(REG8(BL) == 0x00) {
                   15451:                        if(!(SREG(ES) == 0 && REG16(DI) == 0)) {
                   15452:                                for(int i = 0; i < 4; i++) {
                   15453:                                        UINT16 handle = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0);
                   15454:                                        UINT16 page   = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2);
                   15455:                                        
                   15456:                                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   15457:                                                ems_map_page(i, handle, page);
                   15458:                                        } else {
                   15459:                                                ems_unmap_page(i);
                   15460:                                        }
                   15461:                                }
                   15462:                        }
                   15463:                }
                   15464:                stored_bl = REG8(BL);
                   15465:                stored_es = SREG(ES);
                   15466:                stored_di = REG16(DI);
                   15467:                REG8(AH) = 0x00;
                   15468:        } else if(REG8(AL) == 0x02) {
                   15469:                REG16(DX) = 4 * 4;
                   15470:                REG8(AH) = 0x00;
                   15471:        } else if(REG8(AL) == 0x03) {
                   15472:                REG8(BL) = 0x00; // not supported
                   15473:                REG8(AH) = 0x00;
                   15474:        } else if(REG8(AL) == 0x04) {
                   15475:                REG8(AH) = 0x00;
                   15476:        } else if(REG8(AL) == 0x05) {
                   15477:                REG8(BL) = 0x00; // not supported
                   15478:                REG8(AH) = 0x00;
                   15479:        } else {
                   15480:                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));
                   15481:                REG8(AH) = 0x8f;
                   15482:        }
                   15483: }
                   15484: 
1.1.1.43  root     15485: inline void msdos_int_67h_5dh()
                   15486: {
                   15487:        if(!support_ems) {
                   15488:                REG8(AH) = 0x84;
                   15489:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15490:                REG8(AH) = 0xa4; // operating system denied access
                   15491:        } else {
                   15492:                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));
                   15493:                REG8(AH) = 0x8f;
                   15494:        }
                   15495: }
                   15496: 
1.1.1.49  root     15497: inline void msdos_int_67h_70h()
                   15498: {
                   15499:        if(!support_ems) {
                   15500:                REG8(AH) = 0x84;
                   15501:        } else if(REG8(AL) == 0x00) {
                   15502:                REG8(AL) = 0x00;
                   15503:                REG8(AH) = 0x00;
                   15504:        } else if(REG8(AL) == 0x01) {
                   15505:                REG8(AL) = 0x00;
                   15506: //             REG8(AH) = (REG8(BL) == 0x00) ? 0x00 : 0x80;
                   15507:                REG8(AH) = 0x00;
                   15508:        } else {
                   15509:                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));
                   15510:                REG8(AH) = 0x8f;
                   15511:        }
                   15512: }
                   15513: 
1.1.1.30  root     15514: inline void msdos_int_67h_deh()
                   15515: {
                   15516:        REG8(AH) = 0x84;
                   15517: }
                   15518: 
1.1.1.19  root     15519: #ifdef SUPPORT_XMS
                   15520: 
1.1.1.32  root     15521: void msdos_xms_init()
1.1.1.26  root     15522: {
1.1.1.30  root     15523:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   15524:        emb_handle_top->address = EMB_TOP;
                   15525:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     15526:        xms_a20_local_enb_count = 0;
                   15527: }
                   15528: 
1.1.1.32  root     15529: void msdos_xms_finish()
                   15530: {
                   15531:        msdos_xms_release();
                   15532: }
                   15533: 
                   15534: void msdos_xms_release()
1.1.1.30  root     15535: {
                   15536:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   15537:                emb_handle_t *next_handle = emb_handle->next;
                   15538:                free(emb_handle);
                   15539:                emb_handle = next_handle;
                   15540:        }
                   15541: }
                   15542: 
                   15543: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   15544: {
                   15545:        if(handle != 0) {
                   15546:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15547:                        if(emb_handle->handle == handle) {
                   15548:                                return(emb_handle);
                   15549:                        }
                   15550:                }
                   15551:        }
                   15552:        return(NULL);
                   15553: }
                   15554: 
                   15555: int msdos_xms_get_unused_emb_handle_id()
                   15556: {
                   15557:        for(int handle = 1;; handle++) {
                   15558:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   15559:                        return(handle);
                   15560:                }
                   15561:        }
                   15562:        return(0);
                   15563: }
                   15564: 
                   15565: int msdos_xms_get_unused_emb_handle_count()
                   15566: {
                   15567:        int count = 64; //255;
                   15568:        
                   15569:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15570:                if(emb_handle->handle != 0) {
                   15571:                        if(--count == 1) {
                   15572:                                break;
                   15573:                        }
                   15574:                }
                   15575:        }
                   15576:        return(count);
                   15577: }
                   15578: 
                   15579: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   15580: {
                   15581:        if(emb_handle->size_kb > size_kb) {
                   15582:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   15583:                
                   15584:                new_handle->address = emb_handle->address + size_kb * 1024;
                   15585:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   15586:                emb_handle->size_kb = size_kb;
                   15587:                
                   15588:                new_handle->prev = emb_handle;
                   15589:                new_handle->next = emb_handle->next;
                   15590:                if(emb_handle->next != NULL) {
                   15591:                        emb_handle->next->prev = new_handle;
                   15592:                }
                   15593:                emb_handle->next = new_handle;
                   15594:        }
                   15595: }
                   15596: 
                   15597: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   15598: {
                   15599:        emb_handle_t *next_handle = emb_handle->next;
                   15600:        
                   15601:        if(next_handle != NULL) {
                   15602:                emb_handle->size_kb += next_handle->size_kb;
                   15603:                
                   15604:                if(next_handle->next != NULL) {
                   15605:                        next_handle->next->prev = emb_handle;
                   15606:                }
                   15607:                emb_handle->next = next_handle->next;
                   15608:                free(next_handle);
                   15609:        }
                   15610: }
                   15611: 
                   15612: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   15613: {
                   15614:        emb_handle_t *target_handle = NULL;
                   15615:        
                   15616:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15617:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   15618:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   15619:                                target_handle = emb_handle;
                   15620:                        }
                   15621:                }
                   15622:        }
                   15623:        if(target_handle != NULL) {
                   15624:                if(target_handle->size_kb > size_kb) {
                   15625:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   15626:                }
                   15627: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   15628:                return(target_handle);
                   15629:        }
                   15630:        return(NULL);
                   15631: }
                   15632: 
                   15633: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   15634: {
                   15635:        emb_handle_t *prev_handle = emb_handle->prev;
                   15636:        emb_handle_t *next_handle = emb_handle->next;
                   15637:        
                   15638:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   15639:                msdos_xms_combine_emb_handles(prev_handle);
                   15640:                emb_handle = prev_handle;
                   15641:        }
                   15642:        if(next_handle != NULL && next_handle->handle == 0) {
                   15643:                msdos_xms_combine_emb_handles(emb_handle);
                   15644:        }
                   15645:        emb_handle->handle = 0;
                   15646: }
                   15647: 
1.1.1.19  root     15648: inline void msdos_call_xms_00h()
                   15649: {
1.1.1.29  root     15650: #if defined(HAS_I386)
                   15651:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.45  root     15652: //     REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
1.1.1.29  root     15653:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   15654: #else
                   15655:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   15656:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   15657: #endif
                   15658: //     REG16(DX) = 0x0000; // HMA does not exist
                   15659:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     15660: }
                   15661: 
                   15662: inline void msdos_call_xms_01h()
                   15663: {
1.1.1.29  root     15664:        if(REG8(AL) == 0x40) {
                   15665:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   15666:                // DX=KB free extended memory returned by last call of function 08h
                   15667:                REG16(AX) = 0x0000;
                   15668:                REG8(BL) = 0x91;
                   15669:                REG16(DX) = xms_dx_after_call_08h;
                   15670:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   15671:                REG16(AX) = 0x0000;
                   15672:                REG8(BL) = 0x81; // Vdisk was detected
                   15673: #ifdef SUPPORT_HMA
                   15674:        } else if(is_hma_used_by_int_2fh) {
                   15675:                REG16(AX) = 0x0000;
                   15676:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   15677:        } else if(is_hma_used_by_xms) {
                   15678:                REG16(AX) = 0x0000;
                   15679:                REG8(BL) = 0x91; // HMA is already in use
                   15680:        } else {
                   15681:                REG16(AX) = 0x0001;
                   15682:                is_hma_used_by_xms = true;
                   15683: #else
                   15684:        } else {
                   15685:                REG16(AX) = 0x0000;
                   15686:                REG8(BL) = 0x91; // HMA is already in use
                   15687: #endif
                   15688:        }
1.1.1.19  root     15689: }
                   15690: 
                   15691: inline void msdos_call_xms_02h()
                   15692: {
1.1.1.29  root     15693:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   15694:                REG16(AX) = 0x0000;
                   15695:                REG8(BL) = 0x81; // Vdisk was detected
                   15696: #ifdef SUPPORT_HMA
                   15697:        } else if(is_hma_used_by_int_2fh) {
                   15698:                REG16(AX) = 0x0000;
                   15699:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   15700:        } else if(!is_hma_used_by_xms) {
                   15701:                REG16(AX) = 0x0000;
                   15702:                REG8(BL) = 0x93; // HMA is not allocated
                   15703:        } else {
                   15704:                REG16(AX) = 0x0001;
                   15705:                is_hma_used_by_xms = false;
                   15706:                // restore first free mcb in high memory area
                   15707:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   15708: #else
                   15709:        } else {
                   15710:                REG16(AX) = 0x0000;
                   15711:                REG8(BL) = 0x91; // HMA is already in use
                   15712: #endif
                   15713:        }
1.1.1.19  root     15714: }
                   15715: 
                   15716: inline void msdos_call_xms_03h()
                   15717: {
                   15718:        i386_set_a20_line(1);
                   15719:        REG16(AX) = 0x0001;
                   15720:        REG8(BL) = 0x00;
                   15721: }
                   15722: 
                   15723: inline void msdos_call_xms_04h()
                   15724: {
1.1.1.21  root     15725:        i386_set_a20_line(0);
                   15726:        REG16(AX) = 0x0001;
                   15727:        REG8(BL) = 0x00;
1.1.1.19  root     15728: }
                   15729: 
                   15730: inline void msdos_call_xms_05h()
                   15731: {
                   15732:        i386_set_a20_line(1);
                   15733:        REG16(AX) = 0x0001;
                   15734:        REG8(BL) = 0x00;
1.1.1.21  root     15735:        xms_a20_local_enb_count++;
1.1.1.19  root     15736: }
                   15737: 
                   15738: void msdos_call_xms_06h()
                   15739: {
1.1.1.21  root     15740:        if(xms_a20_local_enb_count > 0) {
1.1.1.45  root     15741:                if(--xms_a20_local_enb_count == 0) {
                   15742:                        i386_set_a20_line(0);
                   15743:                        REG16(AX) = 0x0001;
                   15744:                        REG8(BL) = 0x00;
                   15745:                } else {
                   15746:                        REG16(AX) = 0x0000;
                   15747:                        REG8(BL) = 0x94;
                   15748:                }
1.1.1.21  root     15749:        } else {
1.1.1.45  root     15750:                i386_set_a20_line(0);
1.1.1.21  root     15751:                REG16(AX) = 0x0001;
                   15752:                REG8(BL) = 0x00;
1.1.1.19  root     15753:        }
                   15754: }
                   15755: 
                   15756: inline void msdos_call_xms_07h()
                   15757: {
                   15758:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   15759:        REG8(BL) = 0x00;
                   15760: }
                   15761: 
                   15762: inline void msdos_call_xms_08h()
                   15763: {
1.1.1.45  root     15764:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     15765:        
1.1.1.30  root     15766:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15767:                if(emb_handle->handle == 0) {
1.1.1.45  root     15768:                        if(eax < emb_handle->size_kb) {
                   15769:                                eax = emb_handle->size_kb;
1.1.1.19  root     15770:                        }
1.1.1.45  root     15771:                        edx += emb_handle->size_kb;
1.1.1.19  root     15772:                }
                   15773:        }
1.1.1.45  root     15774:        if(eax > 65535) {
                   15775:                eax = 65535;
                   15776:        }
                   15777:        if(edx > 65535) {
                   15778:                edx = 65535;
                   15779:        }
                   15780:        if(eax == 0 && edx == 0) {
1.1.1.19  root     15781:                REG8(BL) = 0xa0;
                   15782:        } else {
                   15783:                REG8(BL) = 0x00;
                   15784:        }
1.1.1.45  root     15785: #if defined(HAS_I386)
                   15786:        REG32(EAX) = eax;
                   15787:        REG32(EDX) = edx;
                   15788: #else
                   15789:        REG16(AX) = (UINT16)eax;
                   15790:        REG16(DX) = (UINT16)edx;
                   15791: #endif
1.1.1.29  root     15792:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     15793: }
                   15794: 
1.1.1.30  root     15795: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     15796: {
1.1.1.30  root     15797:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   15798:        
                   15799:        if(emb_handle != NULL) {
                   15800:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   15801:                
                   15802:                REG16(AX) = 0x0001;
                   15803:                REG16(DX) = emb_handle->handle;
                   15804:                REG8(BL) = 0x00;
                   15805:        } else {
                   15806:                REG16(AX) = REG16(DX) = 0x0000;
                   15807:                REG8(BL) = 0xa0;
1.1.1.19  root     15808:        }
1.1.1.30  root     15809: }
                   15810: 
                   15811: inline void msdos_call_xms_09h()
                   15812: {
                   15813:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     15814: }
                   15815: 
                   15816: inline void msdos_call_xms_0ah()
                   15817: {
1.1.1.30  root     15818:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15819:        
                   15820:        if(emb_handle == NULL) {
1.1.1.19  root     15821:                REG16(AX) = 0x0000;
                   15822:                REG8(BL) = 0xa2;
1.1.1.45  root     15823: //     } else if(emb_handle->lock > 0) {
                   15824: //             REG16(AX) = 0x0000;
                   15825: //             REG8(BL) = 0xab;
1.1.1.19  root     15826:        } else {
1.1.1.30  root     15827:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     15828:                
                   15829:                REG16(AX) = 0x0001;
                   15830:                REG8(BL) = 0x00;
                   15831:        }
                   15832: }
                   15833: 
                   15834: inline void msdos_call_xms_0bh()
                   15835: {
                   15836:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15837:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15838:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   15839:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   15840:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15841:        
                   15842:        UINT8 *src_buffer, *dest_buffer;
                   15843:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     15844:        emb_handle_t *emb_handle;
1.1.1.19  root     15845:        
                   15846:        if(src_handle == 0) {
                   15847:                src_buffer = mem;
                   15848:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   15849:                src_addr_max = MAX_MEM;
                   15850:        } else {
1.1.1.30  root     15851:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     15852:                        REG16(AX) = 0x0000;
                   15853:                        REG8(BL) = 0xa3;
                   15854:                        return;
                   15855:                }
1.1.1.30  root     15856:                src_buffer = mem + emb_handle->address;
                   15857:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     15858:        }
                   15859:        if(dest_handle == 0) {
                   15860:                dest_buffer = mem;
                   15861:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   15862:                dest_addr_max = MAX_MEM;
                   15863:        } else {
1.1.1.30  root     15864:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     15865:                        REG16(AX) = 0x0000;
                   15866:                        REG8(BL) = 0xa5;
                   15867:                        return;
                   15868:                }
1.1.1.30  root     15869:                dest_buffer = mem + emb_handle->address;
                   15870:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     15871:        }
                   15872:        for(int i = 0; i < copy_length; i++) {
                   15873:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   15874:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   15875:                } else {
                   15876:                        break;
                   15877:                }
                   15878:        }
                   15879:        REG16(AX) = 0x0001;
                   15880:        REG8(BL) = 0x00;
                   15881: }
                   15882: 
                   15883: inline void msdos_call_xms_0ch()
                   15884: {
1.1.1.30  root     15885:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15886:        
                   15887:        if(emb_handle == NULL) {
1.1.1.19  root     15888:                REG16(AX) = 0x0000;
                   15889:                REG8(BL) = 0xa2;
                   15890:        } else {
1.1.1.45  root     15891:                if(emb_handle->lock < 255) {
                   15892:                        emb_handle->lock++;
                   15893:                }
1.1.1.19  root     15894:                REG16(AX) = 0x0001;
1.1.1.30  root     15895:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   15896:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     15897:        }
                   15898: }
                   15899: 
                   15900: inline void msdos_call_xms_0dh()
                   15901: {
1.1.1.30  root     15902:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15903:        
                   15904:        if(emb_handle == NULL) {
1.1.1.19  root     15905:                REG16(AX) = 0x0000;
                   15906:                REG8(BL) = 0xa2;
1.1.1.30  root     15907:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     15908:                REG16(AX) = 0x0000;
                   15909:                REG8(BL) = 0xaa;
                   15910:        } else {
1.1.1.30  root     15911:                emb_handle->lock--;
1.1.1.19  root     15912:                REG16(AX) = 0x0001;
                   15913:                REG8(BL) = 0x00;
                   15914:        }
                   15915: }
                   15916: 
                   15917: inline void msdos_call_xms_0eh()
                   15918: {
1.1.1.30  root     15919:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15920:        
                   15921:        if(emb_handle == NULL) {
1.1.1.19  root     15922:                REG16(AX) = 0x0000;
                   15923:                REG8(BL) = 0xa2;
                   15924:        } else {
                   15925:                REG16(AX) = 0x0001;
1.1.1.30  root     15926:                REG8(BH) = emb_handle->lock;
                   15927:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   15928:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     15929:        }
                   15930: }
                   15931: 
1.1.1.30  root     15932: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     15933: {
1.1.1.30  root     15934:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15935:        
                   15936:        if(emb_handle == NULL) {
1.1.1.19  root     15937:                REG16(AX) = 0x0000;
                   15938:                REG8(BL) = 0xa2;
1.1.1.30  root     15939:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     15940:                REG16(AX) = 0x0000;
                   15941:                REG8(BL) = 0xab;
                   15942:        } else {
1.1.1.30  root     15943:                if(emb_handle->size_kb < size_kb) {
                   15944:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   15945:                                msdos_xms_combine_emb_handles(emb_handle);
                   15946:                                if(emb_handle->size_kb > size_kb) {
                   15947:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   15948:                                }
                   15949:                        } else {
                   15950:                                int old_handle = emb_handle->handle;
                   15951:                                int old_size_kb = emb_handle->size_kb;
                   15952:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   15953:                                
                   15954:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   15955:                                msdos_xms_free_emb_handle(emb_handle);
                   15956:                                
                   15957:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   15958:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   15959:                                }
                   15960:                                emb_handle->handle = old_handle;
                   15961:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   15962:                                free(buffer);
                   15963:                        }
                   15964:                } else if(emb_handle->size_kb > size_kb) {
                   15965:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   15966:                }
                   15967:                if(emb_handle->size_kb != size_kb) {
                   15968:                        REG16(AX) = 0x0000;
                   15969:                        REG8(BL) = 0xa0;
                   15970:                } else {
                   15971:                        REG16(AX) = 0x0001;
                   15972:                        REG8(BL) = 0x00;
                   15973:                }
1.1.1.19  root     15974:        }
                   15975: }
                   15976: 
1.1.1.30  root     15977: inline void msdos_call_xms_0fh()
                   15978: {
                   15979:        msdos_call_xms_0fh(REG16(BX));
                   15980: }
                   15981: 
1.1.1.19  root     15982: inline void msdos_call_xms_10h()
                   15983: {
                   15984:        int seg;
                   15985:        
                   15986:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   15987:                REG16(AX) = 0x0001;
                   15988:                REG16(BX) = seg;
                   15989:        } else {
                   15990:                REG16(AX) = 0x0000;
                   15991:                REG8(BL) = 0xb0;
                   15992:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   15993:        }
                   15994: }
                   15995: 
                   15996: inline void msdos_call_xms_11h()
                   15997: {
                   15998:        int mcb_seg = REG16(DX) - 1;
                   15999:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16000:        
                   16001:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16002:                msdos_mem_free(REG16(DX));
                   16003:                REG16(AX) = 0x0001;
                   16004:                REG8(BL) = 0x00;
                   16005:        } else {
                   16006:                REG16(AX) = 0x0000;
                   16007:                REG8(BL) = 0xb2;
                   16008:        }
                   16009: }
                   16010: 
                   16011: inline void msdos_call_xms_12h()
                   16012: {
                   16013:        int mcb_seg = REG16(DX) - 1;
                   16014:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16015:        int max_paragraphs;
                   16016:        
                   16017:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16018:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   16019:                        REG16(AX) = 0x0001;
                   16020:                        REG8(BL) = 0x00;
                   16021:                } else {
                   16022:                        REG16(AX) = 0x0000;
                   16023:                        REG8(BL) = 0xb0;
                   16024:                        REG16(DX) = max_paragraphs;
                   16025:                }
                   16026:        } else {
                   16027:                REG16(AX) = 0x0000;
                   16028:                REG8(BL) = 0xb2;
                   16029:        }
                   16030: }
                   16031: 
1.1.1.29  root     16032: #if defined(HAS_I386)
                   16033: 
                   16034: inline void msdos_call_xms_88h()
                   16035: {
                   16036:        REG32(EAX) = REG32(EDX) = 0x0000;
                   16037:        
1.1.1.30  root     16038:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16039:                if(emb_handle->handle == 0) {
                   16040:                        if(REG32(EAX) < emb_handle->size_kb) {
                   16041:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     16042:                        }
1.1.1.30  root     16043:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     16044:                }
                   16045:        }
                   16046:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   16047:                REG8(BL) = 0xa0;
                   16048:        } else {
                   16049:                REG8(BL) = 0x00;
                   16050:        }
                   16051:        REG32(ECX) = EMB_END - 1;
                   16052: }
                   16053: 
                   16054: inline void msdos_call_xms_89h()
                   16055: {
1.1.1.30  root     16056:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     16057: }
                   16058: 
                   16059: inline void msdos_call_xms_8eh()
                   16060: {
1.1.1.30  root     16061:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16062:        
                   16063:        if(emb_handle == NULL) {
1.1.1.29  root     16064:                REG16(AX) = 0x0000;
                   16065:                REG8(BL) = 0xa2;
                   16066:        } else {
                   16067:                REG16(AX) = 0x0001;
1.1.1.30  root     16068:                REG8(BH) = emb_handle->lock;
                   16069:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   16070:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     16071:        }
                   16072: }
                   16073: 
                   16074: inline void msdos_call_xms_8fh()
                   16075: {
1.1.1.30  root     16076:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     16077: }
                   16078: 
                   16079: #endif
1.1.1.19  root     16080: #endif
                   16081: 
1.1.1.26  root     16082: UINT16 msdos_get_equipment()
                   16083: {
                   16084:        static UINT16 equip = 0;
                   16085:        
                   16086:        if(equip == 0) {
                   16087: #ifdef SUPPORT_FPU
                   16088:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   16089: #endif
                   16090:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   16091:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   16092: //             equip |= (1 << 8);      // 0 if DMA installed
                   16093:                equip |= (2 << 9);      // number of serial ports
                   16094:                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     16095:                
                   16096:                // check only A: and B: if it is floppy drive
                   16097:                int n = 0;
                   16098:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     16099:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   16100:                                n++;
1.1.1.28  root     16101:                        }
                   16102:                }
                   16103:                if(n != 0) {
                   16104:                        equip |= (1 << 0);      // floppy disk(s) installed
                   16105:                        n--;
                   16106:                        equip |= (n << 6);      // number of floppies installed less 1
                   16107:                }
                   16108: //             if(joyGetNumDevs() != 0) {
                   16109: //                     equip |= (1 << 12);     // game port installed
                   16110: //             }
1.1.1.26  root     16111:        }
                   16112:        return(equip);
                   16113: }
                   16114: 
1.1       root     16115: void msdos_syscall(unsigned num)
                   16116: {
1.1.1.22  root     16117: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     16118:        if(num == 0x08 || num == 0x1c) {
                   16119:                // don't log the timer interrupts
1.1.1.45  root     16120: //             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     16121:        } else if(num == 0x30) {
        !          16122:                // dummy interrupt for call 0005h (call near)
        !          16123:                fprintf(fp_debug_log, "call 0005h (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.43  root     16124:        } else if(num == 0x68) {
1.1.1.22  root     16125:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     16126:                fprintf(fp_debug_log, "int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.22  root     16127:        } else if(num == 0x69) {
                   16128:                // dummy interrupt for XMS (call far)
1.1.1.33  root     16129:                fprintf(fp_debug_log, "call XMS (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.50! root     16130:        } else if(num >= 0x6a && num <= 0x6f) {
1.1.1.45  root     16131:                // dummy interrupt
1.1.1.22  root     16132:        } else {
1.1.1.33  root     16133:                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     16134:        }
                   16135: #endif
1.1.1.36  root     16136:        // update cursor position
                   16137:        if(cursor_moved) {
                   16138:                pcbios_update_cursor_position();
                   16139:                cursor_moved = false;
                   16140:        }
1.1.1.50! root     16141: #ifdef USE_SERVICE_THREAD
        !          16142:        // this is called from dummy loop to wait until a serive that waits input is done
        !          16143:        if(!in_service)
        !          16144: #endif
1.1.1.33  root     16145:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     16146:        
1.1       root     16147:        switch(num) {
                   16148:        case 0x00:
1.1.1.28  root     16149:                try {
                   16150:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16151:                        error("division by zero\n");
                   16152:                } catch(...) {
                   16153:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   16154:                }
1.1       root     16155:                break;
                   16156:        case 0x04:
1.1.1.28  root     16157:                try {
                   16158:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16159:                        error("overflow\n");
                   16160:                } catch(...) {
                   16161:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   16162:                }
1.1       root     16163:                break;
                   16164:        case 0x06:
                   16165:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     16166:                if(!ignore_illegal_insn) {
1.1.1.28  root     16167:                        try {
                   16168:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16169:                                error("illegal instruction\n");
                   16170:                        } catch(...) {
                   16171:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   16172:                        }
1.1.1.14  root     16173:                } else {
                   16174: #if defined(HAS_I386)
1.1.1.39  root     16175:                        m_eip = m_int6h_skip_eip;
                   16176: #elif defined(HAS_I286)
                   16177:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     16178: #else
1.1.1.39  root     16179:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     16180: #endif
                   16181:                }
1.1       root     16182:                break;
1.1.1.33  root     16183:        case 0x09:
                   16184:                // ctrl-break is pressed
                   16185:                if(raise_int_1bh) {
                   16186: #if defined(HAS_I386)
                   16187:                        m_ext = 0; // not an external interrupt
                   16188:                        i386_trap(0x1b, 1, 0);
                   16189:                        m_ext = 1;
                   16190: #else
                   16191:                        PREFIX86(_interrupt)(0x1b);
                   16192: #endif
                   16193:                        raise_int_1bh = false;
                   16194:                }
1.1.1.8   root     16195:        case 0x08:
1.1.1.14  root     16196: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     16197:        case 0x0b:
                   16198:        case 0x0c:
                   16199:        case 0x0d:
                   16200:        case 0x0e:
                   16201:        case 0x0f:
                   16202:                // EOI
                   16203:                pic[0].isr &= ~(1 << (num - 0x08));
                   16204:                pic_update();
                   16205:                break;
1.1       root     16206:        case 0x10:
                   16207:                // PC BIOS - Video
1.1.1.14  root     16208:                if(!restore_console_on_exit) {
1.1.1.15  root     16209:                        change_console_size(scr_width, scr_height);
1.1       root     16210:                }
1.1.1.3   root     16211:                m_CF = 0;
1.1       root     16212:                switch(REG8(AH)) {
1.1.1.16  root     16213:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     16214:                case 0x01: pcbios_int_10h_01h(); break;
                   16215:                case 0x02: pcbios_int_10h_02h(); break;
                   16216:                case 0x03: pcbios_int_10h_03h(); break;
                   16217:                case 0x05: pcbios_int_10h_05h(); break;
                   16218:                case 0x06: pcbios_int_10h_06h(); break;
                   16219:                case 0x07: pcbios_int_10h_07h(); break;
                   16220:                case 0x08: pcbios_int_10h_08h(); break;
                   16221:                case 0x09: pcbios_int_10h_09h(); break;
                   16222:                case 0x0a: pcbios_int_10h_0ah(); break;
                   16223:                case 0x0b: break;
1.1.1.40  root     16224:                case 0x0c: pcbios_int_10h_0ch(); break;
                   16225:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     16226:                case 0x0e: pcbios_int_10h_0eh(); break;
                   16227:                case 0x0f: pcbios_int_10h_0fh(); break;
                   16228:                case 0x10: break;
1.1.1.14  root     16229:                case 0x11: pcbios_int_10h_11h(); break;
                   16230:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     16231:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     16232:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     16233:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     16234:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   16235:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     16236:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     16237:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   16238:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     16239:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     16240:                case 0x6f: break;
1.1.1.22  root     16241:                case 0x80: m_CF = 1; break; // unknown
                   16242:                case 0x81: m_CF = 1; break; // unknown
1.1       root     16243:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     16244:                case 0x83: pcbios_int_10h_83h(); break;
                   16245:                case 0x8b: break;
                   16246:                case 0x8c: m_CF = 1; break; // unknown
                   16247:                case 0x8d: m_CF = 1; break; // unknown
                   16248:                case 0x8e: m_CF = 1; break; // unknown
                   16249:                case 0x90: pcbios_int_10h_90h(); break;
                   16250:                case 0x91: pcbios_int_10h_91h(); break;
                   16251:                case 0x92: break;
                   16252:                case 0x93: break;
                   16253:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     16254:                case 0xfa: break; // ega register interface library is not installed
1.1       root     16255:                case 0xfe: pcbios_int_10h_feh(); break;
                   16256:                case 0xff: pcbios_int_10h_ffh(); break;
                   16257:                default:
1.1.1.22  root     16258:                        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));
                   16259:                        m_CF = 1;
1.1       root     16260:                        break;
                   16261:                }
                   16262:                break;
                   16263:        case 0x11:
                   16264:                // PC BIOS - Get Equipment List
1.1.1.26  root     16265:                REG16(AX) = msdos_get_equipment();
1.1       root     16266:                break;
                   16267:        case 0x12:
                   16268:                // PC BIOS - Get Memory Size
1.1.1.33  root     16269:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     16270:                break;
                   16271:        case 0x13:
1.1.1.42  root     16272:                // PC BIOS - Disk I/O
                   16273:                {
                   16274:                        static UINT8 last = 0x00;
                   16275:                        switch(REG8(AH)) {
                   16276:                        case 0x00: pcbios_int_13h_00h(); break;
                   16277:                        case 0x01: // get last status
                   16278:                                REG8(AH) = last;
                   16279:                                break;
                   16280:                        case 0x02: pcbios_int_13h_02h(); break;
                   16281:                        case 0x03: pcbios_int_13h_03h(); break;
                   16282:                        case 0x04: pcbios_int_13h_04h(); break;
                   16283:                        case 0x08: pcbios_int_13h_08h(); break;
                   16284:                        case 0x0a: pcbios_int_13h_02h(); break;
                   16285:                        case 0x0b: pcbios_int_13h_03h(); break;
                   16286:                        case 0x0d: pcbios_int_13h_00h(); break;
                   16287:                        case 0x10: pcbios_int_13h_10h(); break;
                   16288:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     16289:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     16290:                        case 0x05: // format
                   16291:                        case 0x06:
                   16292:                        case 0x07:
                   16293:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   16294:                                m_CF = 1;
                   16295:                                break;
                   16296:                        case 0x09:
                   16297:                        case 0x0c: // seek
                   16298:                        case 0x11: // recalib
                   16299:                        case 0x14:
                   16300:                        case 0x17:
                   16301:                                REG8(AH) = 0x00; // successful completion
                   16302:                                break;
1.1.1.43  root     16303:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   16304:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   16305:                                REG8(AH) = 0x01; // invalid function
                   16306:                                m_CF = 1;
                   16307:                                break;
1.1.1.42  root     16308:                        default:
                   16309:                                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));
                   16310:                                REG8(AH) = 0x01; // invalid function
                   16311:                                m_CF = 1;
                   16312:                                break;
                   16313:                        }
                   16314:                        last = REG8(AH);
                   16315:                }
1.1       root     16316:                break;
                   16317:        case 0x14:
                   16318:                // PC BIOS - Serial I/O
1.1.1.25  root     16319:                switch(REG8(AH)) {
                   16320:                case 0x00: pcbios_int_14h_00h(); break;
                   16321:                case 0x01: pcbios_int_14h_01h(); break;
                   16322:                case 0x02: pcbios_int_14h_02h(); break;
                   16323:                case 0x03: pcbios_int_14h_03h(); break;
                   16324:                case 0x04: pcbios_int_14h_04h(); break;
                   16325:                case 0x05: pcbios_int_14h_05h(); break;
                   16326:                default:
                   16327:                        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));
                   16328:                        break;
                   16329:                }
1.1       root     16330:                break;
                   16331:        case 0x15:
                   16332:                // PC BIOS
1.1.1.3   root     16333:                m_CF = 0;
1.1       root     16334:                switch(REG8(AH)) {
1.1.1.14  root     16335:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     16336:                case 0x23: pcbios_int_15h_23h(); break;
                   16337:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     16338:                case 0x41: break;
1.1       root     16339:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     16340:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     16341:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     16342:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     16343:                case 0x86: pcbios_int_15h_86h(); break;
                   16344:                case 0x87: pcbios_int_15h_87h(); break;
                   16345:                case 0x88: pcbios_int_15h_88h(); break;
                   16346:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     16347:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     16348:                case 0xc0: // PS/2 ???
                   16349:                case 0xc1:
                   16350:                case 0xc2:
1.1.1.30  root     16351:                case 0xc3: // PS50+ ???
                   16352:                case 0xc4:
1.1.1.22  root     16353:                        REG8(AH) = 0x86;
                   16354:                        m_CF = 1;
                   16355:                        break;
1.1.1.3   root     16356: #if defined(HAS_I386)
1.1       root     16357:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     16358: #endif
1.1       root     16359:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     16360:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     16361:                default:
1.1.1.22  root     16362:                        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));
                   16363:                        REG8(AH) = 0x86;
1.1.1.3   root     16364:                        m_CF = 1;
1.1       root     16365:                        break;
                   16366:                }
                   16367:                break;
                   16368:        case 0x16:
                   16369:                // PC BIOS - Keyboard
1.1.1.3   root     16370:                m_CF = 0;
1.1       root     16371:                switch(REG8(AH)) {
                   16372:                case 0x00: pcbios_int_16h_00h(); break;
                   16373:                case 0x01: pcbios_int_16h_01h(); break;
                   16374:                case 0x02: pcbios_int_16h_02h(); break;
                   16375:                case 0x03: pcbios_int_16h_03h(); break;
                   16376:                case 0x05: pcbios_int_16h_05h(); break;
                   16377:                case 0x10: pcbios_int_16h_00h(); break;
                   16378:                case 0x11: pcbios_int_16h_01h(); break;
                   16379:                case 0x12: pcbios_int_16h_12h(); break;
                   16380:                case 0x13: pcbios_int_16h_13h(); break;
                   16381:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     16382:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     16383:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     16384:                case 0xda: break; // unknown
1.1.1.43  root     16385:                case 0xdb: break; // unknown
1.1.1.22  root     16386:                case 0xff: break; // unknown
1.1       root     16387:                default:
1.1.1.22  root     16388:                        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     16389:                        break;
                   16390:                }
                   16391:                break;
                   16392:        case 0x17:
                   16393:                // PC BIOS - Printer
1.1.1.37  root     16394:                m_CF = 0;
                   16395:                switch(REG8(AH)) {
                   16396:                case 0x00: pcbios_int_17h_00h(); break;
                   16397:                case 0x01: pcbios_int_17h_01h(); break;
                   16398:                case 0x02: pcbios_int_17h_02h(); break;
                   16399:                case 0x03: pcbios_int_17h_03h(); break;
                   16400:                case 0x50: pcbios_int_17h_50h(); break;
                   16401:                case 0x51: pcbios_int_17h_51h(); break;
                   16402:                case 0x52: pcbios_int_17h_52h(); break;
                   16403:                case 0x84: pcbios_int_17h_84h(); break;
                   16404:                case 0x85: pcbios_int_17h_85h(); break;
                   16405:                default:
                   16406:                        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));
                   16407:                        break;
                   16408:                }
1.1       root     16409:                break;
                   16410:        case 0x1a:
                   16411:                // PC BIOS - Timer
1.1.1.3   root     16412:                m_CF = 0;
1.1       root     16413:                switch(REG8(AH)) {
                   16414:                case 0x00: pcbios_int_1ah_00h(); break;
                   16415:                case 0x01: break;
                   16416:                case 0x02: pcbios_int_1ah_02h(); break;
                   16417:                case 0x03: break;
                   16418:                case 0x04: pcbios_int_1ah_04h(); break;
                   16419:                case 0x05: break;
                   16420:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   16421:                case 0x0b: break;
1.1.1.14  root     16422:                case 0x35: break; // Word Perfect Third Party Interface?
                   16423:                case 0x36: break; // Word Perfect Third Party Interface
                   16424:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     16425:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     16426:                case 0xb1: break; // PCI BIOS v2.0c+
                   16427:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     16428:                default:
1.1.1.22  root     16429:                        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     16430:                        break;
                   16431:                }
                   16432:                break;
1.1.1.33  root     16433:        case 0x1b:
                   16434:                mem[0x471] = 0x00;
                   16435:                break;
1.1       root     16436:        case 0x20:
1.1.1.28  root     16437:                try {
                   16438:                        msdos_process_terminate(SREG(CS), retval, 1);
                   16439:                } catch(...) {
                   16440:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   16441:                }
1.1       root     16442:                break;
1.1.1.49  root     16443:        case 0x30:
1.1.1.46  root     16444:                // dummy interrupt for case map routine pointed in the country info
                   16445: //             if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
                   16446: //                     REG8(AL) = 0x00;
                   16447: //                     break;
                   16448: //             }
1.1       root     16449:        case 0x21:
                   16450:                // MS-DOS System Call
1.1.1.3   root     16451:                m_CF = 0;
1.1.1.28  root     16452:                try {
1.1.1.46  root     16453:                        switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28  root     16454:                        case 0x00: msdos_int_21h_00h(); break;
                   16455:                        case 0x01: msdos_int_21h_01h(); break;
                   16456:                        case 0x02: msdos_int_21h_02h(); break;
                   16457:                        case 0x03: msdos_int_21h_03h(); break;
                   16458:                        case 0x04: msdos_int_21h_04h(); break;
                   16459:                        case 0x05: msdos_int_21h_05h(); break;
                   16460:                        case 0x06: msdos_int_21h_06h(); break;
                   16461:                        case 0x07: msdos_int_21h_07h(); break;
                   16462:                        case 0x08: msdos_int_21h_08h(); break;
                   16463:                        case 0x09: msdos_int_21h_09h(); break;
                   16464:                        case 0x0a: msdos_int_21h_0ah(); break;
                   16465:                        case 0x0b: msdos_int_21h_0bh(); break;
                   16466:                        case 0x0c: msdos_int_21h_0ch(); break;
                   16467:                        case 0x0d: msdos_int_21h_0dh(); break;
                   16468:                        case 0x0e: msdos_int_21h_0eh(); break;
                   16469:                        case 0x0f: msdos_int_21h_0fh(); break;
                   16470:                        case 0x10: msdos_int_21h_10h(); break;
                   16471:                        case 0x11: msdos_int_21h_11h(); break;
                   16472:                        case 0x12: msdos_int_21h_12h(); break;
                   16473:                        case 0x13: msdos_int_21h_13h(); break;
                   16474:                        case 0x14: msdos_int_21h_14h(); break;
                   16475:                        case 0x15: msdos_int_21h_15h(); break;
                   16476:                        case 0x16: msdos_int_21h_16h(); break;
                   16477:                        case 0x17: msdos_int_21h_17h(); break;
                   16478:                        case 0x18: msdos_int_21h_18h(); break;
                   16479:                        case 0x19: msdos_int_21h_19h(); break;
                   16480:                        case 0x1a: msdos_int_21h_1ah(); break;
                   16481:                        case 0x1b: msdos_int_21h_1bh(); break;
                   16482:                        case 0x1c: msdos_int_21h_1ch(); break;
                   16483:                        case 0x1d: msdos_int_21h_1dh(); break;
                   16484:                        case 0x1e: msdos_int_21h_1eh(); break;
                   16485:                        case 0x1f: msdos_int_21h_1fh(); break;
                   16486:                        case 0x20: msdos_int_21h_20h(); break;
                   16487:                        case 0x21: msdos_int_21h_21h(); break;
                   16488:                        case 0x22: msdos_int_21h_22h(); break;
                   16489:                        case 0x23: msdos_int_21h_23h(); break;
                   16490:                        case 0x24: msdos_int_21h_24h(); break;
                   16491:                        case 0x25: msdos_int_21h_25h(); break;
                   16492:                        case 0x26: msdos_int_21h_26h(); break;
                   16493:                        case 0x27: msdos_int_21h_27h(); break;
                   16494:                        case 0x28: msdos_int_21h_28h(); break;
                   16495:                        case 0x29: msdos_int_21h_29h(); break;
                   16496:                        case 0x2a: msdos_int_21h_2ah(); break;
                   16497:                        case 0x2b: msdos_int_21h_2bh(); break;
                   16498:                        case 0x2c: msdos_int_21h_2ch(); break;
                   16499:                        case 0x2d: msdos_int_21h_2dh(); break;
                   16500:                        case 0x2e: msdos_int_21h_2eh(); break;
                   16501:                        case 0x2f: msdos_int_21h_2fh(); break;
                   16502:                        case 0x30: msdos_int_21h_30h(); break;
                   16503:                        case 0x31: msdos_int_21h_31h(); break;
                   16504:                        case 0x32: msdos_int_21h_32h(); break;
                   16505:                        case 0x33: msdos_int_21h_33h(); break;
                   16506:                        case 0x34: msdos_int_21h_34h(); break;
                   16507:                        case 0x35: msdos_int_21h_35h(); break;
                   16508:                        case 0x36: msdos_int_21h_36h(); break;
                   16509:                        case 0x37: msdos_int_21h_37h(); break;
                   16510:                        case 0x38: msdos_int_21h_38h(); break;
                   16511:                        case 0x39: msdos_int_21h_39h(0); break;
                   16512:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   16513:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   16514:                        case 0x3c: msdos_int_21h_3ch(); break;
                   16515:                        case 0x3d: msdos_int_21h_3dh(); break;
                   16516:                        case 0x3e: msdos_int_21h_3eh(); break;
                   16517:                        case 0x3f: msdos_int_21h_3fh(); break;
                   16518:                        case 0x40: msdos_int_21h_40h(); break;
                   16519:                        case 0x41: msdos_int_21h_41h(0); break;
                   16520:                        case 0x42: msdos_int_21h_42h(); break;
                   16521:                        case 0x43: msdos_int_21h_43h(0); break;
                   16522:                        case 0x44: msdos_int_21h_44h(); break;
                   16523:                        case 0x45: msdos_int_21h_45h(); break;
                   16524:                        case 0x46: msdos_int_21h_46h(); break;
                   16525:                        case 0x47: msdos_int_21h_47h(0); break;
                   16526:                        case 0x48: msdos_int_21h_48h(); break;
                   16527:                        case 0x49: msdos_int_21h_49h(); break;
                   16528:                        case 0x4a: msdos_int_21h_4ah(); break;
                   16529:                        case 0x4b: msdos_int_21h_4bh(); break;
                   16530:                        case 0x4c: msdos_int_21h_4ch(); break;
                   16531:                        case 0x4d: msdos_int_21h_4dh(); break;
                   16532:                        case 0x4e: msdos_int_21h_4eh(); break;
                   16533:                        case 0x4f: msdos_int_21h_4fh(); break;
                   16534:                        case 0x50: msdos_int_21h_50h(); break;
                   16535:                        case 0x51: msdos_int_21h_51h(); break;
                   16536:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     16537:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     16538:                        case 0x54: msdos_int_21h_54h(); break;
                   16539:                        case 0x55: msdos_int_21h_55h(); break;
                   16540:                        case 0x56: msdos_int_21h_56h(0); break;
                   16541:                        case 0x57: msdos_int_21h_57h(); break;
                   16542:                        case 0x58: msdos_int_21h_58h(); break;
                   16543:                        case 0x59: msdos_int_21h_59h(); break;
                   16544:                        case 0x5a: msdos_int_21h_5ah(); break;
                   16545:                        case 0x5b: msdos_int_21h_5bh(); break;
                   16546:                        case 0x5c: msdos_int_21h_5ch(); break;
                   16547:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     16548:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     16549:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     16550:                        case 0x60: msdos_int_21h_60h(0); break;
                   16551:                        case 0x61: msdos_int_21h_61h(); break;
                   16552:                        case 0x62: msdos_int_21h_62h(); break;
                   16553:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     16554:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     16555:                        case 0x65: msdos_int_21h_65h(); break;
                   16556:                        case 0x66: msdos_int_21h_66h(); break;
                   16557:                        case 0x67: msdos_int_21h_67h(); break;
                   16558:                        case 0x68: msdos_int_21h_68h(); break;
                   16559:                        case 0x69: msdos_int_21h_69h(); break;
                   16560:                        case 0x6a: msdos_int_21h_6ah(); break;
                   16561:                        case 0x6b: msdos_int_21h_6bh(); break;
                   16562:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.48  root     16563:                        case 0x6d: // Find First ROM Program
                   16564:                        case 0x6e: // Find Next ROM Program
                   16565:                        case 0x6f: // Get/Set ROM Scan Start Address
                   16566:                                REG8(AL) = 0x00; // if not supported (DOS <5, MS-DOS 5+ non-ROM versions)
                   16567:                                break;
1.1.1.43  root     16568:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.48  root     16569:                        case 0x71: // Windows95 - Long Filename Functions
1.1.1.28  root     16570:                                switch(REG8(AL)) {
                   16571:                                case 0x0d: msdos_int_21h_710dh(); break;
                   16572:                                case 0x39: msdos_int_21h_39h(1); break;
                   16573:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   16574:                                case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.48  root     16575:                                case 0x41: msdos_int_21h_7141h(); break;
1.1.1.28  root     16576:                                case 0x43: msdos_int_21h_43h(1); break;
                   16577:                                case 0x47: msdos_int_21h_47h(1); break;
                   16578:                                case 0x4e: msdos_int_21h_714eh(); break;
                   16579:                                case 0x4f: msdos_int_21h_714fh(); break;
                   16580:                                case 0x56: msdos_int_21h_56h(1); break;
                   16581:                                case 0x60: msdos_int_21h_60h(1); break;
                   16582:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   16583:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   16584:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   16585:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   16586:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   16587:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45  root     16588:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     16589:                                case 0xaa: msdos_int_21h_71aah(); break;
                   16590:                                default:
                   16591:                                        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));
                   16592:                                        REG16(AX) = 0x7100;
                   16593:                                        m_CF = 1;
                   16594:                                        break;
                   16595:                                }
                   16596:                                break;
1.1.1.48  root     16597:                        case 0x72: // Windows95 beta - LFN FindClose
                   16598: //                             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));
                   16599:                                REG16(AX) = 0x7200;
                   16600:                                m_CF = 1;
                   16601:                                break;
                   16602:                        case 0x73: // Windows95 - FAT32 Functions
1.1.1.28  root     16603:                                switch(REG8(AL)) {
                   16604:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     16605:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     16606:                                case 0x02: msdos_int_21h_7302h(); break;
                   16607:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     16608:                                // 0x04: Set DPB to Use for Formatting
                   16609:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     16610:                                default:
                   16611:                                        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));
                   16612:                                        REG16(AX) = 0x7300;
                   16613:                                        m_CF = 1;
                   16614:                                        break;
                   16615:                                }
1.1       root     16616:                                break;
1.1.1.30  root     16617:                        case 0xdb: msdos_int_21h_dbh(); break;
                   16618:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     16619:                        default:
1.1.1.22  root     16620:                                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     16621:                                REG16(AX) = 0x01;
1.1.1.3   root     16622:                                m_CF = 1;
1.1       root     16623:                                break;
                   16624:                        }
1.1.1.28  root     16625:                } catch(int error) {
                   16626:                        REG16(AX) = error;
                   16627:                        m_CF = 1;
                   16628:                } catch(...) {
                   16629:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     16630:                        m_CF = 1;
1.1       root     16631:                }
1.1.1.3   root     16632:                if(m_CF) {
1.1.1.23  root     16633:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     16634:                        sda->int21h_5d0ah_called = 0;
1.1.1.23  root     16635:                        sda->extended_error_code = REG16(AX);
                   16636:                        switch(sda->extended_error_code) {
                   16637:                        case  4: // Too many open files
                   16638:                        case  8: // Insufficient memory
                   16639:                                sda->error_class = 1; // Out of resource
                   16640:                                break;
                   16641:                        case  5: // Access denied
                   16642:                                sda->error_class = 3; // Authorization
                   16643:                                break;
                   16644:                        case  7: // Memory control block destroyed
                   16645:                                sda->error_class = 4; // Internal
                   16646:                                break;
                   16647:                        case  2: // File not found
                   16648:                        case  3: // Path not found
                   16649:                        case 15: // Invaid drive specified
                   16650:                        case 18: // No more files
                   16651:                                sda->error_class = 8; // Not found
                   16652:                                break;
                   16653:                        case 32: // Sharing violation
                   16654:                        case 33: // Lock violation
                   16655:                                sda->error_class = 10; // Locked
                   16656:                                break;
                   16657: //                     case 16: // Removal of current directory attempted
                   16658:                        case 19: // Attempted write on protected disk
                   16659:                        case 21: // Drive not ready
                   16660: //                     case 29: // Write failure
                   16661: //                     case 30: // Read failure
                   16662: //                     case 82: // Cannot create subdirectory
                   16663:                                sda->error_class = 11; // Media
                   16664:                                break;
                   16665:                        case 80: // File already exists
                   16666:                                sda->error_class = 12; // Already exist
                   16667:                                break;
                   16668:                        default:
                   16669:                                sda->error_class = 13; // Unknown
                   16670:                                break;
                   16671:                        }
                   16672:                        sda->suggested_action = 1; // Retry
                   16673:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     16674:                }
1.1.1.33  root     16675:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     16676:                        // raise int 23h
                   16677: #if defined(HAS_I386)
                   16678:                        m_ext = 0; // not an external interrupt
                   16679:                        i386_trap(0x23, 1, 0);
                   16680:                        m_ext = 1;
                   16681: #else
                   16682:                        PREFIX86(_interrupt)(0x23);
                   16683: #endif
                   16684:                }
1.1       root     16685:                break;
                   16686:        case 0x22:
                   16687:                fatalerror("int 22h (terminate address)\n");
                   16688:        case 0x23:
1.1.1.28  root     16689:                try {
                   16690:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   16691:                } catch(...) {
                   16692:                        fatalerror("failed to terminate the current process by int 23h\n");
                   16693:                }
1.1       root     16694:                break;
                   16695:        case 0x24:
1.1.1.32  root     16696: /*
1.1.1.28  root     16697:                try {
                   16698:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16699:                } catch(...) {
                   16700:                        fatalerror("failed to terminate the current process by int 24h\n");
                   16701:                }
1.1.1.32  root     16702: */
                   16703:                msdos_int_24h();
1.1       root     16704:                break;
                   16705:        case 0x25:
                   16706:                msdos_int_25h();
                   16707:                break;
                   16708:        case 0x26:
                   16709:                msdos_int_26h();
                   16710:                break;
                   16711:        case 0x27:
1.1.1.28  root     16712:                try {
                   16713:                        msdos_int_27h();
                   16714:                } catch(...) {
                   16715:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   16716:                }
1.1       root     16717:                break;
                   16718:        case 0x28:
                   16719:                Sleep(10);
1.1.1.35  root     16720:                REQUEST_HARDWRE_UPDATE();
1.1       root     16721:                break;
                   16722:        case 0x29:
                   16723:                msdos_int_29h();
                   16724:                break;
                   16725:        case 0x2e:
                   16726:                msdos_int_2eh();
                   16727:                break;
                   16728:        case 0x2f:
                   16729:                // multiplex interrupt
                   16730:                switch(REG8(AH)) {
1.1.1.22  root     16731:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     16732:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     16733:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     16734:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     16735:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     16736:                case 0x14: msdos_int_2fh_14h(); break;
                   16737:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     16738:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     16739:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     16740:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     16741:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     16742:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     16743:                case 0x46: msdos_int_2fh_46h(); break;
                   16744:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     16745:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     16746:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     16747:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     16748:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     16749:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     16750:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     16751:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     16752:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     16753:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     16754:                default:
1.1.1.30  root     16755:                        switch(REG8(AL)) {
                   16756:                        case 0x00:
                   16757:                                // This is not installed
                   16758: //                             REG8(AL) = 0x00;
                   16759:                                break;
1.1.1.33  root     16760:                        case 0x01:
1.1.1.42  root     16761:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   16762:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   16763:                                        break;
                   16764:                                }
1.1.1.33  root     16765:                                // Banyan VINES v4+ is not installed
                   16766:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   16767:                                        break;
                   16768:                                }
1.1.1.42  root     16769:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   16770:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   16771:                                        break;
                   16772:                                }
1.1.1.30  root     16773:                        default:
1.1.1.42  root     16774:                                // NORTON UTILITIES 5.0+
                   16775:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   16776:                                        break;
                   16777:                                }
1.1.1.30  root     16778:                                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     16779:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     16780:                                m_CF = 1;
                   16781:                                break;
                   16782:                        }
                   16783:                        break;
1.1       root     16784:                }
                   16785:                break;
1.1.1.24  root     16786:        case 0x33:
                   16787:                switch(REG8(AH)) {
                   16788:                case 0x00:
                   16789:                        // Mouse
1.1.1.49  root     16790:                        switch(REG16(AX)) {
                   16791:                        case 0x0000: msdos_int_33h_0000h(); break;
                   16792:                        case 0x0001: msdos_int_33h_0001h(); break;
                   16793:                        case 0x0002: msdos_int_33h_0002h(); break;
                   16794:                        case 0x0003: msdos_int_33h_0003h(); break;
                   16795:                        case 0x0004: msdos_int_33h_0004h(); break;
                   16796:                        case 0x0005: msdos_int_33h_0005h(); break;
                   16797:                        case 0x0006: msdos_int_33h_0006h(); break;
                   16798:                        case 0x0007: msdos_int_33h_0007h(); break;
                   16799:                        case 0x0008: msdos_int_33h_0008h(); break;
                   16800:                        case 0x0009: msdos_int_33h_0009h(); break;
                   16801:                        case 0x000a: msdos_int_33h_000ah(); break;
                   16802:                        case 0x000b: msdos_int_33h_000bh(); break;
                   16803:                        case 0x000c: msdos_int_33h_000ch(); break;
                   16804:                        case 0x000d: break; // MS MOUSE v1.0+ - Light Pen Emulation On
                   16805:                        case 0x000e: break; // MS MOUSE v1.0+ - Light Pen Emulation Off
                   16806:                        case 0x000f: msdos_int_33h_000fh(); break;
                   16807:                        case 0x0010: break; // MS MOUSE v1.0+ - Define Screen Region for Updating
                   16808:                        case 0x0011: msdos_int_33h_0011h(); break;
                   16809:                        case 0x0012: REG16(AX) = 0xffff; break; // MS MOUSE - Set Large Graphics Cursor Block
                   16810:                        case 0x0013: break; // MS MOUSE v5.0+ - Define Double-Speed Threshold
                   16811:                        case 0x0014: msdos_int_33h_0014h(); break;
                   16812:                        case 0x0015: msdos_int_33h_0015h(); break;
                   16813:                        case 0x0016: msdos_int_33h_0016h(); break;
                   16814:                        case 0x0017: msdos_int_33h_0017h(); break;
                   16815:                        case 0x0018: msdos_int_33h_0018h(); break;
                   16816:                        case 0x0019: msdos_int_33h_0019h(); break;
                   16817:                        case 0x001a: msdos_int_33h_001ah(); break;
                   16818:                        case 0x001b: msdos_int_33h_001bh(); break;
                   16819:                        case 0x001c: break; // MS MOUSE v6.0+ - Set Interrupt Rate
                   16820:                        case 0x001d: msdos_int_33h_001dh(); break;
                   16821:                        case 0x001e: msdos_int_33h_001eh(); break;
                   16822:                        case 0x001f: msdos_int_33h_001fh(); break;
                   16823:                        case 0x0020: msdos_int_33h_0020h(); break;
                   16824:                        case 0x0021: msdos_int_33h_0021h(); break;
                   16825:                        case 0x0022: msdos_int_33h_0022h(); break;
                   16826:                        case 0x0023: msdos_int_33h_0023h(); break;
                   16827:                        case 0x0024: msdos_int_33h_0024h(); break;
                   16828:                        case 0x0025: msdos_int_33h_0025h(); break;
                   16829:                        case 0x0026: msdos_int_33h_0026h(); break;
                   16830:                        case 0x0027: msdos_int_33h_0027h(); break;
                   16831:                        case 0x0028: msdos_int_33h_0028h(); break;
                   16832:                        case 0x0029: msdos_int_33h_0029h(); break;
                   16833:                        case 0x002a: msdos_int_33h_002ah(); break;
                   16834:                        // 0x002b: MS MOUSE v7.0+ - Load Acceleration Profiles
                   16835:                        // 0x002c: MS MOUSE v7.0+ - Get Acceleration Profiles
                   16836:                        // 0x002d: MS MOUSE v7.0+ - Select Acceleration Profile
                   16837:                        // 0x002e: MS MOUSE v8.10+ - Set Acceleration Profile Names
                   16838:                        case 0x002f: break; // Mouse Hardware Reset
                   16839:                        // 0x0030: MS MOUSE v7.04+ - Get/Set BallPoint Information
                   16840:                        case 0x0031: msdos_int_33h_0031h(); break;
                   16841:                        case 0x0032: msdos_int_33h_0032h(); break;
                   16842:                        // 0x0033: MS MOUSE v7.05+ - Get Switch Settings And Acceleration Profile Data
                   16843:                        // 0x0034: MS MOUSE v8.0+ - Get Initialization File
                   16844:                        // 0x0035: MS MOUSE v8.10+ - LCD Screen Large Pointer Support
                   16845:                        case 0x004d: msdos_int_33h_004dh(); break;
                   16846:                        case 0x006d: msdos_int_33h_006dh(); break;
1.1.1.24  root     16847:                        default:
                   16848:                                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));
                   16849:                                break;
                   16850:                        }
                   16851:                        break;
                   16852:                default:
                   16853:                        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));
                   16854:                        break;
                   16855:                }
                   16856:                break;
1.1.1.50! root     16857: /*
        !          16858:        case 0x67:
        !          16859:                // int 67h handler is in EMS device driver (EMMXXXX0) and it calls int 68h
        !          16860:                // NOTE: some softwares get address of int 67h handler and recognize the address is in EMS device driver
        !          16861:                break;
        !          16862: */
1.1.1.19  root     16863:        case 0x68:
                   16864:                // dummy interrupt for EMS (int 67h)
                   16865:                switch(REG8(AH)) {
                   16866:                case 0x40: msdos_int_67h_40h(); break;
                   16867:                case 0x41: msdos_int_67h_41h(); break;
                   16868:                case 0x42: msdos_int_67h_42h(); break;
                   16869:                case 0x43: msdos_int_67h_43h(); break;
                   16870:                case 0x44: msdos_int_67h_44h(); break;
                   16871:                case 0x45: msdos_int_67h_45h(); break;
                   16872:                case 0x46: msdos_int_67h_46h(); break;
                   16873:                case 0x47: msdos_int_67h_47h(); break;
                   16874:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     16875:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   16876:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     16877:                case 0x4b: msdos_int_67h_4bh(); break;
                   16878:                case 0x4c: msdos_int_67h_4ch(); break;
                   16879:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     16880:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     16881:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     16882:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     16883:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     16884:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     16885:                case 0x53: msdos_int_67h_53h(); break;
                   16886:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.49  root     16887:                case 0x55: msdos_int_67h_55h(); break;
                   16888:                case 0x56: msdos_int_67h_56h(); break;
1.1.1.20  root     16889:                case 0x57: msdos_int_67h_57h(); break;
                   16890:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     16891:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     16892:                case 0x5a: msdos_int_67h_5ah(); break;
1.1.1.49  root     16893:                case 0x5b: msdos_int_67h_5bh(); break;
1.1.1.43  root     16894:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   16895:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.49  root     16896:                case 0x70: msdos_int_67h_70h(); break;
1.1.1.31  root     16897:                // 0xde: VCPI
1.1.1.30  root     16898:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     16899:                default:
1.1.1.22  root     16900:                        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     16901:                        REG8(AH) = 0x84;
                   16902:                        break;
                   16903:                }
                   16904:                break;
                   16905: #ifdef SUPPORT_XMS
                   16906:        case 0x69:
                   16907:                // dummy interrupt for XMS (call far)
1.1.1.28  root     16908:                try {
                   16909:                        switch(REG8(AH)) {
                   16910:                        case 0x00: msdos_call_xms_00h(); break;
                   16911:                        case 0x01: msdos_call_xms_01h(); break;
                   16912:                        case 0x02: msdos_call_xms_02h(); break;
                   16913:                        case 0x03: msdos_call_xms_03h(); break;
                   16914:                        case 0x04: msdos_call_xms_04h(); break;
                   16915:                        case 0x05: msdos_call_xms_05h(); break;
                   16916:                        case 0x06: msdos_call_xms_06h(); break;
                   16917:                        case 0x07: msdos_call_xms_07h(); break;
                   16918:                        case 0x08: msdos_call_xms_08h(); break;
                   16919:                        case 0x09: msdos_call_xms_09h(); break;
                   16920:                        case 0x0a: msdos_call_xms_0ah(); break;
                   16921:                        case 0x0b: msdos_call_xms_0bh(); break;
                   16922:                        case 0x0c: msdos_call_xms_0ch(); break;
                   16923:                        case 0x0d: msdos_call_xms_0dh(); break;
                   16924:                        case 0x0e: msdos_call_xms_0eh(); break;
                   16925:                        case 0x0f: msdos_call_xms_0fh(); break;
                   16926:                        case 0x10: msdos_call_xms_10h(); break;
                   16927:                        case 0x11: msdos_call_xms_11h(); break;
                   16928:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     16929: #if defined(HAS_I386)
                   16930:                        case 0x88: msdos_call_xms_88h(); break;
                   16931:                        case 0x89: msdos_call_xms_89h(); break;
                   16932:                        case 0x8e: msdos_call_xms_8eh(); break;
                   16933:                        case 0x8f: msdos_call_xms_8fh(); break;
                   16934: #endif
1.1.1.28  root     16935:                        default:
                   16936:                                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));
                   16937:                                REG16(AX) = 0x0000;
                   16938:                                REG8(BL) = 0x80; // function not implemented
                   16939:                                break;
                   16940:                        }
                   16941:                } catch(...) {
1.1.1.19  root     16942:                        REG16(AX) = 0x0000;
1.1.1.28  root     16943:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     16944:                }
                   16945:                break;
                   16946: #endif
                   16947:        case 0x6a:
1.1.1.24  root     16948:                // irq12 (mouse)
                   16949:                mouse_push_ax = REG16(AX);
                   16950:                mouse_push_bx = REG16(BX);
                   16951:                mouse_push_cx = REG16(CX);
                   16952:                mouse_push_dx = REG16(DX);
                   16953:                mouse_push_si = REG16(SI);
                   16954:                mouse_push_di = REG16(DI);
                   16955:                
1.1.1.43  root     16956:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     16957:                        REG16(AX) = mouse.status_irq;
                   16958:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     16959:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   16960:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     16961:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   16962:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   16963:                        
1.1.1.49  root     16964:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   16965:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr.w.l & 0xff;
                   16966:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr.w.l >> 8;
                   16967:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr.w.h & 0xff;
                   16968:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.43  root     16969:                        break;
1.1.1.24  root     16970:                }
1.1.1.43  root     16971:                for(int i = 0; i < 8; i++) {
                   16972:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   16973:                                REG16(AX) = mouse.status_irq_alt;
                   16974:                                REG16(BX) = mouse.get_buttons();
                   16975:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   16976:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   16977:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   16978:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   16979:                                
1.1.1.49  root     16980:                                mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   16981:                                mem[DUMMY_TOP + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   16982:                                mem[DUMMY_TOP + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   16983:                                mem[DUMMY_TOP + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   16984:                                mem[DUMMY_TOP + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
1.1.1.43  root     16985:                                break;
                   16986:                        }
                   16987:                }
                   16988:                // invalid call addr :-(
1.1.1.49  root     16989:                mem[DUMMY_TOP + 0x02] = 0x90;   // nop
                   16990:                mem[DUMMY_TOP + 0x03] = 0x90;   // nop
                   16991:                mem[DUMMY_TOP + 0x04] = 0x90;   // nop
                   16992:                mem[DUMMY_TOP + 0x05] = 0x90;   // nop
                   16993:                mem[DUMMY_TOP + 0x06] = 0x90;   // nop
1.1.1.24  root     16994:                break;
                   16995:        case 0x6b:
                   16996:                // end of irq12 (mouse)
                   16997:                REG16(AX) = mouse_push_ax;
                   16998:                REG16(BX) = mouse_push_bx;
                   16999:                REG16(CX) = mouse_push_cx;
                   17000:                REG16(DX) = mouse_push_dx;
                   17001:                REG16(SI) = mouse_push_si;
                   17002:                REG16(DI) = mouse_push_di;
                   17003:                
                   17004:                // EOI
                   17005:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   17006:                        pic[0].isr &= ~(1 << 2); // master
                   17007:                }
                   17008:                pic_update();
                   17009:                break;
                   17010:        case 0x6c:
1.1.1.19  root     17011:                // dummy interrupt for case map routine pointed in the country info
                   17012:                if(REG8(AL) >= 0x80) {
                   17013:                        char tmp[2] = {0};
                   17014:                        tmp[0] = REG8(AL);
                   17015:                        my_strupr(tmp);
                   17016:                        REG8(AL) = tmp[0];
                   17017:                }
                   17018:                break;
1.1.1.27  root     17019:        case 0x6d:
                   17020:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   17021:                REG8(AL) = 0x86; // not supported
                   17022:                m_CF = 1;
                   17023:                break;
1.1.1.32  root     17024:        case 0x6e:
                   17025:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   17026:                {
                   17027:                        UINT16 code = REG16(AX);
                   17028:                        if(code & 0xf0) {
                   17029:                                code = (code & 7) | ((code & 0x10) >> 1);
                   17030:                        }
                   17031:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   17032:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   17033:                                        const char *message = NULL;
                   17034:                                        if(active_code_page == 932) {
                   17035:                                                message = param_error_table[i].message_japanese;
                   17036:                                        }
                   17037:                                        if(message == NULL) {
                   17038:                                                message = param_error_table[i].message_english;
                   17039:                                        }
                   17040:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   17041:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   17042:                                        
                   17043:                                        SREG(ES) = WORK_TOP >> 4;
                   17044:                                        i386_load_segment_descriptor(ES);
                   17045:                                        REG16(DI) = 0x0000;
                   17046:                                        break;
                   17047:                                }
                   17048:                        }
                   17049:                }
                   17050:                break;
1.1.1.49  root     17051:        case 0x6f:
                   17052:                // dummy interrupt for end of alter page map and call
                   17053:                {
                   17054:                        UINT16 handles[4], pages[4];
                   17055:                        
                   17056:                        // pop old mapping data in new mapping
                   17057:                        for(int i = 0; i < 4; i++) {
                   17058:                                pages  [3 - i] = i386_pop16();
                   17059:                                handles[3 - i] = i386_pop16();
                   17060:                        }
                   17061:                        
                   17062:                        // restore old mapping
                   17063:                        for(int i = 0; i < 4; i++) {
                   17064:                                UINT16 handle = handles[i];
                   17065:                                UINT16 page   = pages  [i];
                   17066:                                
                   17067:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   17068:                                        ems_map_page(i, handle, page);
                   17069:                                } else {
                   17070:                                        ems_unmap_page(i);
                   17071:                                }
                   17072:                        }
                   17073:                        // do ret_far (pop cs/ip) in old mapping
                   17074:                }
                   17075:                break;
1.1.1.8   root     17076:        case 0x70:
                   17077:        case 0x71:
                   17078:        case 0x72:
                   17079:        case 0x73:
                   17080:        case 0x74:
                   17081:        case 0x75:
                   17082:        case 0x76:
                   17083:        case 0x77:
                   17084:                // EOI
                   17085:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   17086:                        pic[0].isr &= ~(1 << 2); // master
                   17087:                }
                   17088:                pic_update();
                   17089:                break;
1.1       root     17090:        default:
1.1.1.22  root     17091: //             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     17092:                break;
                   17093:        }
                   17094:        
                   17095:        // update cursor position
                   17096:        if(cursor_moved) {
1.1.1.36  root     17097:                pcbios_update_cursor_position();
1.1       root     17098:                cursor_moved = false;
                   17099:        }
                   17100: }
                   17101: 
                   17102: // init
                   17103: 
                   17104: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   17105: {
                   17106:        // init file handler
                   17107:        memset(file_handler, 0, sizeof(file_handler));
                   17108:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   17109:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   17110:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     17111: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     17112:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     17113: #else
                   17114:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   17115: #endif
                   17116:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     17117:        }
1.1.1.21  root     17118:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45  root     17119: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   17120:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     17121:        }
1.1       root     17122:        _dup2(0, DUP_STDIN);
                   17123:        _dup2(1, DUP_STDOUT);
                   17124:        _dup2(2, DUP_STDERR);
1.1.1.21  root     17125:        _dup2(3, DUP_STDAUX);
                   17126:        _dup2(4, DUP_STDPRN);
1.1       root     17127:        
1.1.1.24  root     17128:        // init mouse
                   17129:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     17130:        mouse.enabled = true;   // from DOSBox
                   17131:        mouse.hidden = 1;       // hidden in default ???
                   17132:        mouse.old_hidden = 1;   // from DOSBox
                   17133:        mouse.max_position.x = 8 * (scr_width  - 1);
                   17134:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     17135:        mouse.mickey.x = 8;
                   17136:        mouse.mickey.y = 16;
                   17137:        
1.1.1.26  root     17138: #ifdef SUPPORT_XMS
                   17139:        // init xms
                   17140:        msdos_xms_init();
                   17141: #endif
                   17142:        
1.1       root     17143:        // init process
                   17144:        memset(process, 0, sizeof(process));
                   17145:        
1.1.1.13  root     17146:        // init dtainfo
                   17147:        msdos_dta_info_init();
                   17148:        
1.1       root     17149:        // init memory
                   17150:        memset(mem, 0, sizeof(mem));
                   17151:        
                   17152:        // bios data area
1.1.1.23  root     17153:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     17154:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   17155:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     17156:        CONSOLE_FONT_INFO cfi;
                   17157:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   17158:        
                   17159:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   17160:        text_vram_top_address = TEXT_VRAM_TOP;
                   17161:        text_vram_end_address = text_vram_top_address + regen;
                   17162:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   17163:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   17164:        
                   17165:        if(regen > 0x4000) {
                   17166:                regen = 0x8000;
                   17167:                vram_pages = 1;
                   17168:        } else if(regen > 0x2000) {
                   17169:                regen = 0x4000;
                   17170:                vram_pages = 2;
                   17171:        } else if(regen > 0x1000) {
                   17172:                regen = 0x2000;
                   17173:                vram_pages = 4;
                   17174:        } else {
                   17175:                regen = 0x1000;
                   17176:                vram_pages = 8;
                   17177:        }
1.1       root     17178:        
1.1.1.25  root     17179:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   17180:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     17181:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   17182:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     17183:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     17184:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   17185:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     17186: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17187:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     17188: #endif
1.1.1.26  root     17189:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     17190:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     17191:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   17192:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     17193:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     17194:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   17195:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     17196:        *(UINT16 *)(mem + 0x44e) = 0;
                   17197:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     17198:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     17199:        *(UINT8  *)(mem + 0x460) = 7;
                   17200:        *(UINT8  *)(mem + 0x461) = 7;
                   17201:        *(UINT8  *)(mem + 0x462) = 0;
                   17202:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     17203:        *(UINT8  *)(mem + 0x465) = 0x09;
                   17204:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     17205:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     17206:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   17207:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     17208:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   17209:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   17210:        *(UINT8  *)(mem + 0x487) = 0x60;
                   17211:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     17212: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17213:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     17214: #endif
1.1.1.14  root     17215:        
                   17216:        // initial screen
                   17217:        SMALL_RECT rect;
                   17218:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   17219:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   17220:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   17221:                for(int x = 0; x < scr_width; x++) {
                   17222:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   17223:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   17224:                }
                   17225:        }
1.1       root     17226:        
1.1.1.19  root     17227:        // init mcb
1.1       root     17228:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     17229:        
                   17230:        // iret table
                   17231:        // note: int 2eh vector should address the routine in command.com,
                   17232:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   17233:        // so move iret table into allocated memory block
                   17234:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33  root     17235:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19  root     17236:        IRET_TOP = seg << 4;
                   17237:        seg += IRET_SIZE >> 4;
1.1.1.25  root     17238:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     17239:        
                   17240:        // dummy xms/ems device
1.1.1.33  root     17241:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     17242:        XMS_TOP = seg << 4;
                   17243:        seg += XMS_SIZE >> 4;
                   17244:        
                   17245:        // environment
1.1.1.33  root     17246:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     17247:        int env_seg = seg;
                   17248:        int ofs = 0;
1.1.1.32  root     17249:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   17250:        char comspec_added = 0;
1.1.1.33  root     17251:        char lastdrive_added = 0;
1.1.1.32  root     17252:        char env_msdos_path[ENV_SIZE] = {0};
                   17253:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     17254:        char prompt_added = 0;
1.1.1.32  root     17255:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     17256:        char tz_added = 0;
1.1.1.45  root     17257:        const char *path, *short_path;
1.1.1.32  root     17258:        
                   17259:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   17260:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17261:                        strcpy(env_append, short_path);
                   17262:                }
                   17263:        }
                   17264:        if((path = getenv("APPEND")) != NULL) {
                   17265:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17266:                        if(env_append[0] != '\0') {
                   17267:                                strcat(env_append, ";");
                   17268:                        }
                   17269:                        strcat(env_append, short_path);
                   17270:                }
                   17271:        }
                   17272:        
                   17273:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   17274:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17275:                        strcpy(comspec_path, short_path);
                   17276:                }
                   17277:        }
                   17278:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   17279:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17280:                        strcpy(comspec_path, short_path);
                   17281:                }
                   17282:        }
1.1       root     17283:        
1.1.1.28  root     17284:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     17285:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17286:                        strcpy(env_msdos_path, short_path);
                   17287:                        strcpy(env_path, short_path);
1.1.1.14  root     17288:                }
                   17289:        }
1.1.1.28  root     17290:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     17291:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17292:                        if(env_path[0] != '\0') {
                   17293:                                strcat(env_path, ";");
                   17294:                        }
                   17295:                        strcat(env_path, short_path);
1.1.1.9   root     17296:                }
                   17297:        }
1.1.1.32  root     17298:        
                   17299:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   17300:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     17301:        }
1.1.1.32  root     17302:        for(int i = 0; i < 4; i++) {
                   17303:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   17304:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   17305:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17306:                                strcpy(env_temp, short_path);
                   17307:                                break;
                   17308:                        }
                   17309:                }
1.1.1.24  root     17310:        }
1.1.1.32  root     17311:        
1.1.1.9   root     17312:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     17313:                // lower to upper
1.1.1.28  root     17314:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     17315:                strcpy(tmp, *p);
                   17316:                for(int i = 0;; i++) {
                   17317:                        if(tmp[i] == '=') {
                   17318:                                tmp[i] = '\0';
                   17319:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     17320:                                my_strupr(name);
1.1       root     17321:                                tmp[i] = '=';
                   17322:                                break;
                   17323:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     17324:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     17325:                        }
                   17326:                }
1.1.1.33  root     17327:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   17328:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   17329:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     17330:                        // ignore non standard environments
                   17331:                } else {
1.1.1.33  root     17332:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     17333:                                if(env_append[0] != '\0') {
                   17334:                                        sprintf(tmp, "APPEND=%s", env_append);
                   17335:                                } else {
                   17336:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   17337:                                }
                   17338:                                append_added = 1;
                   17339:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     17340:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     17341:                                comspec_added = 1;
1.1.1.33  root     17342:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   17343:                                char *env = getenv("MSDOS_LASTDRIVE");
                   17344:                                if(env != NULL) {
                   17345:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   17346:                                }
                   17347:                                lastdrive_added = 1;
                   17348:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     17349:                                if(env_msdos_path[0] != '\0') {
                   17350:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   17351:                                } else {
                   17352:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   17353:                                }
1.1.1.33  root     17354:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     17355:                                if(env_path[0] != '\0') {
                   17356:                                        sprintf(tmp, "PATH=%s", env_path);
                   17357:                                } else {
                   17358:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   17359:                                }
1.1.1.32  root     17360:                                path_added = 1;
1.1.1.33  root     17361:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   17362:                                prompt_added = 1;
1.1.1.28  root     17363:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   17364:                                if(env_temp[0] != '\0') {
                   17365:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   17366:                                } else {
                   17367:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   17368:                                }
1.1.1.32  root     17369:                                temp_added = 1;
1.1.1.33  root     17370:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     17371:                                if(env_temp[0] != '\0') {
                   17372:                                        sprintf(tmp, "TMP=%s", env_temp);
                   17373:                                } else {
                   17374:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     17375:                                }
1.1.1.32  root     17376:                                tmp_added = 1;
1.1.1.33  root     17377:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   17378:                                char *env = getenv("MSDOS_TZ");
                   17379:                                if(env != NULL) {
                   17380:                                        sprintf(tmp, "TZ=%s", env);
                   17381:                                }
                   17382:                                tz_added = 1;
1.1       root     17383:                        }
                   17384:                        int len = strlen(tmp);
1.1.1.14  root     17385:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     17386:                                fatalerror("too many environments\n");
                   17387:                        }
                   17388:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   17389:                        ofs += len + 1;
                   17390:                }
                   17391:        }
1.1.1.32  root     17392:        if(!append_added && env_append[0] != '\0') {
                   17393:                #define SET_ENV(name, value) { \
                   17394:                        char tmp[ENV_SIZE]; \
                   17395:                        sprintf(tmp, "%s=%s", name, value); \
                   17396:                        int len = strlen(tmp); \
                   17397:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   17398:                                fatalerror("too many environments\n"); \
                   17399:                        } \
                   17400:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   17401:                        ofs += len + 1; \
                   17402:                }
                   17403:                SET_ENV("APPEND", env_append);
                   17404:        }
                   17405:        if(!comspec_added) {
                   17406:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   17407:        }
1.1.1.33  root     17408:        if(!lastdrive_added) {
                   17409:                SET_ENV("LASTDRIVE", "Z");
                   17410:        }
1.1.1.32  root     17411:        if(!path_added) {
                   17412:                SET_ENV("PATH", env_path);
                   17413:        }
1.1.1.33  root     17414:        if(!prompt_added) {
                   17415:                SET_ENV("PROMPT", "$P$G");
                   17416:        }
1.1.1.32  root     17417:        if(!temp_added) {
                   17418:                SET_ENV("TEMP", env_temp);
                   17419:        }
                   17420:        if(!tmp_added) {
                   17421:                SET_ENV("TMP", env_temp);
                   17422:        }
1.1.1.33  root     17423:        if(!tz_added) {
                   17424:                TIME_ZONE_INFORMATION tzi;
                   17425:                HKEY hKey, hSubKey;
                   17426:                char tzi_std_name[64];
                   17427:                char tz_std[8] = "GMT";
                   17428:                char tz_dlt[8] = "GST";
                   17429:                char tz_value[32];
                   17430:                
                   17431:                // timezone name from GetTimeZoneInformation may not be english
                   17432:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   17433:                setlocale(LC_CTYPE, "");
                   17434:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   17435:                
                   17436:                // get english timezone name from registry
                   17437:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   17438:                        for(DWORD i = 0; !tz_added; i++) {
                   17439:                                char reg_name[256], sub_key[1024], std_name[256];
                   17440:                                DWORD size;
                   17441:                                FILETIME ftTime;
                   17442:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   17443:                                
                   17444:                                if(result == ERROR_SUCCESS) {
                   17445:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   17446:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   17447:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   17448:                                                        // search english timezone name from table
1.1.1.37  root     17449:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     17450:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   17451:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   17452:                                                                                if(tz_table[j].std != NULL) {
                   17453:                                                                                        strcpy(tz_std, tz_table[j].std);
                   17454:                                                                                }
                   17455:                                                                                if(tz_table[j].dlt != NULL) {
                   17456:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   17457:                                                                                }
                   17458:                                                                                tz_added = 1;
                   17459:                                                                                break;
                   17460:                                                                        }
                   17461:                                                                }
                   17462:                                                        }
                   17463:                                                }
                   17464:                                                RegCloseKey(hSubKey);
                   17465:                                        }
                   17466:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   17467:                                        break;
                   17468:                                }
                   17469:                        }
                   17470:                        RegCloseKey(hKey);
                   17471:                }
                   17472:                if((tzi.Bias % 60) != 0) {
                   17473:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   17474:                } else {
                   17475:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   17476:                }
                   17477:                if(daylight) {
                   17478:                        strcat(tz_value, tz_dlt);
                   17479:                }
                   17480:                SET_ENV("TZ", tz_value);
                   17481:        }
1.1       root     17482:        seg += (ENV_SIZE >> 4);
                   17483:        
                   17484:        // psp
1.1.1.33  root     17485:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     17486:        current_psp = seg;
1.1.1.35  root     17487:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   17488:        psp->parent_psp = current_psp;
1.1       root     17489:        seg += (PSP_SIZE >> 4);
                   17490:        
1.1.1.19  root     17491:        // first free mcb in conventional memory
1.1.1.33  root     17492:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     17493:        first_mcb = seg;
                   17494:        
1.1.1.19  root     17495:        // dummy mcb to link to umb
1.1.1.33  root     17496: #if 0
1.1.1.39  root     17497:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     17498: #else
1.1.1.39  root     17499:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     17500: #endif
1.1.1.19  root     17501:        
                   17502:        // first free mcb in upper memory block
1.1.1.8   root     17503:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     17504:        
1.1.1.29  root     17505: #ifdef SUPPORT_HMA
                   17506:        // first free mcb in high memory area
                   17507:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   17508: #endif
                   17509:        
1.1.1.26  root     17510:        // interrupt vector
                   17511:        for(int i = 0; i < 0x80; i++) {
                   17512:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   17513:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   17514:        }
1.1.1.49  root     17515:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffc:0018 irq0 (system timer)
                   17516:        *(UINT16 *)(mem + 4 * 0x08 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     17517:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   17518:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   17519:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   17520:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
1.1.1.49  root     17521:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffc:0000 irq12 (mouse)
                   17522:        *(UINT16 *)(mem + 4 * 0x74 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     17523:        
1.1.1.29  root     17524:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     17525:        static const struct {
                   17526:                UINT16 attributes;
                   17527:                char *dev_name;
                   17528:        } dummy_devices[] = {
                   17529:                {0x8013, "CON     "},
                   17530:                {0x8000, "AUX     "},
                   17531:                {0xa0c0, "PRN     "},
                   17532:                {0x8008, "CLOCK$  "},
                   17533:                {0x8000, "COM1    "},
                   17534:                {0xa0c0, "LPT1    "},
                   17535:                {0xa0c0, "LPT2    "},
                   17536:                {0xa0c0, "LPT3    "},
                   17537:                {0x8000, "COM2    "},
                   17538:                {0x8000, "COM3    "},
                   17539:                {0x8000, "COM4    "},
1.1.1.30  root     17540: //             {0xc000, "CONFIG$ "},
                   17541:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     17542:        };
                   17543:        static const UINT8 dummy_device_routine[] = {
                   17544:                // from NUL device of Windows 98 SE
                   17545:                // or word ptr ES:[BX+03],0100
                   17546:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   17547:                // retf
                   17548:                0xcb,
                   17549:        };
1.1.1.29  root     17550:        device_t *last = NULL;
1.1.1.32  root     17551:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     17552:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     17553:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   17554:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     17555:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     17556:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   17557:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     17558:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     17559:                last = device;
                   17560:        }
                   17561:        if(last != NULL) {
                   17562:                last->next_driver.w.l = 0;
                   17563:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     17564:        }
1.1.1.29  root     17565:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     17566:        
1.1.1.25  root     17567:        // dos info
                   17568:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   17569:        dos_info->magic_word = 1;
                   17570:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   17571:        dos_info->first_dpb.w.l = 0;
                   17572:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   17573:        dos_info->first_sft.w.l = 0;
                   17574:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     17575:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     17576:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     17577:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     17578:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   17579:        dos_info->max_sector_len = 512;
                   17580:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   17581:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   17582:        dos_info->cds.w.l = 0;
                   17583:        dos_info->cds.w.h = CDS_TOP >> 4;
                   17584:        dos_info->fcb_table.w.l = 0;
                   17585:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   17586:        dos_info->last_drive = 'Z' - 'A' + 1;
                   17587:        dos_info->buffers_x = 20;
                   17588:        dos_info->buffers_y = 0;
                   17589:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     17590:        dos_info->nul_device.next_driver.w.l = 22;
                   17591:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     17592:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     17593:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   17594:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     17595:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   17596:        dos_info->disk_buf_heads.w.l = 0;
                   17597:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     17598:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     17599:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   17600:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     17601:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     17602:        
                   17603:        char *env;
                   17604:        if((env = getenv("LASTDRIVE")) != NULL) {
                   17605:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   17606:                        dos_info->last_drive = env[0] - 'A' + 1;
                   17607:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   17608:                        dos_info->last_drive = env[0] - 'a' + 1;
                   17609:                }
                   17610:        }
                   17611:        if((env = getenv("windir")) != NULL) {
                   17612:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   17613:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   17614:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   17615:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   17616:                }
                   17617:        }
                   17618: #if defined(HAS_I386)
                   17619:        dos_info->i386_or_later = 1;
                   17620: #else
                   17621:        dos_info->i386_or_later = 0;
                   17622: #endif
                   17623:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   17624:        
1.1.1.27  root     17625:        // ems (int 67h) and xms
1.1.1.25  root     17626:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   17627:        xms_device->next_driver.w.l = 0xffff;
                   17628:        xms_device->next_driver.w.h = 0xffff;
                   17629:        xms_device->attributes = 0xc000;
1.1.1.29  root     17630:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   17631:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     17632:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   17633:        
1.1.1.26  root     17634:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   17635:        mem[XMS_TOP + 0x13] = 0x68;
                   17636:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     17637: #ifdef SUPPORT_XMS
                   17638:        if(support_xms) {
1.1.1.26  root     17639:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   17640:                mem[XMS_TOP + 0x16] = 0x69;
                   17641:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     17642:        } else
                   17643: #endif
1.1.1.26  root     17644:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     17645:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     17646:        
1.1.1.26  root     17647:        // irq12 routine (mouse)
1.1.1.49  root     17648:        mem[DUMMY_TOP + 0x00] = 0xcd;   // int 6ah (dummy)
                   17649:        mem[DUMMY_TOP + 0x01] = 0x6a;
                   17650:        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far mouse
                   17651:        mem[DUMMY_TOP + 0x03] = 0xff;
                   17652:        mem[DUMMY_TOP + 0x04] = 0xff;
                   17653:        mem[DUMMY_TOP + 0x05] = 0xff;
                   17654:        mem[DUMMY_TOP + 0x06] = 0xff;
                   17655:        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17656:        mem[DUMMY_TOP + 0x08] = 0x6b;
                   17657:        mem[DUMMY_TOP + 0x09] = 0xcf;   // iret
1.1.1.24  root     17658:        
1.1.1.27  root     17659:        // case map routine
1.1.1.49  root     17660:        mem[DUMMY_TOP + 0x0a] = 0xcd;   // int 6ch (dummy)
                   17661:        mem[DUMMY_TOP + 0x0b] = 0x6c;
                   17662:        mem[DUMMY_TOP + 0x0c] = 0xcb;   // retf
1.1.1.27  root     17663:        
                   17664:        // font read routine
1.1.1.49  root     17665:        mem[DUMMY_TOP + 0x0d] = 0xcd;   // int 6dh (dummy)
                   17666:        mem[DUMMY_TOP + 0x0e] = 0x6d;
                   17667:        mem[DUMMY_TOP + 0x0f] = 0xcb;   // retf
1.1.1.19  root     17668:        
1.1.1.32  root     17669:        // error message read routine
1.1.1.49  root     17670:        mem[DUMMY_TOP + 0x10] = 0xcd;   // int 6eh (dummy)
                   17671:        mem[DUMMY_TOP + 0x11] = 0x6e;
                   17672:        mem[DUMMY_TOP + 0x12] = 0xcb;   // retf
1.1.1.32  root     17673:        
1.1.1.35  root     17674:        // dummy loop to wait BIOS/DOS service is done
1.1.1.49  root     17675:        mem[DUMMY_TOP + 0x13] = 0xe6;   // out f7h, al
                   17676:        mem[DUMMY_TOP + 0x14] = 0xf7;
                   17677:        mem[DUMMY_TOP + 0x15] = 0x78;   // js/jns -4
                   17678:        mem[DUMMY_TOP + 0x16] = 0xfc;
                   17679:        mem[DUMMY_TOP + 0x17] = 0xcb;   // retf
1.1.1.35  root     17680:        
1.1.1.50! root     17681:        // irq0 routine (system timer)
1.1.1.49  root     17682:        mem[DUMMY_TOP + 0x18] = 0xcd;   // int 1ch
                   17683:        mem[DUMMY_TOP + 0x19] = 0x1c;
                   17684:        mem[DUMMY_TOP + 0x1a] = 0xea;   // jmp far (IRET_TOP >> 4):0008
                   17685:        mem[DUMMY_TOP + 0x1b] = 0x08;
                   17686:        mem[DUMMY_TOP + 0x1c] = 0x00;
                   17687:        mem[DUMMY_TOP + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   17688:        mem[DUMMY_TOP + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
                   17689:        
                   17690:        // alter page map and call routine
                   17691:        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   17692:        mem[DUMMY_TOP + 0x20] = 0xff;
                   17693:        mem[DUMMY_TOP + 0x21] = 0xff;
                   17694:        mem[DUMMY_TOP + 0x22] = 0xff;
                   17695:        mem[DUMMY_TOP + 0x23] = 0xff;
                   17696:        mem[DUMMY_TOP + 0x24] = 0xcd;   // int 6fh (dummy)
                   17697:        mem[DUMMY_TOP + 0x25] = 0x6f;
                   17698:        mem[DUMMY_TOP + 0x26] = 0xcb;   // retf
1.1.1.14  root     17699:        
1.1.1.50! root     17700:        // call int 29h routine
        !          17701:        mem[DUMMY_TOP + 0x27] = 0xcd;   // int 29h
        !          17702:        mem[DUMMY_TOP + 0x28] = 0x29;
        !          17703:        mem[DUMMY_TOP + 0x29] = 0xcb;   // retf
        !          17704:        
1.1.1.26  root     17705:        // boot routine
1.1.1.49  root     17706:        mem[0xffff0 + 0x00] = 0xf4;     // halt
                   17707:        mem[0xffff0 + 0x05] = '0';      // rom date
                   17708:        mem[0xffff0 + 0x06] = '2';
                   17709:        mem[0xffff0 + 0x07] = '/';
                   17710:        mem[0xffff0 + 0x08] = '2';
                   17711:        mem[0xffff0 + 0x09] = '2';
                   17712:        mem[0xffff0 + 0x0a] = '/';
                   17713:        mem[0xffff0 + 0x0b] = '0';
                   17714:        mem[0xffff0 + 0x0c] = '6';
                   17715:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id
                   17716:        mem[0xffff0 + 0x0f] = 0x00;
1.1.1.24  root     17717:        
1.1       root     17718:        // param block
                   17719:        // + 0: param block (22bytes)
                   17720:        // +24: fcb1/2 (20bytes)
                   17721:        // +44: command tail (128bytes)
                   17722:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   17723:        param->env_seg = 0;
                   17724:        param->cmd_line.w.l = 44;
                   17725:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   17726:        param->fcb1.w.l = 24;
                   17727:        param->fcb1.w.h = (WORK_TOP >> 4);
                   17728:        param->fcb2.w.l = 24;
                   17729:        param->fcb2.w.h = (WORK_TOP >> 4);
                   17730:        
                   17731:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   17732:        
                   17733:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   17734:        if(argc > 1) {
                   17735:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   17736:                for(int i = 2; i < argc; i++) {
                   17737:                        char tmp[128];
                   17738:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   17739:                        strcpy(cmd_line->cmd, tmp);
                   17740:                }
                   17741:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   17742:        } else {
                   17743:                cmd_line->len = 0;
                   17744:        }
                   17745:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   17746:        
                   17747:        // system file table
1.1.1.21  root     17748:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   17749:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     17750:        
1.1.1.19  root     17751:        // disk buffer header (from DOSBox)
                   17752:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   17753:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   17754:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   17755:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   17756:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   17757:        
1.1       root     17758:        // fcb table
                   17759:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     17760:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     17761:        
1.1.1.41  root     17762:        // drive parameter block
1.1.1.42  root     17763:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     17764:                // may be a floppy drive
1.1.1.44  root     17765:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   17766:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   17767:                cds->drive_attrib = 0x4000;     // physical drive
                   17768:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   17769:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   17770:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   17771:                cds->bs_offset = 2;
                   17772:                
1.1.1.41  root     17773:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   17774:                dpb->drive_num = i;
                   17775:                dpb->unit_num = i;
1.1.1.43  root     17776:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   17777:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     17778:        }
                   17779:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     17780:                msdos_cds_update(i);
1.1.1.42  root     17781:                UINT16 seg, ofs;
                   17782:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     17783:        }
                   17784:        
1.1.1.17  root     17785:        // nls stuff
                   17786:        msdos_nls_tables_init();
1.1       root     17787:        
                   17788:        // execute command
1.1.1.28  root     17789:        try {
                   17790:                if(msdos_process_exec(argv[0], param, 0)) {
                   17791:                        fatalerror("'%s' not found\n", argv[0]);
                   17792:                }
                   17793:        } catch(...) {
                   17794:                // we should not reach here :-(
                   17795:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     17796:        }
                   17797:        retval = 0;
                   17798:        return(0);
                   17799: }
                   17800: 
                   17801: #define remove_std_file(path) { \
                   17802:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   17803:        if(fd != -1) { \
                   17804:                _lseek(fd, 0, SEEK_END); \
                   17805:                int size = _tell(fd); \
                   17806:                _close(fd); \
                   17807:                if(size == 0) { \
                   17808:                        remove(path); \
                   17809:                } \
                   17810:        } \
                   17811: }
                   17812: 
                   17813: void msdos_finish()
                   17814: {
                   17815:        for(int i = 0; i < MAX_FILES; i++) {
                   17816:                if(file_handler[i].valid) {
                   17817:                        _close(i);
                   17818:                }
                   17819:        }
1.1.1.21  root     17820: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     17821:        remove_std_file("stdaux.txt");
1.1.1.21  root     17822: #endif
1.1.1.30  root     17823: #ifdef SUPPORT_XMS
                   17824:        msdos_xms_finish();
                   17825: #endif
1.1       root     17826:        msdos_dbcs_table_finish();
                   17827: }
                   17828: 
                   17829: /* ----------------------------------------------------------------------------
                   17830:        PC/AT hardware emulation
                   17831: ---------------------------------------------------------------------------- */
                   17832: 
                   17833: void hardware_init()
                   17834: {
1.1.1.3   root     17835:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     17836:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     17837:        m_IF = 1;
1.1.1.3   root     17838: #if defined(HAS_I386)
1.1       root     17839:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   17840:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     17841: #endif
                   17842:        i386_set_a20_line(0);
1.1.1.14  root     17843:        
1.1.1.19  root     17844:        ems_init();
1.1.1.25  root     17845:        dma_init();
1.1       root     17846:        pic_init();
1.1.1.25  root     17847:        pio_init();
1.1.1.8   root     17848: #ifdef PIT_ALWAYS_RUNNING
                   17849:        pit_init();
                   17850: #else
1.1       root     17851:        pit_active = 0;
                   17852: #endif
1.1.1.25  root     17853:        sio_init();
1.1.1.8   root     17854:        cmos_init();
                   17855:        kbd_init();
1.1       root     17856: }
                   17857: 
1.1.1.10  root     17858: void hardware_finish()
                   17859: {
                   17860: #if defined(HAS_I386)
                   17861:        vtlb_free(m_vtlb);
                   17862: #endif
1.1.1.19  root     17863:        ems_finish();
1.1.1.37  root     17864:        pio_finish();
1.1.1.25  root     17865:        sio_finish();
1.1.1.10  root     17866: }
                   17867: 
1.1.1.28  root     17868: void hardware_release()
                   17869: {
                   17870:        // release hardware resources when this program will be terminated abnormally
                   17871: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     17872:        if(fp_debug_log != NULL) {
                   17873:                fclose(fp_debug_log);
                   17874:                fp_debug_log = NULL;
1.1.1.28  root     17875:        }
                   17876: #endif
                   17877: #if defined(HAS_I386)
                   17878:        vtlb_free(m_vtlb);
                   17879: #endif
                   17880:        ems_release();
1.1.1.37  root     17881:        pio_release();
1.1.1.28  root     17882:        sio_release();
                   17883: }
                   17884: 
1.1       root     17885: void hardware_run()
                   17886: {
1.1.1.50! root     17887:        m_halted = 0;
        !          17888:        m_int_num = -1;
        !          17889:        
1.1.1.22  root     17890: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     17891:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     17892:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     17893: #endif
1.1.1.3   root     17894:        while(!m_halted) {
1.1.1.50! root     17895:                hardware_run_cpu();
1.1       root     17896:        }
1.1.1.22  root     17897: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     17898:        if(fp_debug_log != NULL) {
                   17899:                fclose(fp_debug_log);
                   17900:                fp_debug_log = NULL;
1.1.1.28  root     17901:        }
1.1.1.22  root     17902: #endif
1.1       root     17903: }
                   17904: 
1.1.1.50! root     17905: inline void hardware_run_cpu()
        !          17906: {
        !          17907: #if defined(HAS_I386)
        !          17908:        CPU_EXECUTE_CALL(i386);
        !          17909:        if(m_eip != m_prev_eip) {
        !          17910:                idle_ops++;
        !          17911:        }
        !          17912: #else
        !          17913:        CPU_EXECUTE_CALL(CPU_MODEL);
        !          17914:        if(m_pc != m_prevpc) {
        !          17915:                idle_ops++;
        !          17916:        }
        !          17917: #endif
        !          17918: #ifdef USE_DEBUGGER
        !          17919:        // Disallow reentering CPU_EXECUTE() in msdos_syscall()
        !          17920:        if(m_int_num >= 0) {
        !          17921:                unsigned num = (unsigned)m_int_num;
        !          17922:                m_int_num = -1;
        !          17923:                msdos_syscall(num);
        !          17924:        }
        !          17925: #endif
        !          17926:        if(++update_ops == UPDATE_OPS) {
        !          17927:                update_ops = 0;
        !          17928:                hardware_update();
        !          17929:        }
        !          17930: }
        !          17931: 
1.1       root     17932: void hardware_update()
                   17933: {
1.1.1.8   root     17934:        static UINT32 prev_time = 0;
                   17935:        UINT32 cur_time = timeGetTime();
                   17936:        
                   17937:        if(prev_time != cur_time) {
                   17938:                // update pit and raise irq0
                   17939: #ifndef PIT_ALWAYS_RUNNING
                   17940:                if(pit_active)
                   17941: #endif
                   17942:                {
                   17943:                        if(pit_run(0, cur_time)) {
                   17944:                                pic_req(0, 0, 1);
                   17945:                        }
                   17946:                        pit_run(1, cur_time);
                   17947:                        pit_run(2, cur_time);
                   17948:                }
1.1.1.24  root     17949:                
1.1.1.25  root     17950:                // update sio and raise irq4/3
1.1.1.29  root     17951:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     17952:                        sio_update(c);
                   17953:                }
                   17954:                
1.1.1.24  root     17955:                // update keyboard and mouse
1.1.1.14  root     17956:                static UINT32 prev_tick = 0;
                   17957:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     17958:                
1.1.1.14  root     17959:                if(prev_tick != cur_tick) {
                   17960:                        // update keyboard flags
                   17961:                        UINT8 state;
1.1.1.24  root     17962:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   17963:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   17964:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   17965:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   17966:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   17967:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   17968:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   17969:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     17970:                        mem[0x417] = state;
                   17971:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   17972:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   17973:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   17974:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     17975: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   17976: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     17977:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   17978:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   17979:                        mem[0x418] = state;
                   17980:                        
1.1.1.24  root     17981:                        // update console input if needed
1.1.1.34  root     17982:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     17983:                                update_console_input();
                   17984:                        }
                   17985:                        
                   17986:                        // raise irq1 if key is pressed/released
                   17987:                        if(key_changed) {
1.1.1.8   root     17988:                                pic_req(0, 1, 1);
1.1.1.24  root     17989:                                key_changed = false;
                   17990:                        }
                   17991:                        
                   17992:                        // raise irq12 if mouse status is changed
1.1.1.43  root     17993:                        if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
                   17994:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   17995:                                mouse.status_irq_alt = 0; // ???
1.1.1.24  root     17996:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     17997:                                pic_req(1, 4, 1);
                   17998:                        } else {
                   17999:                                for(int i = 0; i < 8; i++) {
                   18000:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   18001:                                                mouse.status_irq = 0; // ???
                   18002:                                                mouse.status_irq_alt = 0;
                   18003:                                                for(int j = 0; j < 8; j++) {
                   18004:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   18005:                                                                mouse.status_irq_alt |=  (1 << j);
                   18006:                                                                mouse.status_alt     &= ~(1 << j);
                   18007:                                                        }
                   18008:                                                }
                   18009:                                                pic_req(1, 4, 1);
                   18010:                                                break;
                   18011:                                        }
                   18012:                                }
1.1.1.8   root     18013:                        }
1.1.1.24  root     18014:                        
1.1.1.14  root     18015:                        prev_tick = cur_tick;
1.1.1.8   root     18016:                }
1.1.1.24  root     18017:                
1.1.1.19  root     18018:                // update daily timer counter
                   18019:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     18020:                
1.1.1.8   root     18021:                prev_time = cur_time;
1.1       root     18022:        }
                   18023: }
                   18024: 
1.1.1.19  root     18025: // ems
                   18026: 
                   18027: void ems_init()
                   18028: {
                   18029:        memset(ems_handles, 0, sizeof(ems_handles));
                   18030:        memset(ems_pages, 0, sizeof(ems_pages));
                   18031:        free_ems_pages = MAX_EMS_PAGES;
                   18032: }
                   18033: 
                   18034: void ems_finish()
                   18035: {
1.1.1.28  root     18036:        ems_release();
                   18037: }
                   18038: 
                   18039: void ems_release()
                   18040: {
1.1.1.31  root     18041:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     18042:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     18043:                        free(ems_handles[i].buffer);
                   18044:                        ems_handles[i].buffer = NULL;
                   18045:                }
                   18046:        }
                   18047: }
                   18048: 
                   18049: void ems_allocate_pages(int handle, int pages)
                   18050: {
                   18051:        if(pages > 0) {
                   18052:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18053:        } else {
                   18054:                ems_handles[handle].buffer = NULL;
                   18055:        }
                   18056:        ems_handles[handle].pages = pages;
                   18057:        ems_handles[handle].allocated = true;
                   18058:        free_ems_pages -= pages;
                   18059: }
                   18060: 
                   18061: void ems_reallocate_pages(int handle, int pages)
                   18062: {
                   18063:        if(ems_handles[handle].allocated) {
                   18064:                if(ems_handles[handle].pages != pages) {
                   18065:                        UINT8 *new_buffer = NULL;
                   18066:                        
                   18067:                        if(pages > 0) {
                   18068:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18069:                        }
1.1.1.32  root     18070:                        if(ems_handles[handle].buffer != NULL) {
                   18071:                                if(new_buffer != NULL) {
1.1.1.19  root     18072:                                        if(pages > ems_handles[handle].pages) {
                   18073:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   18074:                                        } else {
                   18075:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   18076:                                        }
                   18077:                                }
                   18078:                                free(ems_handles[handle].buffer);
                   18079:                                ems_handles[handle].buffer = NULL;
                   18080:                        }
                   18081:                        free_ems_pages += ems_handles[handle].pages;
                   18082:                        
                   18083:                        ems_handles[handle].buffer = new_buffer;
                   18084:                        ems_handles[handle].pages = pages;
                   18085:                        free_ems_pages -= pages;
                   18086:                }
                   18087:        } else {
                   18088:                ems_allocate_pages(handle, pages);
                   18089:        }
                   18090: }
                   18091: 
                   18092: void ems_release_pages(int handle)
                   18093: {
                   18094:        if(ems_handles[handle].allocated) {
1.1.1.32  root     18095:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     18096:                        free(ems_handles[handle].buffer);
                   18097:                        ems_handles[handle].buffer = NULL;
                   18098:                }
                   18099:                free_ems_pages += ems_handles[handle].pages;
                   18100:                ems_handles[handle].allocated = false;
                   18101:        }
                   18102: }
                   18103: 
                   18104: void ems_map_page(int physical, int handle, int logical)
                   18105: {
                   18106:        if(ems_pages[physical].mapped) {
                   18107:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   18108:                        return;
                   18109:                }
                   18110:                ems_unmap_page(physical);
                   18111:        }
1.1.1.32  root     18112:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18113:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   18114:        }
                   18115:        ems_pages[physical].handle = handle;
                   18116:        ems_pages[physical].page = logical;
                   18117:        ems_pages[physical].mapped = true;
                   18118: }
                   18119: 
                   18120: void ems_unmap_page(int physical)
                   18121: {
                   18122:        if(ems_pages[physical].mapped) {
                   18123:                int handle = ems_pages[physical].handle;
                   18124:                int logical = ems_pages[physical].page;
                   18125:                
1.1.1.32  root     18126:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18127:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   18128:                }
                   18129:                ems_pages[physical].mapped = false;
                   18130:        }
                   18131: }
                   18132: 
1.1.1.25  root     18133: // dma
1.1       root     18134: 
1.1.1.25  root     18135: void dma_init()
1.1       root     18136: {
1.1.1.26  root     18137:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     18138:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     18139: //             for(int ch = 0; ch < 4; ch++) {
                   18140: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   18141: //             }
1.1.1.25  root     18142:                dma_reset(c);
                   18143:        }
1.1       root     18144: }
                   18145: 
1.1.1.25  root     18146: void dma_reset(int c)
1.1       root     18147: {
1.1.1.25  root     18148:        dma[c].low_high = false;
                   18149:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   18150:        dma[c].mask = 0xff;
                   18151: }
                   18152: 
                   18153: void dma_write(int c, UINT32 addr, UINT8 data)
                   18154: {
                   18155:        int ch = (addr >> 1) & 3;
                   18156:        UINT8 bit = 1 << (data & 3);
                   18157:        
                   18158:        switch(addr & 0x0f) {
                   18159:        case 0x00: case 0x02: case 0x04: case 0x06:
                   18160:                if(dma[c].low_high) {
                   18161:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     18162:                } else {
1.1.1.25  root     18163:                        dma[c].ch[ch].bareg.b.l = data;
                   18164:                }
                   18165:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   18166:                dma[c].low_high = !dma[c].low_high;
                   18167:                break;
                   18168:        case 0x01: case 0x03: case 0x05: case 0x07:
                   18169:                if(dma[c].low_high) {
                   18170:                        dma[c].ch[ch].bcreg.b.h = data;
                   18171:                } else {
                   18172:                        dma[c].ch[ch].bcreg.b.l = data;
                   18173:                }
                   18174:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   18175:                dma[c].low_high = !dma[c].low_high;
                   18176:                break;
                   18177:        case 0x08:
                   18178:                // command register
                   18179:                dma[c].cmd = data;
                   18180:                break;
                   18181:        case 0x09:
                   18182:                // dma[c].request register
                   18183:                if(data & 4) {
                   18184:                        if(!(dma[c].req & bit)) {
                   18185:                                dma[c].req |= bit;
                   18186: //                             dma_run(c, ch);
                   18187:                        }
                   18188:                } else {
                   18189:                        dma[c].req &= ~bit;
                   18190:                }
                   18191:                break;
                   18192:        case 0x0a:
                   18193:                // single mask register
                   18194:                if(data & 4) {
                   18195:                        dma[c].mask |= bit;
                   18196:                } else {
                   18197:                        dma[c].mask &= ~bit;
                   18198:                }
                   18199:                break;
                   18200:        case 0x0b:
                   18201:                // mode register
                   18202:                dma[c].ch[data & 3].mode = data;
                   18203:                break;
                   18204:        case 0x0c:
                   18205:                dma[c].low_high = false;
                   18206:                break;
                   18207:        case 0x0d:
                   18208:                // clear master
                   18209:                dma_reset(c);
                   18210:                break;
                   18211:        case 0x0e:
                   18212:                // clear mask register
                   18213:                dma[c].mask = 0;
                   18214:                break;
                   18215:        case 0x0f:
                   18216:                // all mask register
                   18217:                dma[c].mask = data & 0x0f;
                   18218:                break;
                   18219:        }
                   18220: }
                   18221: 
                   18222: UINT8 dma_read(int c, UINT32 addr)
                   18223: {
                   18224:        int ch = (addr >> 1) & 3;
                   18225:        UINT8 val = 0xff;
                   18226:        
                   18227:        switch(addr & 0x0f) {
                   18228:        case 0x00: case 0x02: case 0x04: case 0x06:
                   18229:                if(dma[c].low_high) {
                   18230:                        val = dma[c].ch[ch].areg.b.h;
                   18231:                } else {
                   18232:                        val = dma[c].ch[ch].areg.b.l;
                   18233:                }
                   18234:                dma[c].low_high = !dma[c].low_high;
                   18235:                return(val);
                   18236:        case 0x01: case 0x03: case 0x05: case 0x07:
                   18237:                if(dma[c].low_high) {
                   18238:                        val = dma[c].ch[ch].creg.b.h;
                   18239:                } else {
                   18240:                        val = dma[c].ch[ch].creg.b.l;
                   18241:                }
                   18242:                dma[c].low_high = !dma[c].low_high;
                   18243:                return(val);
                   18244:        case 0x08:
                   18245:                // status register
                   18246:                val = (dma[c].req << 4) | dma[c].tc;
                   18247:                dma[c].tc = 0;
                   18248:                return(val);
                   18249:        case 0x0d:
1.1.1.26  root     18250:                // temporary register (intel 82374 does not support)
1.1.1.25  root     18251:                return(dma[c].tmp & 0xff);
1.1.1.26  root     18252:        case 0x0f:
                   18253:                // mask register (intel 82374 does support)
                   18254:                return(dma[c].mask);
1.1.1.25  root     18255:        }
                   18256:        return(0xff);
                   18257: }
                   18258: 
                   18259: void dma_page_write(int c, int ch, UINT8 data)
                   18260: {
                   18261:        dma[c].ch[ch].pagereg = data;
                   18262: }
                   18263: 
                   18264: UINT8 dma_page_read(int c, int ch)
                   18265: {
                   18266:        return(dma[c].ch[ch].pagereg);
                   18267: }
                   18268: 
                   18269: void dma_run(int c, int ch)
                   18270: {
                   18271:        UINT8 bit = 1 << ch;
                   18272:        
                   18273:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   18274:                // execute dma
                   18275:                while(dma[c].req & bit) {
                   18276:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   18277:                                // memory -> memory
                   18278:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   18279:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   18280:                                
                   18281:                                if(c == 0) {
                   18282:                                        dma[c].tmp = read_byte(saddr);
                   18283:                                        write_byte(daddr, dma[c].tmp);
                   18284:                                } else {
                   18285:                                        dma[c].tmp = read_word(saddr << 1);
                   18286:                                        write_word(daddr << 1, dma[c].tmp);
                   18287:                                }
                   18288:                                if(!(dma[c].cmd & 0x02)) {
                   18289:                                        if(dma[c].ch[0].mode & 0x20) {
                   18290:                                                dma[c].ch[0].areg.w--;
                   18291:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   18292:                                                        dma[c].ch[0].pagereg--;
                   18293:                                                }
                   18294:                                        } else {
                   18295:                                                dma[c].ch[0].areg.w++;
                   18296:                                                if(dma[c].ch[0].areg.w == 0) {
                   18297:                                                        dma[c].ch[0].pagereg++;
                   18298:                                                }
                   18299:                                        }
                   18300:                                }
                   18301:                                if(dma[c].ch[1].mode & 0x20) {
                   18302:                                        dma[c].ch[1].areg.w--;
                   18303:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   18304:                                                dma[c].ch[1].pagereg--;
                   18305:                                        }
                   18306:                                } else {
                   18307:                                        dma[c].ch[1].areg.w++;
                   18308:                                        if(dma[c].ch[1].areg.w == 0) {
                   18309:                                                dma[c].ch[1].pagereg++;
                   18310:                                        }
                   18311:                                }
                   18312:                                
                   18313:                                // check dma condition
                   18314:                                if(dma[c].ch[0].creg.w-- == 0) {
                   18315:                                        if(dma[c].ch[0].mode & 0x10) {
                   18316:                                                // self initialize
                   18317:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   18318:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   18319:                                        } else {
                   18320: //                                             dma[c].mask |= bit;
                   18321:                                        }
                   18322:                                }
                   18323:                                if(dma[c].ch[1].creg.w-- == 0) {
                   18324:                                        // terminal count
                   18325:                                        if(dma[c].ch[1].mode & 0x10) {
                   18326:                                                // self initialize
                   18327:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   18328:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   18329:                                        } else {
                   18330:                                                dma[c].mask |= bit;
                   18331:                                        }
                   18332:                                        dma[c].req &= ~bit;
                   18333:                                        dma[c].tc |= bit;
                   18334:                                }
                   18335:                        } else {
                   18336:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   18337:                                
                   18338:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   18339:                                        // verify
                   18340:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   18341:                                        // io -> memory
                   18342:                                        if(c == 0) {
                   18343:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   18344:                                                write_byte(addr, dma[c].tmp);
                   18345:                                        } else {
                   18346:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   18347:                                                write_word(addr << 1, dma[c].tmp);
                   18348:                                        }
                   18349:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   18350:                                        // memory -> io
                   18351:                                        if(c == 0) {
                   18352:                                                dma[c].tmp = read_byte(addr);
                   18353:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   18354:                                        } else {
                   18355:                                                dma[c].tmp = read_word(addr << 1);
                   18356:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   18357:                                        }
                   18358:                                }
                   18359:                                if(dma[c].ch[ch].mode & 0x20) {
                   18360:                                        dma[c].ch[ch].areg.w--;
                   18361:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   18362:                                                dma[c].ch[ch].pagereg--;
                   18363:                                        }
                   18364:                                } else {
                   18365:                                        dma[c].ch[ch].areg.w++;
                   18366:                                        if(dma[c].ch[ch].areg.w == 0) {
                   18367:                                                dma[c].ch[ch].pagereg++;
                   18368:                                        }
                   18369:                                }
                   18370:                                
                   18371:                                // check dma condition
                   18372:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   18373:                                        // terminal count
                   18374:                                        if(dma[c].ch[ch].mode & 0x10) {
                   18375:                                                // self initialize
                   18376:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   18377:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   18378:                                        } else {
                   18379:                                                dma[c].mask |= bit;
                   18380:                                        }
                   18381:                                        dma[c].req &= ~bit;
                   18382:                                        dma[c].tc |= bit;
                   18383:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   18384:                                        // single mode
                   18385:                                        break;
                   18386:                                }
                   18387:                        }
                   18388:                }
                   18389:        }
                   18390: }
                   18391: 
                   18392: // pic
                   18393: 
                   18394: void pic_init()
                   18395: {
                   18396:        memset(pic, 0, sizeof(pic));
                   18397:        pic[0].imr = pic[1].imr = 0xff;
                   18398:        
                   18399:        // from bochs bios
                   18400:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   18401:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   18402:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   18403:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   18404:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   18405:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   18406:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   18407:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   18408:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   18409: }
                   18410: 
                   18411: void pic_write(int c, UINT32 addr, UINT8 data)
                   18412: {
                   18413:        if(addr & 1) {
                   18414:                if(pic[c].icw2_r) {
                   18415:                        // icw2
                   18416:                        pic[c].icw2 = data;
                   18417:                        pic[c].icw2_r = 0;
                   18418:                } else if(pic[c].icw3_r) {
                   18419:                        // icw3
                   18420:                        pic[c].icw3 = data;
                   18421:                        pic[c].icw3_r = 0;
                   18422:                } else if(pic[c].icw4_r) {
                   18423:                        // icw4
                   18424:                        pic[c].icw4 = data;
                   18425:                        pic[c].icw4_r = 0;
                   18426:                } else {
                   18427:                        // ocw1
1.1       root     18428:                        pic[c].imr = data;
                   18429:                }
                   18430:        } else {
                   18431:                if(data & 0x10) {
                   18432:                        // icw1
                   18433:                        pic[c].icw1 = data;
                   18434:                        pic[c].icw2_r = 1;
                   18435:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   18436:                        pic[c].icw4_r = data & 1;
                   18437:                        pic[c].irr = 0;
                   18438:                        pic[c].isr = 0;
                   18439:                        pic[c].imr = 0;
                   18440:                        pic[c].prio = 0;
                   18441:                        if(!(pic[c].icw1 & 1)) {
                   18442:                                pic[c].icw4 = 0;
                   18443:                        }
                   18444:                        pic[c].ocw3 = 0;
                   18445:                } else if(data & 8) {
                   18446:                        // ocw3
                   18447:                        if(!(data & 2)) {
                   18448:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   18449:                        }
                   18450:                        if(!(data & 0x40)) {
                   18451:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   18452:                        }
                   18453:                        pic[c].ocw3 = data;
                   18454:                } else {
                   18455:                        // ocw2
                   18456:                        int level = 0;
                   18457:                        if(data & 0x40) {
                   18458:                                level = data & 7;
                   18459:                        } else {
                   18460:                                if(!pic[c].isr) {
                   18461:                                        return;
                   18462:                                }
                   18463:                                level = pic[c].prio;
                   18464:                                while(!(pic[c].isr & (1 << level))) {
                   18465:                                        level = (level + 1) & 7;
                   18466:                                }
                   18467:                        }
                   18468:                        if(data & 0x80) {
                   18469:                                pic[c].prio = (level + 1) & 7;
                   18470:                        }
                   18471:                        if(data & 0x20) {
                   18472:                                pic[c].isr &= ~(1 << level);
                   18473:                        }
                   18474:                }
                   18475:        }
                   18476:        pic_update();
                   18477: }
                   18478: 
                   18479: UINT8 pic_read(int c, UINT32 addr)
                   18480: {
                   18481:        if(addr & 1) {
                   18482:                return(pic[c].imr);
                   18483:        } else {
                   18484:                // polling mode is not supported...
                   18485:                //if(pic[c].ocw3 & 4) {
                   18486:                //      return ???;
                   18487:                //}
                   18488:                if(pic[c].ocw3 & 1) {
                   18489:                        return(pic[c].isr);
                   18490:                } else {
                   18491:                        return(pic[c].irr);
                   18492:                }
                   18493:        }
                   18494: }
                   18495: 
                   18496: void pic_req(int c, int level, int signal)
                   18497: {
                   18498:        if(signal) {
                   18499:                pic[c].irr |= (1 << level);
                   18500:        } else {
                   18501:                pic[c].irr &= ~(1 << level);
                   18502:        }
                   18503:        pic_update();
                   18504: }
                   18505: 
                   18506: int pic_ack()
                   18507: {
                   18508:        // ack (INTA=L)
                   18509:        pic[pic_req_chip].isr |= pic_req_bit;
                   18510:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   18511:        if(pic_req_chip > 0) {
                   18512:                // update isr and irr of master
                   18513:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   18514:                pic[pic_req_chip - 1].isr |= slave;
                   18515:                pic[pic_req_chip - 1].irr &= ~slave;
                   18516:        }
                   18517:        //if(pic[pic_req_chip].icw4 & 1) {
                   18518:                // 8086 mode
                   18519:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   18520:        //} else {
                   18521:        //      // 8080 mode
                   18522:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   18523:        //      if(pic[pic_req_chip].icw1 & 4) {
                   18524:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   18525:        //      } else {
                   18526:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   18527:        //      }
                   18528:        //      vector = 0xcd | (addr << 8);
                   18529:        //}
                   18530:        if(pic[pic_req_chip].icw4 & 2) {
                   18531:                // auto eoi
                   18532:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   18533:        }
                   18534:        return(vector);
                   18535: }
                   18536: 
                   18537: void pic_update()
                   18538: {
                   18539:        for(int c = 0; c < 2; c++) {
                   18540:                UINT8 irr = pic[c].irr;
                   18541:                if(c + 1 < 2) {
                   18542:                        // this is master
                   18543:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   18544:                                // request from slave
                   18545:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   18546:                        }
                   18547:                }
                   18548:                irr &= (~pic[c].imr);
                   18549:                if(!irr) {
                   18550:                        break;
                   18551:                }
                   18552:                if(!(pic[c].ocw3 & 0x20)) {
                   18553:                        irr |= pic[c].isr;
                   18554:                }
                   18555:                int level = pic[c].prio;
                   18556:                UINT8 bit = 1 << level;
                   18557:                while(!(irr & bit)) {
                   18558:                        level = (level + 1) & 7;
                   18559:                        bit = 1 << level;
                   18560:                }
                   18561:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   18562:                        // check slave
                   18563:                        continue;
                   18564:                }
                   18565:                if(pic[c].isr & bit) {
                   18566:                        break;
                   18567:                }
                   18568:                // interrupt request
                   18569:                pic_req_chip = c;
                   18570:                pic_req_level = level;
                   18571:                pic_req_bit = bit;
1.1.1.3   root     18572:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     18573:                return;
                   18574:        }
1.1.1.3   root     18575:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     18576: }
1.1       root     18577: 
1.1.1.25  root     18578: // pio
                   18579: 
                   18580: void pio_init()
                   18581: {
1.1.1.38  root     18582: //     bool conv_mode = (GetConsoleCP() == 932);
                   18583:        
1.1.1.26  root     18584:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     18585:        
1.1.1.25  root     18586:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     18587:                pio[c].stat = 0xdf;
1.1.1.25  root     18588:                pio[c].ctrl = 0x0c;
1.1.1.38  root     18589: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     18590:        }
                   18591: }
                   18592: 
1.1.1.37  root     18593: void pio_finish()
                   18594: {
                   18595:        pio_release();
                   18596: }
                   18597: 
                   18598: void pio_release()
                   18599: {
                   18600:        for(int c = 0; c < 2; c++) {
                   18601:                if(pio[c].fp != NULL) {
1.1.1.38  root     18602:                        if(pio[c].jis_mode) {
                   18603:                                fputc(0x1c, pio[c].fp);
                   18604:                                fputc(0x2e, pio[c].fp);
                   18605:                        }
1.1.1.37  root     18606:                        fclose(pio[c].fp);
                   18607:                        pio[c].fp = NULL;
                   18608:                }
                   18609:        }
                   18610: }
                   18611: 
1.1.1.25  root     18612: void pio_write(int c, UINT32 addr, UINT8 data)
                   18613: {
                   18614:        switch(addr & 3) {
                   18615:        case 0:
                   18616:                pio[c].data = data;
                   18617:                break;
                   18618:        case 2:
1.1.1.37  root     18619:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   18620:                        // strobe H -> L
                   18621:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   18622:                                // auto feed
                   18623:                                printer_out(c, 0x0d);
                   18624:                                printer_out(c, 0x0a);
                   18625:                        } else {
                   18626:                                printer_out(c, pio[c].data);
                   18627:                        }
                   18628:                        pio[c].stat &= ~0x40; // set ack
                   18629:                }
1.1.1.25  root     18630:                pio[c].ctrl = data;
                   18631:                break;
                   18632:        }
                   18633: }
                   18634: 
                   18635: UINT8 pio_read(int c, UINT32 addr)
                   18636: {
                   18637:        switch(addr & 3) {
                   18638:        case 0:
1.1.1.37  root     18639:                if(pio[c].ctrl & 0x20) {
                   18640:                        // input mode
                   18641:                        return(0xff);
                   18642:                }
1.1.1.25  root     18643:                return(pio[c].data);
                   18644:        case 1:
1.1.1.37  root     18645:                {
                   18646:                        UINT8 stat = pio[c].stat;
                   18647:                        pio[c].stat |= 0x40; // clear ack
                   18648:                        return(stat);
                   18649:                }
1.1.1.25  root     18650:        case 2:
                   18651:                return(pio[c].ctrl);
                   18652:        }
                   18653:        return(0xff);
                   18654: }
                   18655: 
1.1.1.37  root     18656: void printer_out(int c, UINT8 data)
                   18657: {
                   18658:        SYSTEMTIME time;
1.1.1.38  root     18659:        bool jis_mode = false;
1.1.1.37  root     18660:        
                   18661:        GetLocalTime(&time);
                   18662:        
                   18663:        if(pio[c].fp != NULL) {
                   18664:                // if at least 1000ms passed from last written, close the current file
                   18665:                FILETIME ftime1;
                   18666:                FILETIME ftime2;
                   18667:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   18668:                SystemTimeToFileTime(&time, &ftime2);
                   18669:                INT64 *time1 = (INT64 *)&ftime1;
                   18670:                INT64 *time2 = (INT64 *)&ftime2;
                   18671:                INT64 msec = (*time2 - *time1) / 10000;
                   18672:                
                   18673:                if(msec >= 1000) {
1.1.1.38  root     18674:                        if(pio[c].jis_mode) {
                   18675:                                fputc(0x1c, pio[c].fp);
                   18676:                                fputc(0x2e, pio[c].fp);
                   18677:                                jis_mode = true;
                   18678:                        }
1.1.1.37  root     18679:                        fclose(pio[c].fp);
                   18680:                        pio[c].fp = NULL;
                   18681:                }
                   18682:        }
                   18683:        if(pio[c].fp == NULL) {
                   18684:                // create a new file in the temp folder
                   18685:                char file_name[MAX_PATH];
                   18686:                
                   18687:                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);
                   18688:                if(GetTempPath(MAX_PATH, pio[c].path)) {
                   18689:                        strcat(pio[c].path, file_name);
                   18690:                } else {
                   18691:                        strcpy(pio[c].path, file_name);
                   18692:                }
1.1.1.38  root     18693:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     18694:        }
                   18695:        if(pio[c].fp != NULL) {
1.1.1.38  root     18696:                if(jis_mode) {
                   18697:                        fputc(0x1c, pio[c].fp);
                   18698:                        fputc(0x26, pio[c].fp);
                   18699:                }
1.1.1.37  root     18700:                fputc(data, pio[c].fp);
1.1.1.38  root     18701:                
                   18702:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   18703:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   18704:                        UINT8 buffer[4];
                   18705:                        fseek(pio[c].fp, 0, SEEK_SET);
                   18706:                        fread(buffer, 4, 1, pio[c].fp);
                   18707:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   18708:                                fclose(pio[c].fp);
                   18709:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   18710:                        }
                   18711:                }
1.1.1.37  root     18712:                pio[c].time = time;
                   18713:        }
                   18714: }
                   18715: 
1.1       root     18716: // pit
                   18717: 
1.1.1.22  root     18718: #define PIT_FREQ 1193182ULL
1.1       root     18719: #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)
                   18720: 
                   18721: void pit_init()
                   18722: {
1.1.1.8   root     18723:        memset(pit, 0, sizeof(pit));
1.1       root     18724:        for(int ch = 0; ch < 3; ch++) {
                   18725:                pit[ch].count = 0x10000;
                   18726:                pit[ch].ctrl_reg = 0x34;
                   18727:                pit[ch].mode = 3;
                   18728:        }
                   18729:        
                   18730:        // from bochs bios
                   18731:        pit_write(3, 0x34);
                   18732:        pit_write(0, 0x00);
                   18733:        pit_write(0, 0x00);
                   18734: }
                   18735: 
                   18736: void pit_write(int ch, UINT8 val)
                   18737: {
1.1.1.8   root     18738: #ifndef PIT_ALWAYS_RUNNING
1.1       root     18739:        if(!pit_active) {
                   18740:                pit_active = 1;
                   18741:                pit_init();
                   18742:        }
1.1.1.8   root     18743: #endif
1.1       root     18744:        switch(ch) {
                   18745:        case 0:
                   18746:        case 1:
                   18747:        case 2:
                   18748:                // write count register
                   18749:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   18750:                        if(pit[ch].ctrl_reg & 0x10) {
                   18751:                                pit[ch].low_write = 1;
                   18752:                        }
                   18753:                        if(pit[ch].ctrl_reg & 0x20) {
                   18754:                                pit[ch].high_write = 1;
                   18755:                        }
                   18756:                }
                   18757:                if(pit[ch].low_write) {
                   18758:                        pit[ch].count_reg = val;
                   18759:                        pit[ch].low_write = 0;
                   18760:                } else if(pit[ch].high_write) {
                   18761:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   18762:                                pit[ch].count_reg = val << 8;
                   18763:                        } else {
                   18764:                                pit[ch].count_reg |= val << 8;
                   18765:                        }
                   18766:                        pit[ch].high_write = 0;
                   18767:                }
                   18768:                // start count
1.1.1.8   root     18769:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   18770:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   18771:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   18772:                                pit[ch].prev_time = timeGetTime();
                   18773:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     18774:                        }
                   18775:                }
                   18776:                break;
                   18777:        case 3: // ctrl reg
                   18778:                if((val & 0xc0) == 0xc0) {
                   18779:                        // i8254 read-back command
                   18780:                        for(ch = 0; ch < 3; ch++) {
                   18781:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   18782:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   18783:                                        pit[ch].status_latched = 1;
                   18784:                                }
                   18785:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   18786:                                        pit_latch_count(ch);
                   18787:                                }
                   18788:                        }
                   18789:                        break;
                   18790:                }
                   18791:                ch = (val >> 6) & 3;
                   18792:                if(val & 0x30) {
1.1.1.35  root     18793:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     18794:                        pit[ch].mode = modes[(val >> 1) & 7];
                   18795:                        pit[ch].count_latched = 0;
                   18796:                        pit[ch].low_read = pit[ch].high_read = 0;
                   18797:                        pit[ch].low_write = pit[ch].high_write = 0;
                   18798:                        pit[ch].ctrl_reg = val;
                   18799:                        // stop count
1.1.1.8   root     18800:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     18801:                        pit[ch].count_reg = 0;
                   18802:                } else if(!pit[ch].count_latched) {
                   18803:                        pit_latch_count(ch);
                   18804:                }
                   18805:                break;
                   18806:        }
                   18807: }
                   18808: 
                   18809: UINT8 pit_read(int ch)
                   18810: {
1.1.1.8   root     18811: #ifndef PIT_ALWAYS_RUNNING
1.1       root     18812:        if(!pit_active) {
                   18813:                pit_active = 1;
                   18814:                pit_init();
                   18815:        }
1.1.1.8   root     18816: #endif
1.1       root     18817:        switch(ch) {
                   18818:        case 0:
                   18819:        case 1:
                   18820:        case 2:
                   18821:                if(pit[ch].status_latched) {
                   18822:                        pit[ch].status_latched = 0;
                   18823:                        return(pit[ch].status);
                   18824:                }
                   18825:                // if not latched, through current count
                   18826:                if(!pit[ch].count_latched) {
                   18827:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   18828:                                pit_latch_count(ch);
                   18829:                        }
                   18830:                }
                   18831:                // return latched count
                   18832:                if(pit[ch].low_read) {
                   18833:                        pit[ch].low_read = 0;
                   18834:                        if(!pit[ch].high_read) {
                   18835:                                pit[ch].count_latched = 0;
                   18836:                        }
                   18837:                        return(pit[ch].latch & 0xff);
                   18838:                } else if(pit[ch].high_read) {
                   18839:                        pit[ch].high_read = 0;
                   18840:                        pit[ch].count_latched = 0;
                   18841:                        return((pit[ch].latch >> 8) & 0xff);
                   18842:                }
                   18843:        }
                   18844:        return(0xff);
                   18845: }
                   18846: 
1.1.1.8   root     18847: int pit_run(int ch, UINT32 cur_time)
1.1       root     18848: {
1.1.1.8   root     18849:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     18850:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     18851:                pit[ch].prev_time = pit[ch].expired_time;
                   18852:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   18853:                if(cur_time >= pit[ch].expired_time) {
                   18854:                        pit[ch].prev_time = cur_time;
                   18855:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     18856:                }
1.1.1.8   root     18857:                return(1);
1.1       root     18858:        }
1.1.1.8   root     18859:        return(0);
1.1       root     18860: }
                   18861: 
                   18862: void pit_latch_count(int ch)
                   18863: {
1.1.1.8   root     18864:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     18865:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     18866:                pit_run(ch, cur_time);
                   18867:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     18868:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   18869:                
                   18870:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   18871:                        // decrement counter in 1msec period
                   18872:                        if(pit[ch].next_latch == 0) {
                   18873:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   18874:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   18875:                        }
                   18876:                        if(pit[ch].latch > pit[ch].next_latch) {
                   18877:                                pit[ch].latch--;
                   18878:                        }
                   18879:                } else {
                   18880:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   18881:                        pit[ch].next_latch = 0;
                   18882:                }
1.1.1.8   root     18883:        } else {
                   18884:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     18885:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     18886:        }
                   18887:        pit[ch].count_latched = 1;
                   18888:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   18889:                // lower byte
                   18890:                pit[ch].low_read = 1;
                   18891:                pit[ch].high_read = 0;
                   18892:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   18893:                // upper byte
                   18894:                pit[ch].low_read = 0;
                   18895:                pit[ch].high_read = 1;
                   18896:        } else {
                   18897:                // lower -> upper
1.1.1.14  root     18898:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     18899:        }
                   18900: }
                   18901: 
1.1.1.8   root     18902: int pit_get_expired_time(int ch)
1.1       root     18903: {
1.1.1.22  root     18904:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   18905:        UINT64 val = pit[ch].accum >> 10;
                   18906:        pit[ch].accum -= val << 10;
                   18907:        return((val != 0) ? val : 1);
1.1.1.8   root     18908: }
                   18909: 
1.1.1.25  root     18910: // sio
                   18911: 
                   18912: void sio_init()
                   18913: {
1.1.1.26  root     18914:        memset(sio, 0, sizeof(sio));
                   18915:        memset(sio_mt, 0, sizeof(sio_mt));
                   18916:        
1.1.1.29  root     18917:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     18918:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   18919:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   18920:                
                   18921:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   18922:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     18923:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   18924:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     18925:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   18926:                sio[c].irq_identify = 0x01;     // no pending irq
                   18927:                
                   18928:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   18929:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   18930:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   18931:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   18932:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   18933:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   18934:                
1.1.1.26  root     18935:                if(sio_port_number[c] != 0) {
1.1.1.25  root     18936:                        sio[c].channel = c;
                   18937:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   18938:                }
                   18939:        }
                   18940: }
                   18941: 
                   18942: void sio_finish()
                   18943: {
1.1.1.29  root     18944:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     18945:                if(sio_mt[c].hThread != NULL) {
                   18946:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   18947:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     18948:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     18949:                }
                   18950:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   18951:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   18952:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   18953:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   18954:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   18955:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     18956:        }
                   18957:        sio_release();
                   18958: }
                   18959: 
                   18960: void sio_release()
                   18961: {
1.1.1.29  root     18962:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     18963:                // sio_thread() may access the resources :-(
1.1.1.32  root     18964:                bool running = (sio_mt[c].hThread != NULL);
                   18965:                
                   18966:                if(running) {
                   18967:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   18968:                }
                   18969:                if(sio[c].send_buffer != NULL) {
                   18970:                        sio[c].send_buffer->release();
                   18971:                        delete sio[c].send_buffer;
                   18972:                        sio[c].send_buffer = NULL;
                   18973:                }
                   18974:                if(running) {
                   18975:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   18976:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   18977:                }
                   18978:                if(sio[c].recv_buffer != NULL) {
                   18979:                        sio[c].recv_buffer->release();
                   18980:                        delete sio[c].recv_buffer;
                   18981:                        sio[c].recv_buffer = NULL;
                   18982:                }
                   18983:                if(running) {
                   18984:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     18985:                }
1.1.1.25  root     18986:        }
                   18987: }
                   18988: 
                   18989: void sio_write(int c, UINT32 addr, UINT8 data)
                   18990: {
                   18991:        switch(addr & 7) {
                   18992:        case 0:
                   18993:                if(sio[c].selector & 0x80) {
                   18994:                        if(sio[c].divisor.b.l != data) {
                   18995:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   18996:                                sio[c].divisor.b.l = data;
                   18997:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   18998:                        }
                   18999:                } else {
                   19000:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19001:                        if(sio[c].send_buffer != NULL) {
                   19002:                                sio[c].send_buffer->write(data);
                   19003:                        }
1.1.1.25  root     19004:                        // transmitter holding/shift registers are not empty
                   19005:                        sio[c].line_stat_buf &= ~0x60;
                   19006:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19007:                        
                   19008:                        if(sio[c].irq_enable & 0x02) {
                   19009:                                sio_update_irq(c);
                   19010:                        }
                   19011:                }
                   19012:                break;
                   19013:        case 1:
                   19014:                if(sio[c].selector & 0x80) {
                   19015:                        if(sio[c].divisor.b.h != data) {
                   19016:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19017:                                sio[c].divisor.b.h = data;
                   19018:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19019:                        }
                   19020:                } else {
                   19021:                        if(sio[c].irq_enable != data) {
                   19022:                                sio[c].irq_enable = data;
                   19023:                                sio_update_irq(c);
                   19024:                        }
                   19025:                }
                   19026:                break;
                   19027:        case 3:
                   19028:                {
                   19029:                        UINT8 line_ctrl = data & 0x3f;
                   19030:                        bool set_brk = ((data & 0x40) != 0);
                   19031:                        
                   19032:                        if(sio[c].line_ctrl != line_ctrl) {
                   19033:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19034:                                sio[c].line_ctrl = line_ctrl;
                   19035:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19036:                        }
                   19037:                        if(sio[c].set_brk != set_brk) {
                   19038:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   19039:                                sio[c].set_brk = set_brk;
                   19040:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19041:                        }
                   19042:                }
                   19043:                sio[c].selector = data;
                   19044:                break;
                   19045:        case 4:
                   19046:                {
                   19047:                        bool set_dtr = ((data & 0x01) != 0);
                   19048:                        bool set_rts = ((data & 0x02) != 0);
                   19049:                        
                   19050:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     19051: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     19052:                                sio[c].set_dtr = set_dtr;
                   19053:                                sio[c].set_rts = set_rts;
1.1.1.26  root     19054: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19055:                                
                   19056:                                bool state_changed = false;
                   19057:                                
                   19058:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19059:                                if(set_dtr) {
                   19060:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   19061:                                } else {
                   19062:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   19063:                                }
                   19064:                                if(set_rts) {
                   19065:                                        sio[c].modem_stat |= 0x10;      // cts on
                   19066:                                } else {
                   19067:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   19068:                                }
                   19069:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   19070:                                        if(!(sio[c].modem_stat & 0x02)) {
                   19071:                                                if(sio[c].irq_enable & 0x08) {
                   19072:                                                        state_changed = true;
                   19073:                                                }
                   19074:                                                sio[c].modem_stat |= 0x02;
                   19075:                                        }
                   19076:                                }
                   19077:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   19078:                                        if(!(sio[c].modem_stat & 0x01)) {
                   19079:                                                if(sio[c].irq_enable & 0x08) {
                   19080:                                                        state_changed = true;
                   19081:                                                }
                   19082:                                                sio[c].modem_stat |= 0x01;
                   19083:                                        }
                   19084:                                }
                   19085:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19086:                                
                   19087:                                if(state_changed) {
                   19088:                                        sio_update_irq(c);
                   19089:                                }
1.1.1.25  root     19090:                        }
                   19091:                }
                   19092:                sio[c].modem_ctrl = data;
                   19093:                break;
                   19094:        case 7:
                   19095:                sio[c].scratch = data;
                   19096:                break;
                   19097:        }
                   19098: }
                   19099: 
                   19100: UINT8 sio_read(int c, UINT32 addr)
                   19101: {
                   19102:        switch(addr & 7) {
                   19103:        case 0:
                   19104:                if(sio[c].selector & 0x80) {
                   19105:                        return(sio[c].divisor.b.l);
                   19106:                } else {
                   19107:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19108:                        UINT8 data = 0;
                   19109:                        if(sio[c].recv_buffer != NULL) {
                   19110:                                data = sio[c].recv_buffer->read();
                   19111:                        }
1.1.1.25  root     19112:                        // data is not ready
                   19113:                        sio[c].line_stat_buf &= ~0x01;
                   19114:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19115:                        
                   19116:                        if(sio[c].irq_enable & 0x01) {
                   19117:                                sio_update_irq(c);
                   19118:                        }
                   19119:                        return(data);
                   19120:                }
                   19121:        case 1:
                   19122:                if(sio[c].selector & 0x80) {
                   19123:                        return(sio[c].divisor.b.h);
                   19124:                } else {
                   19125:                        return(sio[c].irq_enable);
                   19126:                }
                   19127:        case 2:
                   19128:                return(sio[c].irq_identify);
                   19129:        case 3:
                   19130:                return(sio[c].selector);
                   19131:        case 4:
                   19132:                return(sio[c].modem_ctrl);
                   19133:        case 5:
                   19134:                {
                   19135:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   19136:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   19137:                        sio[c].line_stat_err = 0x00;
                   19138:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   19139:                        
                   19140:                        bool state_changed = false;
                   19141:                        
                   19142:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   19143:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19144:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     19145:                                        // transmitter holding register will be empty first
                   19146:                                        if(sio[c].irq_enable & 0x02) {
                   19147:                                                state_changed = true;
                   19148:                                        }
                   19149:                                        sio[c].line_stat_buf |= 0x20;
                   19150:                                }
                   19151:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   19152:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   19153:                                // transmitter shift register will be empty later
                   19154:                                sio[c].line_stat_buf |= 0x40;
                   19155:                        }
                   19156:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   19157:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19158:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     19159:                                        // data is ready
                   19160:                                        if(sio[c].irq_enable & 0x01) {
                   19161:                                                state_changed = true;
                   19162:                                        }
                   19163:                                        sio[c].line_stat_buf |= 0x01;
                   19164:                                }
                   19165:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19166:                        }
                   19167:                        if(state_changed) {
                   19168:                                sio_update_irq(c);
                   19169:                        }
                   19170:                        return(val);
                   19171:                }
                   19172:        case 6:
                   19173:                {
                   19174:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   19175:                        UINT8 val = sio[c].modem_stat;
                   19176:                        sio[c].modem_stat &= 0xf0;
                   19177:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   19178:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19179:                        
                   19180:                        if(sio[c].modem_ctrl & 0x10) {
                   19181:                                // loop-back
                   19182:                                val &= 0x0f;
                   19183:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   19184:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   19185:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   19186:                        }
                   19187:                        return(val);
                   19188:                }
                   19189:        case 7:
                   19190:                return(sio[c].scratch);
                   19191:        }
                   19192:        return(0xff);
                   19193: }
                   19194: 
                   19195: void sio_update(int c)
                   19196: {
                   19197:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   19198:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19199:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     19200:                        // transmitter holding/shift registers will be empty
                   19201:                        sio[c].line_stat_buf |= 0x60;
                   19202:                }
                   19203:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   19204:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   19205:                // transmitter shift register will be empty
                   19206:                sio[c].line_stat_buf |= 0x40;
                   19207:        }
                   19208:        if(!(sio[c].line_stat_buf & 0x01)) {
                   19209:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19210:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     19211:                        // data is ready
                   19212:                        sio[c].line_stat_buf |= 0x01;
                   19213:                }
                   19214:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19215:        }
                   19216:        sio_update_irq(c);
                   19217: }
                   19218: 
                   19219: void sio_update_irq(int c)
                   19220: {
                   19221:        int level = -1;
                   19222:        
                   19223:        if(sio[c].irq_enable & 0x08) {
                   19224:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19225:                if((sio[c].modem_stat & 0x0f) != 0) {
                   19226:                        level = 0;
                   19227:                }
                   19228:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19229:        }
                   19230:        if(sio[c].irq_enable & 0x02) {
                   19231:                if(sio[c].line_stat_buf & 0x20) {
                   19232:                        level = 1;
                   19233:                }
                   19234:        }
                   19235:        if(sio[c].irq_enable & 0x01) {
                   19236:                if(sio[c].line_stat_buf & 0x01) {
                   19237:                        level = 2;
                   19238:                }
                   19239:        }
                   19240:        if(sio[c].irq_enable & 0x04) {
                   19241:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   19242:                if(sio[c].line_stat_err != 0) {
                   19243:                        level = 3;
                   19244:                }
                   19245:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   19246:        }
1.1.1.29  root     19247:        
                   19248:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     19249:        if(level != -1) {
                   19250:                sio[c].irq_identify = level << 1;
1.1.1.29  root     19251:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     19252:        } else {
                   19253:                sio[c].irq_identify = 1;
1.1.1.29  root     19254:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     19255:        }
                   19256: }
                   19257: 
                   19258: DWORD WINAPI sio_thread(void *lpx)
                   19259: {
                   19260:        volatile sio_t *p = (sio_t *)lpx;
                   19261:        sio_mt_t *q = &sio_mt[p->channel];
                   19262:        
                   19263:        char name[] = "COM1";
1.1.1.26  root     19264:        name[3] = '0' + sio_port_number[p->channel];
                   19265:        HANDLE hComm = NULL;
                   19266:        COMMPROP commProp;
                   19267:        DCB dcb;
                   19268:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   19269:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   19270:        
                   19271:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   19272:                if(GetCommProperties(hComm, &commProp)) {
                   19273:                        dwSettableBaud = commProp.dwSettableBaud;
                   19274:                }
1.1.1.25  root     19275:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     19276: //             EscapeCommFunction(hComm, SETRTS);
                   19277: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     19278:                
                   19279:                while(!m_halted) {
                   19280:                        // setup comm port
                   19281:                        bool comm_state_changed = false;
                   19282:                        
                   19283:                        EnterCriticalSection(&q->csLineCtrl);
                   19284:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   19285:                                p->prev_divisor = p->divisor.w;
                   19286:                                p->prev_line_ctrl = p->line_ctrl;
                   19287:                                comm_state_changed = true;
                   19288:                        }
                   19289:                        LeaveCriticalSection(&q->csLineCtrl);
                   19290:                        
                   19291:                        if(comm_state_changed) {
1.1.1.26  root     19292:                                if(GetCommState(hComm, &dcb)) {
                   19293: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   19294:                                        DWORD baud = 115200 / p->prev_divisor;
                   19295:                                        dcb.BaudRate = 9600; // default
                   19296:                                        
                   19297:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   19298:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   19299:                                        // 134.5bps is not supported ???
                   19300: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   19301:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   19302:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   19303:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   19304:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   19305:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   19306:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   19307:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   19308:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   19309:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   19310: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   19311: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   19312: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   19313:                                        
                   19314:                                        switch(p->prev_line_ctrl & 0x03) {
                   19315:                                        case 0x00: dcb.ByteSize = 5; break;
                   19316:                                        case 0x01: dcb.ByteSize = 6; break;
                   19317:                                        case 0x02: dcb.ByteSize = 7; break;
                   19318:                                        case 0x03: dcb.ByteSize = 8; break;
                   19319:                                        }
                   19320:                                        switch(p->prev_line_ctrl & 0x04) {
                   19321:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   19322:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   19323:                                        }
                   19324:                                        switch(p->prev_line_ctrl & 0x38) {
                   19325:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   19326:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   19327:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   19328:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   19329:                                        default:   dcb.Parity = NOPARITY;    break;
                   19330:                                        }
                   19331:                                        dcb.fBinary = TRUE;
                   19332:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   19333:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   19334:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   19335:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   19336:                                        dcb.fTXContinueOnXoff = TRUE;
                   19337:                                        dcb.fOutX = dcb.fInX = FALSE;
                   19338:                                        dcb.fErrorChar = FALSE;
                   19339:                                        dcb.fNull = FALSE;
                   19340:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   19341:                                        dcb.fAbortOnError = FALSE;
                   19342:                                        
                   19343:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     19344:                                }
                   19345:                                
                   19346:                                // check again to apply all comm state changes
                   19347:                                Sleep(10);
                   19348:                                continue;
                   19349:                        }
                   19350:                        
                   19351:                        // set comm pins
                   19352:                        bool change_brk = false;
1.1.1.26  root     19353: //                     bool change_rts = false;
                   19354: //                     bool change_dtr = false;
1.1.1.25  root     19355:                        
                   19356:                        EnterCriticalSection(&q->csModemCtrl);
                   19357:                        if(p->prev_set_brk != p->set_brk) {
                   19358:                                p->prev_set_brk = p->set_brk;
                   19359:                                change_brk = true;
                   19360:                        }
1.1.1.26  root     19361: //                     if(p->prev_set_rts != p->set_rts) {
                   19362: //                             p->prev_set_rts = p->set_rts;
                   19363: //                             change_rts = true;
                   19364: //                     }
                   19365: //                     if(p->prev_set_dtr != p->set_dtr) {
                   19366: //                             p->prev_set_dtr = p->set_dtr;
                   19367: //                             change_dtr = true;
                   19368: //                     }
1.1.1.25  root     19369:                        LeaveCriticalSection(&q->csModemCtrl);
                   19370:                        
                   19371:                        if(change_brk) {
1.1.1.26  root     19372:                                static UINT32 clear_time = 0;
                   19373:                                if(p->prev_set_brk) {
                   19374:                                        EscapeCommFunction(hComm, SETBREAK);
                   19375:                                        clear_time = timeGetTime() + 200;
                   19376:                                } else {
                   19377:                                        // keep break for at least 200msec
                   19378:                                        UINT32 cur_time = timeGetTime();
                   19379:                                        if(clear_time > cur_time) {
                   19380:                                                Sleep(clear_time - cur_time);
                   19381:                                        }
                   19382:                                        EscapeCommFunction(hComm, CLRBREAK);
                   19383:                                }
1.1.1.25  root     19384:                        }
1.1.1.26  root     19385: //                     if(change_rts) {
                   19386: //                             if(p->prev_set_rts) {
                   19387: //                                     EscapeCommFunction(hComm, SETRTS);
                   19388: //                             } else {
                   19389: //                                     EscapeCommFunction(hComm, CLRRTS);
                   19390: //                             }
                   19391: //                     }
                   19392: //                     if(change_dtr) {
                   19393: //                             if(p->prev_set_dtr) {
                   19394: //                                     EscapeCommFunction(hComm, SETDTR);
                   19395: //                             } else {
                   19396: //                                     EscapeCommFunction(hComm, CLRDTR);
                   19397: //                             }
                   19398: //                     }
1.1.1.25  root     19399:                        
                   19400:                        // get comm pins
                   19401:                        DWORD dwModemStat = 0;
                   19402:                        
                   19403:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   19404:                                EnterCriticalSection(&q->csModemStat);
                   19405:                                if(dwModemStat & MS_RLSD_ON) {
                   19406:                                        p->modem_stat |= 0x80;
                   19407:                                } else {
                   19408:                                        p->modem_stat &= ~0x80;
                   19409:                                }
                   19410:                                if(dwModemStat & MS_RING_ON) {
                   19411:                                        p->modem_stat |= 0x40;
                   19412:                                } else {
                   19413:                                        p->modem_stat &= ~0x40;
                   19414:                                }
1.1.1.26  root     19415: //                             if(dwModemStat & MS_DSR_ON) {
                   19416: //                                     p->modem_stat |= 0x20;
                   19417: //                             } else {
                   19418: //                                     p->modem_stat &= ~0x20;
                   19419: //                             }
                   19420: //                             if(dwModemStat & MS_CTS_ON) {
                   19421: //                                     p->modem_stat |= 0x10;
                   19422: //                             } else {
                   19423: //                                     p->modem_stat &= ~0x10;
                   19424: //                             }
1.1.1.25  root     19425:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   19426:                                        p->modem_stat |= 0x08;
                   19427:                                }
                   19428:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   19429:                                        p->modem_stat |= 0x04;
                   19430:                                }
1.1.1.26  root     19431: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   19432: //                                     p->modem_stat |= 0x02;
                   19433: //                             }
                   19434: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   19435: //                                     p->modem_stat |= 0x01;
                   19436: //                             }
1.1.1.25  root     19437:                                LeaveCriticalSection(&q->csModemStat);
                   19438:                        }
                   19439:                        
                   19440:                        // send data
                   19441:                        DWORD dwSend = 0;
                   19442:                        
                   19443:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     19444:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     19445:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   19446:                        }
                   19447:                        LeaveCriticalSection(&q->csSendData);
                   19448:                        
                   19449:                        if(dwSend != 0) {
                   19450:                                DWORD dwWritten = 0;
                   19451:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   19452:                        }
                   19453:                        
                   19454:                        // get line status and recv data
                   19455:                        DWORD dwLineStat = 0;
                   19456:                        COMSTAT comStat;
                   19457:                        
                   19458:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   19459:                                EnterCriticalSection(&q->csLineStat);
                   19460:                                if(dwLineStat & CE_BREAK) {
                   19461:                                        p->line_stat_err |= 0x10;
                   19462:                                }
                   19463:                                if(dwLineStat & CE_FRAME) {
                   19464:                                        p->line_stat_err |= 0x08;
                   19465:                                }
                   19466:                                if(dwLineStat & CE_RXPARITY) {
                   19467:                                        p->line_stat_err |= 0x04;
                   19468:                                }
                   19469:                                if(dwLineStat & CE_OVERRUN) {
                   19470:                                        p->line_stat_err |= 0x02;
                   19471:                                }
                   19472:                                LeaveCriticalSection(&q->csLineStat);
                   19473:                                
                   19474:                                if(comStat.cbInQue != 0) {
                   19475:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     19476:                                        DWORD dwRecv = 0;
                   19477:                                        if(p->recv_buffer != NULL) {
                   19478:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   19479:                                        }
1.1.1.25  root     19480:                                        LeaveCriticalSection(&q->csRecvData);
                   19481:                                        
                   19482:                                        if(dwRecv != 0) {
                   19483:                                                DWORD dwRead = 0;
                   19484:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   19485:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     19486:                                                        if(p->recv_buffer != NULL) {
                   19487:                                                                for(int i = 0; i < dwRead; i++) {
                   19488:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   19489:                                                                }
1.1.1.25  root     19490:                                                        }
                   19491:                                                        LeaveCriticalSection(&q->csRecvData);
                   19492:                                                }
                   19493:                                        }
                   19494:                                }
                   19495:                        }
                   19496:                        Sleep(10);
                   19497:                }
                   19498:                CloseHandle(hComm);
                   19499:        }
                   19500:        return 0;
                   19501: }
                   19502: 
1.1.1.8   root     19503: // cmos
                   19504: 
                   19505: void cmos_init()
                   19506: {
                   19507:        memset(cmos, 0, sizeof(cmos));
                   19508:        cmos_addr = 0;
1.1       root     19509:        
1.1.1.8   root     19510:        // from DOSBox
                   19511:        cmos_write(0x0a, 0x26);
                   19512:        cmos_write(0x0b, 0x02);
                   19513:        cmos_write(0x0d, 0x80);
1.1       root     19514: }
                   19515: 
1.1.1.8   root     19516: void cmos_write(int addr, UINT8 val)
1.1       root     19517: {
1.1.1.8   root     19518:        cmos[addr & 0x7f] = val;
                   19519: }
                   19520: 
                   19521: #define CMOS_GET_TIME() { \
                   19522:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   19523:        if(prev_sec != cur_sec) { \
                   19524:                GetLocalTime(&time); \
                   19525:                prev_sec = cur_sec; \
                   19526:        } \
1.1       root     19527: }
1.1.1.8   root     19528: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     19529: 
1.1.1.8   root     19530: UINT8 cmos_read(int addr)
1.1       root     19531: {
1.1.1.8   root     19532:        static SYSTEMTIME time;
                   19533:        static UINT32 prev_sec = 0;
1.1       root     19534:        
1.1.1.8   root     19535:        switch(addr & 0x7f) {
                   19536:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   19537:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   19538:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   19539:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   19540:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   19541:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   19542:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   19543: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   19544:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   19545:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   19546:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   19547:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   19548:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   19549:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   19550:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   19551:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     19552:        }
1.1.1.8   root     19553:        return(cmos[addr & 0x7f]);
1.1       root     19554: }
                   19555: 
1.1.1.7   root     19556: // kbd (a20)
                   19557: 
                   19558: void kbd_init()
                   19559: {
1.1.1.8   root     19560:        kbd_data = kbd_command = 0;
1.1.1.7   root     19561:        kbd_status = 0x18;
                   19562: }
                   19563: 
                   19564: UINT8 kbd_read_data()
                   19565: {
1.1.1.8   root     19566:        kbd_status &= ~1;
1.1.1.7   root     19567:        return(kbd_data);
                   19568: }
                   19569: 
                   19570: void kbd_write_data(UINT8 val)
                   19571: {
                   19572:        switch(kbd_command) {
                   19573:        case 0xd1:
                   19574:                i386_set_a20_line((val >> 1) & 1);
                   19575:                break;
                   19576:        }
                   19577:        kbd_command = 0;
1.1.1.8   root     19578:        kbd_status &= ~8;
1.1.1.7   root     19579: }
                   19580: 
                   19581: UINT8 kbd_read_status()
                   19582: {
                   19583:        return(kbd_status);
                   19584: }
                   19585: 
                   19586: void kbd_write_command(UINT8 val)
                   19587: {
                   19588:        switch(val) {
                   19589:        case 0xd0:
                   19590:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     19591:                kbd_status |= 1;
1.1.1.7   root     19592:                break;
                   19593:        case 0xdd:
                   19594:                i386_set_a20_line(0);
                   19595:                break;
                   19596:        case 0xdf:
                   19597:                i386_set_a20_line(1);
                   19598:                break;
1.1.1.26  root     19599:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   19600:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     19601:                if(!(val & 1)) {
1.1.1.8   root     19602:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     19603:                                // reset pic
                   19604:                                pic_init();
                   19605:                                pic[0].irr = pic[1].irr = 0x00;
                   19606:                                pic[0].imr = pic[1].imr = 0xff;
                   19607:                        }
                   19608:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     19609:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   19610:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   19611:                        i386_jmp_far(selector, address);
1.1.1.7   root     19612:                }
                   19613:                i386_set_a20_line((val >> 1) & 1);
                   19614:                break;
                   19615:        }
                   19616:        kbd_command = val;
1.1.1.8   root     19617:        kbd_status |= 8;
1.1.1.7   root     19618: }
                   19619: 
1.1.1.9   root     19620: // vga
                   19621: 
                   19622: UINT8 vga_read_status()
                   19623: {
                   19624:        // 60hz
                   19625:        static const int period[3] = {16, 17, 17};
                   19626:        static int index = 0;
                   19627:        UINT32 time = timeGetTime() % period[index];
                   19628:        
                   19629:        index = (index + 1) % 3;
1.1.1.14  root     19630:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     19631: }
                   19632: 
1.1       root     19633: // i/o bus
                   19634: 
1.1.1.29  root     19635: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   19636: //#define SW1US_PATCH
                   19637: 
1.1.1.25  root     19638: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     19639: #ifdef USE_DEBUGGER
1.1.1.25  root     19640: {
1.1.1.33  root     19641:        if(now_debugging) {
                   19642:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   19643:                        if(in_break_point.table[i].status == 1) {
                   19644:                                if(addr == in_break_point.table[i].addr) {
                   19645:                                        in_break_point.hit = i + 1;
                   19646:                                        now_suspended = true;
                   19647:                                        break;
                   19648:                                }
                   19649:                        }
                   19650:                }
1.1.1.25  root     19651:        }
1.1.1.33  root     19652:        return(debugger_read_io_byte(addr));
1.1.1.25  root     19653: }
1.1.1.33  root     19654: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     19655: #endif
1.1       root     19656: {
1.1.1.33  root     19657:        UINT8 val = 0xff;
                   19658:        
1.1       root     19659:        switch(addr) {
1.1.1.29  root     19660: #ifdef SW1US_PATCH
                   19661:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   19662:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     19663:                val = sio_read(0, addr - 1);
                   19664:                break;
1.1.1.29  root     19665: #else
1.1.1.25  root     19666:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   19667:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     19668:                val = dma_read(0, addr);
                   19669:                break;
1.1.1.29  root     19670: #endif
1.1.1.25  root     19671:        case 0x20: case 0x21:
1.1.1.33  root     19672:                val = pic_read(0, addr);
                   19673:                break;
1.1.1.25  root     19674:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     19675:                val = pit_read(addr & 0x03);
                   19676:                break;
1.1.1.7   root     19677:        case 0x60:
1.1.1.33  root     19678:                val = kbd_read_data();
                   19679:                break;
1.1.1.9   root     19680:        case 0x61:
1.1.1.33  root     19681:                val = system_port;
                   19682:                break;
1.1.1.7   root     19683:        case 0x64:
1.1.1.33  root     19684:                val = kbd_read_status();
                   19685:                break;
1.1       root     19686:        case 0x71:
1.1.1.33  root     19687:                val = cmos_read(cmos_addr);
                   19688:                break;
1.1.1.25  root     19689:        case 0x81:
1.1.1.33  root     19690:                val = dma_page_read(0, 2);
                   19691:                break;
1.1.1.25  root     19692:        case 0x82:
1.1.1.33  root     19693:                val = dma_page_read(0, 3);
                   19694:                break;
1.1.1.25  root     19695:        case 0x83:
1.1.1.33  root     19696:                val = dma_page_read(0, 1);
                   19697:                break;
1.1.1.25  root     19698:        case 0x87:
1.1.1.33  root     19699:                val = dma_page_read(0, 0);
                   19700:                break;
1.1.1.25  root     19701:        case 0x89:
1.1.1.33  root     19702:                val = dma_page_read(1, 2);
                   19703:                break;
1.1.1.25  root     19704:        case 0x8a:
1.1.1.33  root     19705:                val = dma_page_read(1, 3);
                   19706:                break;
1.1.1.25  root     19707:        case 0x8b:
1.1.1.33  root     19708:                val = dma_page_read(1, 1);
                   19709:                break;
1.1.1.25  root     19710:        case 0x8f:
1.1.1.33  root     19711:                val = dma_page_read(1, 0);
                   19712:                break;
1.1       root     19713:        case 0x92:
1.1.1.33  root     19714:                val = (m_a20_mask >> 19) & 2;
                   19715:                break;
1.1.1.25  root     19716:        case 0xa0: case 0xa1:
1.1.1.33  root     19717:                val = pic_read(1, addr);
                   19718:                break;
1.1.1.25  root     19719:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   19720:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     19721:                val = dma_read(1, (addr - 0xc0) >> 1);
                   19722:                break;
1.1.1.37  root     19723:        case 0x278: case 0x279: case 0x27a:
                   19724:                val = pio_read(1, addr);
                   19725:                break;
1.1.1.29  root     19726:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     19727:                val = sio_read(3, addr);
                   19728:                break;
1.1.1.25  root     19729:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     19730:                val = sio_read(1, addr);
                   19731:                break;
1.1.1.25  root     19732:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     19733:                val = pio_read(0, addr);
                   19734:                break;
1.1.1.25  root     19735:        case 0x3ba: case 0x3da:
1.1.1.33  root     19736:                val = vga_read_status();
                   19737:                break;
1.1.1.37  root     19738:        case 0x3bc: case 0x3bd: case 0x3be:
                   19739:                val = pio_read(2, addr);
                   19740:                break;
1.1.1.29  root     19741:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     19742:                val = sio_read(2, addr);
                   19743:                break;
1.1.1.25  root     19744:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     19745:                val = sio_read(0, addr);
                   19746:                break;
1.1       root     19747:        default:
1.1.1.33  root     19748: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     19749:                break;
                   19750:        }
1.1.1.33  root     19751: #ifdef ENABLE_DEBUG_IOPORT
                   19752:        if(fp_debug_log != NULL) {
                   19753:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   19754:        }
                   19755: #endif
                   19756:        return(val);
1.1       root     19757: }
                   19758: 
                   19759: UINT16 read_io_word(offs_t addr)
                   19760: {
                   19761:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   19762: }
                   19763: 
1.1.1.33  root     19764: #ifdef USE_DEBUGGER
                   19765: UINT16 debugger_read_io_word(offs_t addr)
                   19766: {
                   19767:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   19768: }
                   19769: #endif
                   19770: 
1.1       root     19771: UINT32 read_io_dword(offs_t addr)
                   19772: {
                   19773:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   19774: }
                   19775: 
1.1.1.33  root     19776: #ifdef USE_DEBUGGER
                   19777: UINT32 debugger_read_io_dword(offs_t addr)
                   19778: {
                   19779:        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));
                   19780: }
                   19781: #endif
                   19782: 
1.1       root     19783: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     19784: #ifdef USE_DEBUGGER
                   19785: {
                   19786:        if(now_debugging) {
                   19787:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   19788:                        if(out_break_point.table[i].status == 1) {
                   19789:                                if(addr == out_break_point.table[i].addr) {
                   19790:                                        out_break_point.hit = i + 1;
                   19791:                                        now_suspended = true;
                   19792:                                        break;
                   19793:                                }
                   19794:                        }
                   19795:                }
                   19796:        }
                   19797:        debugger_write_io_byte(addr, val);
                   19798: }
                   19799: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   19800: #endif
1.1       root     19801: {
1.1.1.25  root     19802: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     19803:        if(fp_debug_log != NULL) {
1.1.1.43  root     19804: #ifdef USE_SERVICE_THREAD
                   19805:                if(addr != 0xf7)
                   19806: #endif
1.1.1.33  root     19807:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     19808:        }
                   19809: #endif
1.1       root     19810:        switch(addr) {
1.1.1.29  root     19811: #ifdef SW1US_PATCH
                   19812:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   19813:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   19814:                sio_write(0, addr - 1, val);
                   19815:                break;
                   19816: #else
1.1.1.25  root     19817:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   19818:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   19819:                dma_write(0, addr, val);
                   19820:                break;
1.1.1.29  root     19821: #endif
1.1.1.25  root     19822:        case 0x20: case 0x21:
1.1       root     19823:                pic_write(0, addr, val);
                   19824:                break;
1.1.1.25  root     19825:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     19826:                pit_write(addr & 0x03, val);
                   19827:                break;
1.1.1.7   root     19828:        case 0x60:
                   19829:                kbd_write_data(val);
                   19830:                break;
1.1.1.9   root     19831:        case 0x61:
                   19832:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   19833:                        // beep on
                   19834: //                     MessageBeep(-1);
                   19835:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   19836:                        // beep off
                   19837:                }
                   19838:                system_port = val;
                   19839:                break;
1.1       root     19840:        case 0x64:
1.1.1.7   root     19841:                kbd_write_command(val);
1.1       root     19842:                break;
                   19843:        case 0x70:
                   19844:                cmos_addr = val;
                   19845:                break;
                   19846:        case 0x71:
1.1.1.8   root     19847:                cmos_write(cmos_addr, val);
1.1       root     19848:                break;
1.1.1.25  root     19849:        case 0x81:
                   19850:                dma_page_write(0, 2, val);
                   19851:        case 0x82:
                   19852:                dma_page_write(0, 3, val);
                   19853:        case 0x83:
                   19854:                dma_page_write(0, 1, val);
                   19855:        case 0x87:
                   19856:                dma_page_write(0, 0, val);
                   19857:        case 0x89:
                   19858:                dma_page_write(1, 2, val);
                   19859:        case 0x8a:
                   19860:                dma_page_write(1, 3, val);
                   19861:        case 0x8b:
                   19862:                dma_page_write(1, 1, val);
                   19863:        case 0x8f:
                   19864:                dma_page_write(1, 0, val);
1.1       root     19865:        case 0x92:
1.1.1.7   root     19866:                i386_set_a20_line((val >> 1) & 1);
1.1       root     19867:                break;
1.1.1.25  root     19868:        case 0xa0: case 0xa1:
1.1       root     19869:                pic_write(1, addr, val);
                   19870:                break;
1.1.1.25  root     19871:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   19872:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     19873:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     19874:                break;
1.1.1.35  root     19875: #ifdef USE_SERVICE_THREAD
                   19876:        case 0xf7:
                   19877:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     19878:                if(in_service && cursor_moved) {
                   19879:                        // update cursor position before service is done
                   19880:                        pcbios_update_cursor_position();
                   19881:                        cursor_moved = false;
                   19882:                }
1.1.1.35  root     19883:                finish_service_loop();
                   19884:                break;
                   19885: #endif
1.1.1.37  root     19886:        case 0x278: case 0x279: case 0x27a:
                   19887:                pio_write(1, addr, val);
                   19888:                break;
1.1.1.29  root     19889:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   19890:                sio_write(3, addr, val);
                   19891:                break;
1.1.1.25  root     19892:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   19893:                sio_write(1, addr, val);
                   19894:                break;
                   19895:        case 0x378: case 0x379: case 0x37a:
                   19896:                pio_write(0, addr, val);
                   19897:                break;
1.1.1.37  root     19898:        case 0x3bc: case 0x3bd: case 0x3be:
                   19899:                pio_write(2, addr, val);
                   19900:                break;
1.1.1.29  root     19901:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   19902:                sio_write(2, addr, val);
                   19903:                break;
1.1.1.25  root     19904:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   19905:                sio_write(0, addr, val);
                   19906:                break;
1.1       root     19907:        default:
1.1.1.33  root     19908: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     19909:                break;
                   19910:        }
                   19911: }
                   19912: 
                   19913: void write_io_word(offs_t addr, UINT16 val)
                   19914: {
                   19915:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   19916:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   19917: }
                   19918: 
1.1.1.33  root     19919: #ifdef USE_DEBUGGER
                   19920: void debugger_write_io_word(offs_t addr, UINT16 val)
                   19921: {
                   19922:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   19923:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   19924: }
                   19925: #endif
                   19926: 
1.1       root     19927: void write_io_dword(offs_t addr, UINT32 val)
                   19928: {
                   19929:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   19930:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   19931:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   19932:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   19933: }
1.1.1.33  root     19934: 
                   19935: #ifdef USE_DEBUGGER
                   19936: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   19937: {
                   19938:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   19939:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   19940:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   19941:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   19942: }
                   19943: #endif

unix.superglobalmegacorp.com

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