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

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.35  root      775: #ifdef USE_SERVICE_THREAD
1.1.1.24  root      776: void i386_call_far(UINT16 selector, UINT32 address)
                    777: {
                    778: #if defined(HAS_I386)
                    779:        if(PROTECTED_MODE && !V8086_MODE) {
                    780:                i386_protected_mode_call(selector, address, 1, m_operand_size);
                    781:        } else {
                    782:                PUSH16(SREG(CS));
                    783:                PUSH16(m_eip);
                    784:                SREG(CS) = selector;
                    785:                m_performed_intersegment_jump = 1;
                    786:                i386_load_segment_descriptor(CS);
                    787:                m_eip = address;
                    788:                CHANGE_PC(m_eip);
                    789:        }
                    790: #else
                    791:        UINT16 ip = m_pc - SREG_BASE(CS);
                    792:        UINT16 cs = SREG(CS);
                    793: #if defined(HAS_I286)
                    794:        i80286_code_descriptor(selector, address, 2);
                    795: #else
                    796:        SREG(CS) = selector;
                    797:        i386_load_segment_descriptor(CS);
                    798:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    799: #endif
                    800:        PUSH(cs);
                    801:        PUSH(ip);
                    802:        CHANGE_PC(m_pc);
                    803: #endif
                    804: }
1.1.1.35  root      805: #endif
1.1.1.24  root      806: 
1.1.1.29  root      807: UINT16 i386_read_stack()
                    808: {
                    809: #if defined(HAS_I386)
                    810:        UINT32 ea, new_esp;
                    811:        if( STACK_32BIT ) {
                    812:                new_esp = REG32(ESP) + 2;
                    813:                ea = i386_translate(SS, new_esp - 2, 0);
                    814:        } else {
                    815:                new_esp = REG16(SP) + 2;
                    816:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    817:        }
                    818:        return READ16(ea);
                    819: #else
                    820:        UINT16 sp = m_regs.w[SP] + 2;
                    821:        return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
                    822: #endif
                    823: }
                    824: 
1.1       root      825: /* ----------------------------------------------------------------------------
1.1.1.33  root      826:        debugger
                    827: ---------------------------------------------------------------------------- */
                    828: 
                    829: #ifdef USE_DEBUGGER
                    830: #define TELNET_BLUE      0x0004 // text color contains blue.
                    831: #define TELNET_GREEN     0x0002 // text color contains green.
                    832: #define TELNET_RED       0x0001 // text color contains red.
                    833: #define TELNET_INTENSITY 0x0008 // text color is intensified.
                    834: 
                    835: int svr_socket = 0;
                    836: int cli_socket = 0;
                    837: 
                    838: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error);
                    839: 
                    840: void debugger_init()
                    841: {
                    842:        now_debugging = false;
                    843:        now_going = false;
                    844:        now_suspended = false;
                    845:        force_suspend = false;
                    846:        
                    847:        memset(&break_point, 0, sizeof(break_point_t));
                    848:        memset(&rd_break_point, 0, sizeof(break_point_t));
                    849:        memset(&wr_break_point, 0, sizeof(break_point_t));
                    850:        memset(&in_break_point, 0, sizeof(break_point_t));
                    851:        memset(&out_break_point, 0, sizeof(break_point_t));
                    852:        memset(&int_break_point, 0, sizeof(int_break_point_t));
                    853: }
                    854: 
1.1.1.45! root      855: void telnet_send(const char *string)
1.1.1.33  root      856: {
                    857:        char buffer[8192], *ptr;
                    858:        strcpy(buffer, string);
                    859:        while((ptr = strstr(buffer, "\n")) != NULL) {
                    860:                char tmp[8192];
                    861:                *ptr = '\0';
                    862:                sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
                    863:                strcpy(buffer, tmp);
                    864:        }
                    865:        
                    866:        int len = strlen(buffer), res;
                    867:        ptr = buffer;
                    868:        while(len > 0) {
                    869:                if((res = send(cli_socket, ptr, len, 0)) > 0) {
                    870:                        len -= res;
                    871:                        ptr += res;
                    872:                }
                    873:        }
                    874: }
                    875: 
                    876: void telnet_command(const char *format, ...)
                    877: {
                    878:        char buffer[1024];
                    879:        va_list ap;
                    880:        va_start(ap, format);
                    881:        vsprintf(buffer, format, ap);
                    882:        va_end(ap);
                    883:        
                    884:        telnet_send(buffer);
                    885: }
                    886: 
                    887: void telnet_printf(const char *format, ...)
                    888: {
                    889:        char buffer[1024];
                    890:        va_list ap;
                    891:        va_start(ap, format);
                    892:        vsprintf(buffer, format, ap);
                    893:        va_end(ap);
                    894:        
                    895:        if(fp_debugger != NULL) {
                    896:                fprintf(fp_debugger, "%s", buffer);
                    897:        }
                    898:        telnet_send(buffer);
                    899: }
                    900: 
                    901: bool telnet_gets(char *str, int n)
                    902: {
                    903:        char buffer[1024];
                    904:        int ptr = 0;
                    905:        
                    906:        telnet_command("\033[12l"); // local echo on
                    907:        telnet_command("\033[2l");  // key unlock
                    908:        
                    909:        while(!m_halted) {
                    910:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    911:                
                    912:                if(len > 0 && buffer[0] != 0xff) {
                    913:                        for(int i = 0; i < len; i++) {
                    914:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                    915:                                        str[ptr] = 0;
                    916:                                        telnet_command("\033[2h");  // key lock
                    917:                                        telnet_command("\033[12h"); // local echo off
                    918:                                        return(!m_halted);
                    919:                                } else if(buffer[i] == 0x08) {
                    920:                                        if(ptr > 0) {
                    921:                                                telnet_command("\033[0K"); // erase from cursor position
                    922:                                                ptr--;
                    923:                                        } else {
                    924:                                                telnet_command("\033[1C"); // move cursor forward
                    925:                                        }
                    926:                                } else if(ptr < n - 1) {
1.1.1.37  root      927:                                        if(buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
1.1.1.33  root      928:                                                str[ptr++] = buffer[i];
                    929:                                        }
                    930:                                } else {
                    931:                                        telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
                    932:                                }
                    933:                        }
                    934:                } else if(len == -1) {
                    935:                        if(WSAGetLastError() != WSAEWOULDBLOCK) {
                    936:                                return(false);
                    937:                        }
                    938:                } else if(len == 0) {
                    939:                        return(false);
                    940:                }
                    941:                Sleep(10);
                    942:        }
                    943:        return(!m_halted);
                    944: }
                    945: 
                    946: bool telnet_kbhit()
                    947: {
                    948:        char buffer[1024];
                    949:        
                    950:        if(!m_halted) {
                    951:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    952:                
                    953:                if(len > 0) {
                    954:                        for(int i = 0; i < len; i++) {
                    955:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                    956:                                        return(true);
                    957:                                }
                    958:                        }
                    959:                } else if(len == 0) {
                    960:                        return(true); // disconnected
                    961:                }
                    962:        }
                    963:        return(false);
                    964: }
                    965: 
                    966: bool telnet_disconnected()
                    967: {
                    968:        char buffer[1024];
                    969:        int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    970:        
                    971:        if(len == 0) {
                    972:                return(true);
                    973:        } else if(len == -1) {
                    974:                if(WSAGetLastError() != WSAEWOULDBLOCK) {
                    975:                        return(true);
                    976:                }
                    977:        }
                    978:        return(false);
                    979: }
                    980: 
                    981: void telnet_set_color(int color)
                    982: {
                    983:        telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
                    984: }
                    985: 
                    986: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
                    987: {
                    988: //     UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
                    989:        UINT8 ops[16];
                    990:        for(int i = 0; i < 16; i++) {
                    991:                ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
                    992:        }
                    993:        UINT8 *oprom = ops;
                    994:        
                    995: #if defined(HAS_I386)
                    996:        if(m_operand_size) {
                    997:                return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
                    998:        } else
                    999: #endif
                   1000:        return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
                   1001: }
                   1002: 
                   1003: void debugger_regs_info(char *buffer)
                   1004: {
                   1005: #if defined(HAS_I386)
                   1006:        UINT32 flags = get_flags();
                   1007: #else
                   1008:        UINT32 flags = CompressFlags();
                   1009: #endif
                   1010: #if defined(HAS_I386)
                   1011:        if(m_operand_size) {
                   1012:                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",
                   1013:                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),
                   1014:                PROTECTED_MODE ? "PE" : "--",
                   1015:                (flags & 0x40000) ? 'A' : '-',
                   1016:                (flags & 0x20000) ? 'V' : '-',
                   1017:                (flags & 0x10000) ? 'R' : '-',
                   1018:                (flags & 0x04000) ? 'N' : '-',
                   1019:                (flags & 0x02000) ? '1' : '0',
                   1020:                (flags & 0x01000) ? '1' : '0',
                   1021:                (flags & 0x00800) ? 'O' : '-',
                   1022:                (flags & 0x00400) ? 'D' : '-',
                   1023:                (flags & 0x00200) ? 'I' : '-',
                   1024:                (flags & 0x00100) ? 'T' : '-',
                   1025:                (flags & 0x00080) ? 'S' : '-',
                   1026:                (flags & 0x00040) ? 'Z' : '-',
                   1027:                (flags & 0x00010) ? 'A' : '-',
                   1028:                (flags & 0x00004) ? 'P' : '-',
                   1029:                (flags & 0x00001) ? 'C' : '-');
                   1030:        } else {
                   1031: #endif
                   1032:                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",
                   1033:                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),
                   1034: #if defined(HAS_I386)
                   1035:                PROTECTED_MODE ? "PE" : "--",
                   1036: #else
                   1037:                "--",
                   1038: #endif
                   1039:                (flags & 0x40000) ? 'A' : '-',
                   1040:                (flags & 0x20000) ? 'V' : '-',
                   1041:                (flags & 0x10000) ? 'R' : '-',
                   1042:                (flags & 0x04000) ? 'N' : '-',
                   1043:                (flags & 0x02000) ? '1' : '0',
                   1044:                (flags & 0x01000) ? '1' : '0',
                   1045:                (flags & 0x00800) ? 'O' : '-',
                   1046:                (flags & 0x00400) ? 'D' : '-',
                   1047:                (flags & 0x00200) ? 'I' : '-',
                   1048:                (flags & 0x00100) ? 'T' : '-',
                   1049:                (flags & 0x00080) ? 'S' : '-',
                   1050:                (flags & 0x00040) ? 'Z' : '-',
                   1051:                (flags & 0x00010) ? 'A' : '-',
                   1052:                (flags & 0x00004) ? 'P' : '-',
                   1053:                (flags & 0x00001) ? 'C' : '-');
                   1054: #if defined(HAS_I386)
                   1055:        }
                   1056: #endif
                   1057: }
                   1058: 
                   1059: void debugger_process_info(char *buffer)
                   1060: {
                   1061:        UINT16 psp_seg = current_psp;
                   1062:        process_t *process;
                   1063:        bool check[0x10000] = {0};
                   1064:        
                   1065:        buffer[0] = '\0';
                   1066:        
                   1067:        while(!check[psp_seg] && (process  = msdos_process_info_get(psp_seg, false)) != NULL) {
                   1068:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1069:                char *file = process->module_path, *s;
                   1070:                char tmp[8192];
                   1071:                
                   1072:                while((s = strstr(file, "\\")) != NULL) {
                   1073:                        file = s + 1;
                   1074:                }
                   1075:                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));
                   1076:                strcat(tmp, buffer);
                   1077:                strcpy(buffer, tmp);
                   1078:                
                   1079:                check[psp_seg] = true;
                   1080:                psp_seg = psp->parent_psp;
                   1081:        }
                   1082: }
                   1083: 
                   1084: UINT32 debugger_get_val(const char *str)
                   1085: {
                   1086:        char tmp[1024];
                   1087:        
                   1088:        if(str == NULL || strlen(str) == 0) {
                   1089:                return(0);
                   1090:        }
                   1091:        strcpy(tmp, str);
                   1092:        
                   1093:        if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
                   1094:                // ank
                   1095:                return(tmp[1] & 0xff);
                   1096:        } else if(tmp[0] == '%') {
                   1097:                // decimal
                   1098:                return(strtoul(tmp + 1, NULL, 10));
                   1099:        }
                   1100:        return(strtoul(tmp, NULL, 16));
                   1101: }
                   1102: 
                   1103: UINT32 debugger_get_seg(const char *str, UINT32 val)
                   1104: {
                   1105:        char tmp[1024], *s;
                   1106:        
                   1107:        if(str == NULL || strlen(str) == 0) {
                   1108:                return(val);
                   1109:        }
                   1110:        strcpy(tmp, str);
                   1111:        
                   1112:        if((s = strstr(tmp, ":")) != NULL) {
                   1113:                // 0000:0000
                   1114:                *s = '\0';
                   1115:                return(debugger_get_val(tmp));
                   1116:        }
                   1117:        return(val);
                   1118: }
                   1119: 
                   1120: UINT32 debugger_get_ofs(const char *str)
                   1121: {
                   1122:        char tmp[1024], *s;
                   1123:        
                   1124:        if(str == NULL || strlen(str) == 0) {
                   1125:                return(0);
                   1126:        }
                   1127:        strcpy(tmp, str);
                   1128:        
                   1129:        if((s = strstr(tmp, ":")) != NULL) {
                   1130:                // 0000:0000
                   1131:                return(debugger_get_val(s + 1));
                   1132:        }
                   1133:        return(debugger_get_val(tmp));
                   1134: }
                   1135: 
                   1136: void debugger_main()
                   1137: {
                   1138:        telnet_command("\033[20h"); // cr-lf
                   1139:        
                   1140:        force_suspend = true;
                   1141:        now_going = false;
                   1142:        now_debugging = true;
                   1143:        Sleep(100);
                   1144:        
                   1145:        if(!m_halted && !now_suspended) {
                   1146:                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1147:                telnet_printf("waiting until cpu is suspended...\n");
                   1148:        }
                   1149:        while(!m_halted && !now_suspended) {
                   1150:                if(telnet_disconnected()) {
                   1151:                        break;
                   1152:                }
                   1153:                Sleep(10);
                   1154:        }
                   1155:        
                   1156:        char buffer[8192];
                   1157:        
                   1158:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1159:        debugger_process_info(buffer);
                   1160:        telnet_printf("%s", buffer);
                   1161:        debugger_regs_info(buffer);
                   1162:        telnet_printf("%s", buffer);
                   1163:        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1164:        telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
                   1165:        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1166:        debugger_dasm(buffer, SREG(CS), m_eip);
                   1167:        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1168:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1169:        
                   1170:        #define MAX_COMMAND_LEN 64
                   1171:        
                   1172:        char command[MAX_COMMAND_LEN + 1];
                   1173:        char prev_command[MAX_COMMAND_LEN + 1] = {0};
                   1174:        
                   1175:        UINT32 data_seg = SREG(DS);
                   1176:        UINT32 data_ofs = 0;
                   1177:        UINT32 dasm_seg = SREG(CS);
                   1178:        UINT32 dasm_ofs = m_eip;
                   1179:        
                   1180:        while(!m_halted) {
                   1181:                telnet_printf("- ");
                   1182:                command[0] = '\0';
                   1183:                
                   1184:                if(fi_debugger != NULL) {
                   1185:                        while(command[0] == '\0') {
                   1186:                                if(fgets(command, sizeof(command), fi_debugger) == NULL) {
                   1187:                                        break;
                   1188:                                }
                   1189:                                while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
                   1190:                                        command[strlen(command) - 1] = '\0';
                   1191:                                }
                   1192:                        }
                   1193:                        if(command[0] != '\0') {
                   1194:                                telnet_command("%s\n", command);
                   1195:                        }
                   1196:                }
                   1197:                if(command[0] == '\0') {
                   1198:                        if(!telnet_gets(command, sizeof(command))) {
                   1199:                                break;
                   1200:                        }
                   1201:                }
                   1202:                if(command[0] == '\0') {
                   1203:                        strcpy(command, prev_command);
                   1204:                } else {
                   1205:                        strcpy(prev_command, command);
                   1206:                }
                   1207:                if(fp_debugger != NULL) {
                   1208:                        fprintf(fp_debugger, "%s\n", command);
                   1209:                }
                   1210:                
                   1211:                if(!m_halted && command[0] != 0) {
                   1212:                        char *params[32], *token = NULL;
                   1213:                        int num = 0;
                   1214:                        
                   1215:                        if((token = strtok(command, " ")) != NULL) {
                   1216:                                params[num++] = token;
                   1217:                                while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
                   1218:                                        params[num++] = token;
                   1219:                                }
                   1220:                        }
                   1221:                        if(stricmp(params[0], "D") == 0) {
                   1222:                                if(num <= 3) {
                   1223:                                        if(num >= 2) {
                   1224:                                                data_seg = debugger_get_seg(params[1], data_seg);
                   1225:                                                data_ofs = debugger_get_ofs(params[1]);
                   1226:                                        }
                   1227:                                        UINT32 end_seg = data_seg;
                   1228:                                        UINT32 end_ofs = data_ofs + 8 * 16 - 1;
                   1229:                                        if(num == 3) {
                   1230:                                                end_seg = debugger_get_seg(params[2], data_seg);
                   1231:                                                end_ofs = debugger_get_ofs(params[2]);
                   1232:                                        }
                   1233:                                        UINT64 start_addr = (data_seg << 4) + data_ofs;
                   1234:                                        UINT64 end_addr = (end_seg << 4) + end_ofs;
1.1.1.37  root     1235: //                                     bool is_sjis = false;
1.1.1.33  root     1236:                                        
                   1237:                                        for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
                   1238:                                                if((addr & 0x0f) == 0) {
                   1239:                                                        if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
                   1240:                                                                data_seg += 0x1000;
                   1241:                                                                data_ofs -= 0x10000;
                   1242:                                                        }
                   1243:                                                        telnet_printf("%06X:%04X ", data_seg, data_ofs);
                   1244:                                                        memset(buffer, 0, sizeof(buffer));
                   1245:                                                }
                   1246:                                                if(addr < start_addr || addr > end_addr) {
                   1247:                                                        telnet_printf("   ");
                   1248:                                                        buffer[addr & 0x0f] = ' ';
                   1249:                                                } else {
                   1250:                                                        UINT8 data = debugger_read_byte(addr & ADDR_MASK);
                   1251:                                                        telnet_printf(" %02X", data);
1.1.1.37  root     1252: //                                                     if(is_sjis) {
1.1.1.33  root     1253: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1254: //                                                             is_sjis = false;
1.1.1.33  root     1255: //                                                     } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
                   1256: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1257: //                                                             is_sjis = true;
1.1.1.33  root     1258: //                                                     } else
                   1259:                                                        if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
                   1260:                                                                buffer[addr & 0x0f] = data;
                   1261:                                                        } else {
                   1262:                                                                buffer[addr & 0x0f] = '.';
                   1263:                                                        }
                   1264:                                                }
                   1265:                                                if((addr & 0x0f) == 0x0f) {
                   1266:                                                        telnet_printf("  %s\n", buffer);
                   1267:                                                }
                   1268:                                        }
                   1269:                                        if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
                   1270:                                                data_seg += 0x1000;
                   1271:                                                data_ofs -= 0x10000;
                   1272:                                        }
                   1273:                                        prev_command[1] = '\0'; // remove parameters to dump continuously
                   1274:                                } else {
                   1275:                                        telnet_printf("invalid parameter number\n");
                   1276:                                }
                   1277:                        } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 0) {
                   1278:                                if(num >= 3) {
                   1279:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1280:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1281:                                        for(int i = 2, j = 0; i < num; i++, j++) {
                   1282:                                                debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
                   1283:                                        }
                   1284:                                } else {
                   1285:                                        telnet_printf("invalid parameter number\n");
                   1286:                                }
                   1287:                        } else if(stricmp(params[0], "EW") == 0) {
                   1288:                                if(num >= 3) {
                   1289:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1290:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1291:                                        for(int i = 2, j = 0; i < num; i++, j += 2) {
                   1292:                                                debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
                   1293:                                        }
                   1294:                                } else {
                   1295:                                        telnet_printf("invalid parameter number\n");
                   1296:                                }
                   1297:                        } else if(stricmp(params[0], "ED") == 0) {
                   1298:                                if(num >= 3) {
                   1299:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1300:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1301:                                        for(int i = 2, j = 0; i < num; i++, j += 4) {
                   1302:                                                debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
                   1303:                                        }
                   1304:                                } else {
                   1305:                                        telnet_printf("invalid parameter number\n");
                   1306:                                }
                   1307:                        } else if(stricmp(params[0], "EA") == 0) {
                   1308:                                if(num >= 3) {
                   1309:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1310:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1311:                                        strcpy(buffer, prev_command);
                   1312:                                        if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
                   1313:                                                int len = strlen(token);
                   1314:                                                for(int i = 0; i < len; i++) {
                   1315:                                                        debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
                   1316:                                                }
                   1317:                                        } else {
                   1318:                                                telnet_printf("invalid parameter\n");
                   1319:                                        }
                   1320:                                } else {
                   1321:                                        telnet_printf("invalid parameter number\n");
                   1322:                                }
                   1323:                        } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
                   1324:                                if(num == 2) {
                   1325:                                        telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
                   1326:                                } else {
                   1327:                                        telnet_printf("invalid parameter number\n");
                   1328:                                }
                   1329:                        } else if(stricmp(params[0], "IW") == 0) {
                   1330:                                if(num == 2) {
                   1331:                                        telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
                   1332:                                } else {
                   1333:                                        telnet_printf("invalid parameter number\n");
                   1334:                                }
                   1335:                        } else if(stricmp(params[0], "ID") == 0) {
                   1336:                                if(num == 2) {
                   1337:                                        telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
                   1338:                                } else {
                   1339:                                        telnet_printf("invalid parameter number\n");
                   1340:                                }
                   1341:                        } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
                   1342:                                if(num == 3) {
                   1343:                                        debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
                   1344:                                } else {
                   1345:                                        telnet_printf("invalid parameter number\n");
                   1346:                                }
                   1347:                        } else if(stricmp(params[0], "OW") == 0) {
                   1348:                                if(num == 3) {
                   1349:                                        debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
                   1350:                                } else {
                   1351:                                        telnet_printf("invalid parameter number\n");
                   1352:                                }
                   1353:                        } else if(stricmp(params[0], "OD") == 0) {
                   1354:                                if(num == 3) {
                   1355:                                        debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
                   1356:                                } else {
                   1357:                                        telnet_printf("invalid parameter number\n");
                   1358:                                }
                   1359:                        } else if(stricmp(params[0], "R") == 0) {
                   1360:                                if(num == 1) {
                   1361:                                        debugger_regs_info(buffer);
                   1362:                                        telnet_printf("%s", buffer);
                   1363:                                } else if(num == 3) {
                   1364: #if defined(HAS_I386)
                   1365:                                        if(stricmp(params[1], "EAX") == 0) {
                   1366:                                                REG32(EAX) = debugger_get_val(params[2]);
                   1367:                                        } else if(stricmp(params[1], "EBX") == 0) {
                   1368:                                                REG32(EBX) = debugger_get_val(params[2]);
                   1369:                                        } else if(stricmp(params[1], "ECX") == 0) {
                   1370:                                                REG32(ECX) = debugger_get_val(params[2]);
                   1371:                                        } else if(stricmp(params[1], "EDX") == 0) {
                   1372:                                                REG32(EDX) = debugger_get_val(params[2]);
                   1373:                                        } else if(stricmp(params[1], "ESP") == 0) {
                   1374:                                                REG32(ESP) = debugger_get_val(params[2]);
                   1375:                                        } else if(stricmp(params[1], "EBP") == 0) {
                   1376:                                                REG32(EBP) = debugger_get_val(params[2]);
                   1377:                                        } else if(stricmp(params[1], "ESI") == 0) {
                   1378:                                                REG32(ESI) = debugger_get_val(params[2]);
                   1379:                                        } else if(stricmp(params[1], "EDI") == 0) {
                   1380:                                                REG32(EDI) = debugger_get_val(params[2]);
                   1381:                                        } else
                   1382: #endif
                   1383:                                        if(stricmp(params[1], "AX") == 0) {
                   1384:                                                REG16(AX) = debugger_get_val(params[2]);
                   1385:                                        } else if(stricmp(params[1], "BX") == 0) {
                   1386:                                                REG16(BX) = debugger_get_val(params[2]);
                   1387:                                        } else if(stricmp(params[1], "CX") == 0) {
                   1388:                                                REG16(CX) = debugger_get_val(params[2]);
                   1389:                                        } else if(stricmp(params[1], "DX") == 0) {
                   1390:                                                REG16(DX) = debugger_get_val(params[2]);
                   1391:                                        } else if(stricmp(params[1], "SP") == 0) {
                   1392:                                                REG16(SP) = debugger_get_val(params[2]);
                   1393:                                        } else if(stricmp(params[1], "BP") == 0) {
                   1394:                                                REG16(BP) = debugger_get_val(params[2]);
                   1395:                                        } else if(stricmp(params[1], "SI") == 0) {
                   1396:                                                REG16(SI) = debugger_get_val(params[2]);
                   1397:                                        } else if(stricmp(params[1], "DI") == 0) {
                   1398:                                                REG16(DI) = debugger_get_val(params[2]);
                   1399:                                        } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
                   1400: #if defined(HAS_I386)
                   1401:                                                if(m_operand_size) {
                   1402:                                                        m_eip = debugger_get_val(params[2]);
                   1403:                                                } else {
                   1404:                                                        m_eip = debugger_get_val(params[2]) & 0xffff;
                   1405:                                                }
                   1406:                                                CHANGE_PC(m_eip);
                   1407: #else
                   1408:                                                m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
                   1409:                                                CHANGE_PC(m_pc);
                   1410: #endif
                   1411:                                        } else if(stricmp(params[1], "AL") == 0) {
                   1412:                                                REG8(AL) = debugger_get_val(params[2]);
                   1413:                                        } else if(stricmp(params[1], "AH") == 0) {
                   1414:                                                REG8(AH) = debugger_get_val(params[2]);
                   1415:                                        } else if(stricmp(params[1], "BL") == 0) {
                   1416:                                                REG8(BL) = debugger_get_val(params[2]);
                   1417:                                        } else if(stricmp(params[1], "BH") == 0) {
                   1418:                                                REG8(BH) = debugger_get_val(params[2]);
                   1419:                                        } else if(stricmp(params[1], "CL") == 0) {
                   1420:                                                REG8(CL) = debugger_get_val(params[2]);
                   1421:                                        } else if(stricmp(params[1], "CH") == 0) {
                   1422:                                                REG8(CH) = debugger_get_val(params[2]);
                   1423:                                        } else if(stricmp(params[1], "DL") == 0) {
                   1424:                                                REG8(DL) = debugger_get_val(params[2]);
                   1425:                                        } else if(stricmp(params[1], "DH") == 0) {
                   1426:                                                REG8(DH) = debugger_get_val(params[2]);
                   1427:                                        } else {
                   1428:                                                telnet_printf("unknown register %s\n", params[1]);
                   1429:                                        }
                   1430:                                } else {
                   1431:                                        telnet_printf("invalid parameter number\n");
                   1432:                                }
                   1433:                        } else if(_tcsicmp(params[0], "S") == 0) {
                   1434:                                if(num >= 4) {
                   1435:                                        UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
                   1436:                                        UINT32 cur_ofs = debugger_get_ofs(params[1]);
                   1437:                                        UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
                   1438:                                        UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1439:                                        UINT8 list[32];
                   1440:                                        
                   1441:                                        for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1442:                                                list[j] = debugger_get_val(params[i]);
                   1443:                                        }
                   1444:                                        while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
                   1445:                                                bool found = true;
                   1446:                                                for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1447:                                                        if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
                   1448:                                                                found = false;
                   1449:                                                                break;
                   1450:                                                        }
                   1451:                                                }
                   1452:                                                if(found) {
                   1453:                                                        telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
                   1454:                                                }
                   1455:                                                if((cur_ofs += 1) > 0xffff) {
                   1456:                                                        cur_seg += 0x1000;
                   1457:                                                        cur_ofs -= 0x10000;
                   1458:                                                }
                   1459:                                        }
                   1460:                                } else {
                   1461:                                        telnet_printf("invalid parameter number\n");
                   1462:                                }
                   1463:                        } else if(stricmp(params[0], "U") == 0) {
                   1464:                                if(num <= 3) {
                   1465:                                        if(num >= 2) {
                   1466:                                                dasm_seg = debugger_get_seg(params[1], dasm_seg);
                   1467:                                                dasm_ofs = debugger_get_ofs(params[1]);
                   1468:                                        }
                   1469:                                        if(num == 3) {
                   1470:                                                UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
                   1471:                                                UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1472:                                                
                   1473:                                                while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
                   1474:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1475:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1476:                                                        for(int i = 0; i < len; i++) {
                   1477:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1478:                                                        }
                   1479:                                                        for(int i = len; i < 8; i++) {
                   1480:                                                                telnet_printf("  ");
                   1481:                                                        }
                   1482:                                                        telnet_printf("  %s\n", buffer);
                   1483:                                                        if((dasm_ofs += len) > 0xffff) {
                   1484:                                                                dasm_seg += 0x1000;
                   1485:                                                                dasm_ofs -= 0x10000;
                   1486:                                                        }
                   1487:                                                }
                   1488:                                        } else {
                   1489:                                                for(int i = 0; i < 16; i++) {
                   1490:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1491:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1492:                                                        for(int i = 0; i < len; i++) {
                   1493:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1494:                                                        }
                   1495:                                                        for(int i = len; i < 8; i++) {
                   1496:                                                                telnet_printf("  ");
                   1497:                                                        }
                   1498:                                                        telnet_printf("  %s\n", buffer);
                   1499:                                                        if((dasm_ofs += len) > 0xffff) {
                   1500:                                                                dasm_seg += 0x1000;
                   1501:                                                                dasm_ofs -= 0x10000;
                   1502:                                                        }
                   1503:                                                }
                   1504:                                        }
                   1505:                                        prev_command[1] = '\0'; // remove parameters to disassemble continuously
                   1506:                                } else {
                   1507:                                        telnet_printf("invalid parameter number\n");
                   1508:                                }
                   1509:                        } else if(stricmp(params[0], "H") == 0) {
                   1510:                                if(num == 3) {
                   1511:                                        UINT32 l = debugger_get_val(params[1]);
                   1512:                                        UINT32 r = debugger_get_val(params[2]);
                   1513:                                        telnet_printf("%08X  %08X\n", l + r, l - r);
                   1514:                                } else {
                   1515:                                        telnet_printf("invalid parameter number\n");
                   1516:                                }
                   1517:                        } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
                   1518:                                break_point_t *break_point_ptr;
                   1519:                                #define GET_BREAK_POINT_PTR() { \
                   1520:                                        if(params[0][0] == 'R') { \
                   1521:                                                break_point_ptr = &rd_break_point; \
                   1522:                                        } else if(params[0][0] == 'W') { \
                   1523:                                                break_point_ptr = &wr_break_point; \
                   1524:                                        } else if(params[0][0] == 'I') { \
                   1525:                                                break_point_ptr = &in_break_point; \
                   1526:                                        } else if(params[0][0] == 'O') { \
                   1527:                                                break_point_ptr = &out_break_point; \
                   1528:                                        } else { \
                   1529:                                                break_point_ptr = &break_point; \
                   1530:                                        } \
                   1531:                                }
                   1532:                                GET_BREAK_POINT_PTR();
                   1533:                                if(num == 2) {
                   1534:                                        UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1535:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1536:                                        bool found = false;
                   1537:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1538:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
                   1539:                                                        break_point_ptr->table[i].addr = (seg << 4) + ofs;
                   1540:                                                        break_point_ptr->table[i].seg = seg;
                   1541:                                                        break_point_ptr->table[i].ofs = ofs;
                   1542:                                                        break_point_ptr->table[i].status = 1;
                   1543:                                                        found = true;
                   1544:                                                }
                   1545:                                        }
                   1546:                                        if(!found) {
                   1547:                                                telnet_printf("too many break points\n");
                   1548:                                        }
                   1549:                                } else {
                   1550:                                        telnet_printf("invalid parameter number\n");
                   1551:                                }
                   1552:                        } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
                   1553:                                break_point_t *break_point_ptr;
                   1554:                                GET_BREAK_POINT_PTR();
                   1555:                                if(num == 2) {
                   1556:                                        UINT32 addr = debugger_get_val(params[1]);
                   1557:                                        bool found = false;
                   1558:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1559:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
                   1560:                                                        break_point_ptr->table[i].addr = addr;
                   1561:                                                        break_point_ptr->table[i].status = 1;
                   1562:                                                        found = true;
                   1563:                                                }
                   1564:                                        }
                   1565:                                        if(!found) {
                   1566:                                                telnet_printf("too many break points\n");
                   1567:                                        }
                   1568:                                } else {
                   1569:                                        telnet_printf("invalid parameter number\n");
                   1570:                                }
                   1571:                        } 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) {
                   1572:                                break_point_t *break_point_ptr;
                   1573:                                GET_BREAK_POINT_PTR();
                   1574:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1575:                                        memset(break_point_ptr, 0, sizeof(break_point_t));
                   1576:                                } else if(num >= 2) {
                   1577:                                        for(int i = 1; i < num; i++) {
                   1578:                                                int index = debugger_get_val(params[i]);
                   1579:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1580:                                                        telnet_printf("invalid index %x\n", index);
                   1581:                                                } else {
                   1582:                                                        break_point_ptr->table[index - 1].addr = 0;
                   1583:                                                        break_point_ptr->table[index - 1].seg = 0;
                   1584:                                                        break_point_ptr->table[index - 1].ofs = 0;
                   1585:                                                        break_point_ptr->table[index - 1].status = 0;
                   1586:                                                }
                   1587:                                        }
                   1588:                                } else {
                   1589:                                        telnet_printf("invalid parameter number\n");
                   1590:                                }
                   1591:                        } 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 ||
                   1592:                                  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) {
                   1593:                                break_point_t *break_point_ptr;
                   1594:                                GET_BREAK_POINT_PTR();
                   1595:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1596:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1597:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1598:                                                if(break_point_ptr->table[i].status != 0) {
                   1599:                                                        break_point_ptr->table[i].status = enabled ? 1 : -1;
                   1600:                                                }
                   1601:                                        }
                   1602:                                } else if(num >= 2) {
                   1603:                                        for(int i = 1; i < num; i++) {
                   1604:                                                int index = debugger_get_val(params[i]);
                   1605:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1606:                                                        telnet_printf("invalid index %x\n", index);
                   1607:                                                } else if(break_point_ptr->table[index - 1].status == 0) {
                   1608:                                                        telnet_printf("break point %x is null\n", index);
                   1609:                                                } else {
                   1610:                                                        break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
                   1611:                                                }
                   1612:                                        }
                   1613:                                } else {
                   1614:                                        telnet_printf("invalid parameter number\n");
                   1615:                                }
                   1616:                        } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
                   1617:                                break_point_t *break_point_ptr;
                   1618:                                GET_BREAK_POINT_PTR();
                   1619:                                if(num == 1) {
                   1620:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1621:                                                if(break_point_ptr->table[i].status) {
                   1622:                                                        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);
                   1623:                                                }
                   1624:                                        }
                   1625:                                } else {
                   1626:                                        telnet_printf("invalid parameter number\n");
                   1627:                                }
                   1628:                        } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
                   1629:                                break_point_t *break_point_ptr;
                   1630:                                GET_BREAK_POINT_PTR();
                   1631:                                if(num == 1) {
                   1632:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1633:                                                if(break_point_ptr->table[i].status) {
                   1634:                                                        telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
                   1635:                                                }
                   1636:                                        }
                   1637:                                } else {
                   1638:                                        telnet_printf("invalid parameter number\n");
                   1639:                                }
                   1640:                        } else if(stricmp(params[0], "INTBP") == 0) {
                   1641:                                if(num >= 2 && num <= 4) {
                   1642:                                        int int_num = debugger_get_val(params[1]);
                   1643:                                        UINT8 ah = 0, ah_registered = 0;
                   1644:                                        UINT8 al = 0, al_registered = 0;
                   1645:                                        if(num >= 3) {
                   1646:                                                ah = debugger_get_val(params[2]);
                   1647:                                                ah_registered = 1;
                   1648:                                        }
                   1649:                                        if(num == 4) {
                   1650:                                                al = debugger_get_val(params[3]);
                   1651:                                                al_registered = 1;
                   1652:                                        }
                   1653:                                        bool found = false;
                   1654:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1655:                                                if(int_break_point.table[i].status == 0 || (
                   1656:                                                   int_break_point.table[i].int_num == int_num &&
                   1657:                                                   int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
                   1658:                                                   int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
                   1659:                                                        int_break_point.table[i].int_num = int_num;
                   1660:                                                        int_break_point.table[i].ah = ah;
                   1661:                                                        int_break_point.table[i].ah_registered = ah_registered;
                   1662:                                                        int_break_point.table[i].al = al;
                   1663:                                                        int_break_point.table[i].al_registered = al_registered;
                   1664:                                                        int_break_point.table[i].status = 1;
                   1665:                                                        found = true;
                   1666:                                                }
                   1667:                                        }
                   1668:                                        if(!found) {
                   1669:                                                telnet_printf("too many break points\n");
                   1670:                                        }
                   1671:                                } else {
                   1672:                                        telnet_printf("invalid parameter number\n");
                   1673:                                }
                   1674:                        } else if(stricmp(params[0], "INTBC") == 0) {
                   1675:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1676:                                        memset(&int_break_point, 0, sizeof(int_break_point_t));
                   1677:                                } else if(num >= 2) {
                   1678:                                        for(int i = 1; i < num; i++) {
                   1679:                                                int index = debugger_get_val(params[i]);
                   1680:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1681:                                                        telnet_printf("invalid index %x\n", index);
                   1682:                                                } else {
                   1683:                                                        int_break_point.table[index - 1].int_num = 0;
                   1684:                                                        int_break_point.table[index - 1].ah = 0;
                   1685:                                                        int_break_point.table[index - 1].ah_registered = 0;
                   1686:                                                        int_break_point.table[index - 1].al = 0;
                   1687:                                                        int_break_point.table[index - 1].al_registered = 0;
                   1688:                                                        int_break_point.table[index - 1].status = 0;
                   1689:                                                }
                   1690:                                        }
                   1691:                                } else {
                   1692:                                        telnet_printf("invalid parameter number\n");
                   1693:                                }
                   1694:                        } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
                   1695:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1696:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1697:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1698:                                                if(int_break_point.table[i].status != 0) {
                   1699:                                                        int_break_point.table[i].status = enabled ? 1 : -1;
                   1700:                                                }
                   1701:                                        }
                   1702:                                } else if(num >= 2) {
                   1703:                                        for(int i = 1; i < num; i++) {
                   1704:                                                int index = debugger_get_val(params[i]);
                   1705:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1706:                                                        telnet_printf("invalid index %x\n", index);
                   1707:                                                } else if(int_break_point.table[index - 1].status == 0) {
                   1708:                                                        telnet_printf("break point %x is null\n", index);
                   1709:                                                } else {
                   1710:                                                        int_break_point.table[index - 1].status = enabled ? 1 : -1;
                   1711:                                                }
                   1712:                                        }
                   1713:                                } else {
                   1714:                                        telnet_printf("invalid parameter number\n");
                   1715:                                }
                   1716:                        } else if(stricmp(params[0], "INTBL") == 0) {
                   1717:                                if(num == 1) {
                   1718:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1719:                                                if(int_break_point.table[i].status) {
                   1720:                                                        telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
                   1721:                                                        if(int_break_point.table[i].ah_registered) {
                   1722:                                                                telnet_printf(" %02X", int_break_point.table[i].ah);
                   1723:                                                        }
                   1724:                                                        if(int_break_point.table[i].al_registered) {
                   1725:                                                                telnet_printf(" %02X", int_break_point.table[i].al);
                   1726:                                                        }
                   1727:                                                        telnet_printf("\n");
                   1728:                                                }
                   1729:                                        }
                   1730:                                } else {
                   1731:                                        telnet_printf("invalid parameter number\n");
                   1732:                                }
                   1733:                        } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
                   1734:                                if(num == 1 || num == 2) {
                   1735:                                        break_point_t break_point_stored;
                   1736:                                        bool break_points_stored = false;
                   1737:                                        
                   1738:                                        if(stricmp(params[0], "P") == 0) {
                   1739:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1740:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1741:                                                break_points_stored = true;
                   1742:                                                
                   1743:                                                break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
                   1744:                                                break_point.table[0].status = 1;
                   1745:                                        } else if(num >= 2) {
                   1746:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1747:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1748:                                                break_points_stored = true;
                   1749:                                                
                   1750:                                                UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1751:                                                UINT32 ofs = debugger_get_ofs(params[1]);
                   1752:                                                break_point.table[0].addr = (seg << 4) + ofs;
                   1753:                                                break_point.table[0].seg = seg;
                   1754:                                                break_point.table[0].ofs = ofs;
                   1755:                                                break_point.table[0].status = 1;
                   1756:                                        }
                   1757:                                        break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1758:                                        now_going = true;
                   1759:                                        now_suspended = false;
                   1760:                                        
                   1761:                                        telnet_command("\033[2l"); // key unlock
                   1762:                                        while(!m_halted && !now_suspended) {
                   1763:                                                if(telnet_kbhit()) {
                   1764:                                                        break;
                   1765:                                                }
                   1766:                                                Sleep(10);
                   1767:                                        }
                   1768:                                        now_going = false;
                   1769:                                        telnet_command("\033[2h"); // key lock
                   1770:                                        
                   1771:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1772:                                                Sleep(100);
                   1773:                                                if(!m_halted && !now_suspended) {
                   1774:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1775:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1776:                                                }
                   1777:                                        }
                   1778:                                        while(!m_halted && !now_suspended) {
                   1779:                                                if(telnet_disconnected()) {
                   1780:                                                        break;
                   1781:                                                }
                   1782:                                                Sleep(10);
                   1783:                                        }
                   1784:                                        dasm_seg = SREG(CS);
                   1785:                                        dasm_ofs = m_eip;
                   1786:                                        
                   1787:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1788:                                        debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1789:                                        telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1790:                                        
                   1791:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1792:                                        debugger_regs_info(buffer);
                   1793:                                        telnet_printf("%s", buffer);
                   1794:                                        
                   1795:                                        if(break_point.hit) {
                   1796:                                                if(stricmp(params[0], "G") == 0 && num == 1) {
                   1797:                                                        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1798:                                                        telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1799:                                                }
                   1800:                                        } else if(rd_break_point.hit) {
                   1801:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1802:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1803:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1804:                                                m_prev_cs, m_prev_eip);
                   1805:                                        } else if(wr_break_point.hit) {
                   1806:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1807:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1808:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1809:                                                m_prev_cs, m_prev_eip);
                   1810:                                        } else if(in_break_point.hit) {
                   1811:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1812:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1813:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1814:                                                m_prev_cs, m_prev_eip);
                   1815:                                        } else if(out_break_point.hit) {
                   1816:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1817:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1818:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1819:                                                m_prev_cs, m_prev_eip);
                   1820:                                        } else if(int_break_point.hit) {
                   1821:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1822:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1823:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1824:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1825:                                                }
                   1826:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1827:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1828:                                                }
                   1829:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1830:                                        } else {
                   1831:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1832:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1833:                                        }
                   1834:                                        if(break_points_stored) {
                   1835:                                                memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
                   1836:                                        }
                   1837:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1838:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1839:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1840:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1841:                                } else {
                   1842:                                        telnet_printf("invalid parameter number\n");
                   1843:                                }
                   1844:                        } else if(stricmp(params[0], "T") == 0) {
                   1845:                                if(num == 1 || num == 2) {
                   1846:                                        int steps = 1;
                   1847:                                        if(num >= 2) {
                   1848:                                                steps = debugger_get_val(params[1]);
                   1849:                                        }
                   1850:                                        
                   1851:                                        telnet_command("\033[2l"); // key unlock
                   1852:                                        while(steps-- > 0) {
                   1853:                                                break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1854:                                                now_going = false;
                   1855:                                                now_suspended = false;
                   1856:                                                
                   1857:                                                while(!m_halted && !now_suspended) {
                   1858:                                                        if(telnet_disconnected()) {
                   1859:                                                                break;
                   1860:                                                        }
                   1861:                                                        Sleep(10);
                   1862:                                                }
                   1863:                                                dasm_seg = SREG(CS);
                   1864:                                                dasm_ofs = m_eip;
                   1865:                                                
                   1866:                                                telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1867:                                                debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1868:                                                telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1869:                                                
                   1870:                                                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1871:                                                debugger_regs_info(buffer);
                   1872:                                                telnet_printf("%s", buffer);
                   1873:                                                
                   1874:                                                if(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit || telnet_kbhit()) {
                   1875:                                                        break;
                   1876:                                                }
                   1877:                                        }
                   1878:                                        telnet_command("\033[2h"); // key lock
                   1879:                                        
                   1880:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1881:                                                Sleep(100);
                   1882:                                                if(!m_halted && !now_suspended) {
                   1883:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1884:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1885:                                                }
                   1886:                                        }
                   1887:                                        while(!m_halted && !now_suspended) {
                   1888:                                                if(telnet_disconnected()) {
                   1889:                                                        break;
                   1890:                                                }
                   1891:                                                Sleep(10);
                   1892:                                        }
                   1893:                                        if(break_point.hit) {
                   1894:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1895:                                                telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1896:                                        } else if(rd_break_point.hit) {
                   1897:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1898:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1899:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1900:                                                m_prev_cs, m_prev_eip);
                   1901:                                        } else if(wr_break_point.hit) {
                   1902:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1903:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1904:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1905:                                                m_prev_cs, m_prev_eip);
                   1906:                                        } else if(in_break_point.hit) {
                   1907:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1908:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1909:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1910:                                                m_prev_cs, m_prev_eip);
                   1911:                                        } else if(out_break_point.hit) {
                   1912:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1913:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1914:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1915:                                                m_prev_cs, m_prev_eip);
                   1916:                                        } else if(int_break_point.hit) {
                   1917:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1918:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1919:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1920:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1921:                                                }
                   1922:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1923:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1924:                                                }
                   1925:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1926:                                        } else if(steps > 0) {
                   1927:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1928:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1929:                                        }
                   1930:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1931:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1932:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1933:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1934:                                } else {
                   1935:                                        telnet_printf("invalid parameter number\n");
                   1936:                                }
                   1937:                        } else if(stricmp(params[0], "Q") == 0) {
                   1938:                                break;
                   1939:                        } else if(stricmp(params[0], "X") == 0) {
                   1940:                                debugger_process_info(buffer);
                   1941:                                telnet_printf("%s", buffer);
                   1942:                        } else if(stricmp(params[0], ">") == 0) {
                   1943:                                if(num == 2) {
                   1944:                                        if(fp_debugger != NULL) {
                   1945:                                                fclose(fp_debugger);
                   1946:                                                fp_debugger = NULL;
                   1947:                                        }
                   1948:                                        fp_debugger = fopen(params[1], "w");
                   1949:                                } else {
                   1950:                                        telnet_printf("invalid parameter number\n");
                   1951:                                }
                   1952:                        } else if(stricmp(params[0], "<") == 0) {
                   1953:                                if(num == 2) {
                   1954:                                        if(fi_debugger != NULL) {
                   1955:                                                fclose(fi_debugger);
                   1956:                                                fi_debugger = NULL;
                   1957:                                        }
                   1958:                                        fi_debugger = fopen(params[1], "r");
                   1959:                                } else {
                   1960:                                        telnet_printf("invalid parameter number\n");
                   1961:                                }
                   1962:                        } else if(stricmp(params[0], "?") == 0) {
                   1963:                                telnet_printf("D [<start> [<end>]] - dump memory\n");
                   1964:                                telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
                   1965:                                telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
                   1966:                                telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
                   1967:                                telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
                   1968:                                
                   1969:                                telnet_printf("R - show registers\n");
                   1970:                                telnet_printf("R <reg> <value> - edit register\n");
                   1971:                                telnet_printf("S <start> <end> <list> - search\n");
                   1972:                                telnet_printf("U [<start> [<end>]] - unassemble\n");
                   1973:                                
                   1974:                                telnet_printf("H <value> <value> - hexadd\n");
                   1975:                                
                   1976:                                telnet_printf("BP <address> - set breakpoint\n");
                   1977:                                telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
                   1978:                                telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
                   1979:                                telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
                   1980:                                telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
                   1981:                                telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
                   1982:                                
                   1983:                                telnet_printf("G - go (press enter key to break)\n");
                   1984:                                telnet_printf("G <address> - go and break at address\n");
                   1985:                                telnet_printf("P - trace one opcode (step over)\n");
                   1986:                                telnet_printf("T [<count>] - trace (step in)\n");
                   1987:                                telnet_printf("Q - quit\n");
                   1988:                                telnet_printf("X - show dos process info\n");
                   1989:                                
                   1990:                                telnet_printf("> <filename> - output logfile\n");
                   1991:                                telnet_printf("< <filename> - input commands from file\n");
                   1992:                                
                   1993:                                telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
                   1994:                                telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
                   1995:                        } else {
                   1996:                                telnet_printf("unknown command %s\n", params[0]);
                   1997:                        }
                   1998:                }
                   1999:        }
                   2000:        if(fp_debugger != NULL) {
                   2001:                fclose(fp_debugger);
                   2002:                fp_debugger = NULL;
                   2003:        }
                   2004:        if(fi_debugger != NULL) {
                   2005:                fclose(fi_debugger);
                   2006:                fi_debugger = NULL;
                   2007:        }
                   2008:        now_debugging = now_going = now_suspended = force_suspend = false;
                   2009:        closesocket(cli_socket);
                   2010: }
                   2011: 
                   2012: const char *debugger_get_ttermpro_path()
                   2013: {
                   2014:        static char path[MAX_PATH] = {0};
                   2015:        
                   2016:        if(getenv("ProgramFiles")) {
                   2017:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
                   2018:        }
                   2019:        return(path);
                   2020: }
                   2021: 
                   2022: const char *debugger_get_ttermpro_x86_path()
                   2023: {
                   2024:        static char path[MAX_PATH] = {0};
                   2025:        
                   2026:        if(getenv("ProgramFiles(x86)")) {
                   2027:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
                   2028:        }
                   2029:        return(path);
                   2030: }
                   2031: 
                   2032: const char *debugger_get_putty_path()
                   2033: {
                   2034:        static char path[MAX_PATH] = {0};
                   2035:        
                   2036:        if(getenv("ProgramFiles")) {
                   2037:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
                   2038:        }
                   2039:        return(path);
                   2040: }
                   2041: 
                   2042: const char *debugger_get_putty_x86_path()
                   2043: {
                   2044:        static char path[MAX_PATH] = {0};
                   2045:        
                   2046:        if(getenv("ProgramFiles(x86)")) {
                   2047:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
                   2048:        }
                   2049:        return(path);
                   2050: }
                   2051: 
                   2052: const char *debugger_get_telnet_path()
                   2053: {
                   2054:        // NOTE: When you run 32bit version of msdos.exe on Windows x64,
                   2055:        // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
                   2056:        // But 32bit version of telnet.exe will not be installed in SysWOW64
                   2057:        // and 64bit version of telnet.exe will be installed in System32.
                   2058:        static char path[MAX_PATH] = {0};
                   2059:        
                   2060:        if(getenv("windir") != NULL) {
                   2061:                sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
                   2062:        }
                   2063:        return(path);
                   2064: }
                   2065: 
                   2066: DWORD WINAPI debugger_thread(LPVOID)
                   2067: {
                   2068:        WSADATA was_data;
                   2069:        struct sockaddr_in svr_addr;
                   2070:        struct sockaddr_in cli_addr;
                   2071:        int cli_addr_len = sizeof(cli_addr);
                   2072:        int port = 23;
                   2073:        int bind_stat = SOCKET_ERROR;
                   2074:        struct timeval timeout;
                   2075:        
                   2076:        WSAStartup(MAKEWORD(2,0), &was_data);
                   2077:        
                   2078:        if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
                   2079:                memset(&svr_addr, 0, sizeof(svr_addr));
                   2080:                svr_addr.sin_family = AF_INET;
                   2081:                svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
                   2082:                
                   2083:                while(!m_halted && port < 10000) {
                   2084:                        svr_addr.sin_port = htons(port);
                   2085:                        if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
                   2086:                                break;
                   2087:                        } else {
                   2088:                                port = (port == 23) ? 9000 : (port + 1);
                   2089:                        }
                   2090:                }
                   2091:                if(bind_stat == 0) {
                   2092:                        timeout.tv_sec = 1;
                   2093:                        timeout.tv_usec = 0;
1.1.1.45! root     2094:                        setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
1.1.1.33  root     2095:                        
                   2096:                        listen(svr_socket, 1);
                   2097:                        
                   2098:                        char command[MAX_PATH] = {0};
                   2099:                        STARTUPINFO si;
                   2100:                        PROCESS_INFORMATION pi;
                   2101:                        
                   2102:                        if(_access(debugger_get_ttermpro_path(), 0) == 0) {
                   2103:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
                   2104:                        } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
                   2105:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
                   2106:                        } else if(_access(debugger_get_putty_path(), 0) == 0) {
                   2107:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
                   2108:                        } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
                   2109:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
                   2110:                        } else if(_access(debugger_get_telnet_path(), 0) == 0) {
                   2111:                                sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
                   2112:                        }
                   2113:                        if(command[0] != '\0') {
                   2114:                                memset(&si, 0, sizeof(STARTUPINFO));
                   2115:                                memset(&pi, 0, sizeof(PROCESS_INFORMATION));
                   2116:                                CreateProcess(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
                   2117:                        }
                   2118:                        
                   2119:                        while(!m_halted) {
                   2120:                                if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
                   2121:                                        u_long val = 1;
                   2122:                                        ioctlsocket(cli_socket, FIONBIO, &val);
                   2123:                                        debugger_main();
                   2124:                                }
                   2125:                        }
                   2126:                }
                   2127:        }
                   2128:        WSACleanup();
                   2129:        return(0);
                   2130: }
                   2131: #endif
                   2132: 
                   2133: /* ----------------------------------------------------------------------------
1.1       root     2134:        main
                   2135: ---------------------------------------------------------------------------- */
                   2136: 
1.1.1.28  root     2137: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                   2138: {
                   2139:        if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33  root     2140:                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     2141: #ifdef USE_SERVICE_THREAD
                   2142:                        EnterCriticalSection(&key_buf_crit_sect);
                   2143: #endif
1.1.1.33  root     2144:                        key_buf_char->clear();
                   2145:                        key_buf_scan->clear();
1.1.1.35  root     2146: #ifdef USE_SERVICE_THREAD
                   2147:                        LeaveCriticalSection(&key_buf_crit_sect);
                   2148: #endif
1.1.1.33  root     2149:                }
                   2150: //             key_code = key_recv = 0;
1.1.1.28  root     2151:                return TRUE;
                   2152:        } else if(dwCtrlType == CTRL_C_EVENT) {
                   2153:                return TRUE;
                   2154:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
                   2155:                // this program will be terminated abnormally, do minimum end process
                   2156:                exit_handler();
                   2157:                exit(1);
                   2158:        }
                   2159:        return FALSE;
                   2160: }
                   2161: 
                   2162: void exit_handler()
                   2163: {
                   2164:        if(temp_file_created) {
                   2165:                DeleteFile(temp_file_path);
                   2166:                temp_file_created = false;
                   2167:        }
                   2168:        if(key_buf_char != NULL) {
                   2169:                key_buf_char->release();
                   2170:                delete key_buf_char;
                   2171:                key_buf_char = NULL;
                   2172:        }
                   2173:        if(key_buf_scan != NULL) {
                   2174:                key_buf_scan->release();
                   2175:                delete key_buf_scan;
                   2176:                key_buf_scan = NULL;
                   2177:        }
1.1.1.32  root     2178: #ifdef SUPPORT_XMS
                   2179:        msdos_xms_release();
                   2180: #endif
1.1.1.28  root     2181:        hardware_release();
                   2182: }
                   2183: 
1.1.1.35  root     2184: #ifdef USE_VRAM_THREAD
1.1.1.28  root     2185: DWORD WINAPI vram_thread(LPVOID)
                   2186: {
                   2187:        while(!m_halted) {
                   2188:                EnterCriticalSection(&vram_crit_sect);
                   2189:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                   2190:                        vram_flush_char();
                   2191:                }
                   2192:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                   2193:                        vram_flush_attr();
                   2194:                }
                   2195:                vram_last_length_char = vram_length_char;
                   2196:                vram_last_length_attr = vram_length_attr;
                   2197:                LeaveCriticalSection(&vram_crit_sect);
                   2198:                // this is about half the maximum keyboard repeat rate - any
                   2199:                // lower tends to be jerky, any higher misses updates
                   2200:                Sleep(15);
                   2201:        }
                   2202:        return 0;
                   2203: }
                   2204: #endif
                   2205: 
1.1.1.45! root     2206: long get_section_in_exec_file(FILE *fp, const char *name)
1.1.1.28  root     2207: {
                   2208:        UINT8 header[0x400];
                   2209:        
                   2210:        long position = ftell(fp);
                   2211:        fseek(fp, 0, SEEK_SET);
                   2212:        fread(header, sizeof(header), 1, fp);
                   2213:        fseek(fp, position, SEEK_SET);
                   2214:        
                   2215:        try {
                   2216:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2217:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2218:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2219:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2220:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2221:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2222:                
                   2223:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                   2224:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                   2225:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                   2226:                                return(sectionHeader->PointerToRawData);
                   2227:                        }
                   2228:                }
                   2229:        } catch(...) {
                   2230:        }
                   2231:        return(0);
                   2232: }
                   2233: 
1.1.1.10  root     2234: bool is_started_from_command_prompt()
                   2235: {
1.1.1.18  root     2236:        bool ret = false;
                   2237:        
                   2238:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                   2239:        if(hLibrary) {
                   2240:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                   2241:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                   2242:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
                   2243:                if(lpfnGetConsoleProcessList) {
                   2244:                        DWORD pl;
                   2245:                        ret = (lpfnGetConsoleProcessList(&pl, 1) > 1);
                   2246:                        FreeLibrary(hLibrary);
                   2247:                        return(ret);
                   2248:                }
                   2249:                FreeLibrary(hLibrary);
                   2250:        }
                   2251:        
                   2252:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   2253:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                   2254:                DWORD dwParentProcessID = 0;
                   2255:                PROCESSENTRY32 pe32;
                   2256:                pe32.dwSize = sizeof(PROCESSENTRY32);
                   2257:                if(Process32First(hSnapshot, &pe32)) {
                   2258:                        do {
                   2259:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                   2260:                                        dwParentProcessID = pe32.th32ParentProcessID;
                   2261:                                        break;
                   2262:                                }
                   2263:                        } while(Process32Next(hSnapshot, &pe32));
                   2264:                }
                   2265:                CloseHandle(hSnapshot);
                   2266:                if(dwParentProcessID != 0) {
                   2267:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                   2268:                        if(hProcess != NULL) {
                   2269:                                HMODULE hMod;
                   2270:                                DWORD cbNeeded;
                   2271:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                   2272:                                        char module_name[MAX_PATH];
                   2273:                                        if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
                   2274:                                                ret = (_strnicmp(module_name, "cmd.exe", 7) == 0);
                   2275:                                        }
                   2276:                                }
                   2277:                                CloseHandle(hProcess);
                   2278:                        }
                   2279:                }
                   2280:        }
                   2281:        return(ret);
1.1.1.14  root     2282: }
                   2283: 
                   2284: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                   2285: {
1.1.1.24  root     2286:        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
1.1.1.14  root     2287:        OSVERSIONINFOEX osvi;
                   2288:        DWORDLONG dwlConditionMask = 0;
                   2289:        int op = VER_GREATER_EQUAL;
                   2290:        
                   2291:        // Initialize the OSVERSIONINFOEX structure.
                   2292:        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
                   2293:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
                   2294:        osvi.dwMajorVersion = dwMajorVersion;
                   2295:        osvi.dwMinorVersion = dwMinorVersion;
                   2296:        osvi.wServicePackMajor = wServicePackMajor;
                   2297:        osvi.wServicePackMinor = wServicePackMinor;
                   2298:        
                   2299:         // Initialize the condition mask.
                   2300:        VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                   2301:        VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                   2302:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                   2303:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                   2304:        
                   2305:        // Perform the test.
                   2306:        return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                   2307: }
                   2308: 
1.1.1.27  root     2309: void get_sio_port_numbers()
                   2310: {
                   2311:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                   2312:        HDEVINFO hDevInfo = 0;
                   2313:        HKEY hKey = 0;
                   2314:        if((hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
                   2315:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                   2316:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                   2317:                                char chData[256];
                   2318:                                DWORD dwType = 0;
                   2319:                                DWORD dwSize = sizeof(chData);
                   2320:                                int port_number = 0;
                   2321:                                
                   2322:                                if(RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
                   2323:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                   2324:                                                port_number = atoi(chData + 3);
                   2325:                                        }
                   2326:                                }
                   2327:                                RegCloseKey(hKey);
                   2328:                                
1.1.1.29  root     2329:                                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     2330:                                        continue;
                   2331:                                }
                   2332:                                if(sio_port_number[0] == 0) {
                   2333:                                        sio_port_number[0] = port_number;
                   2334:                                } else if(sio_port_number[1] == 0) {
                   2335:                                        sio_port_number[1] = port_number;
1.1.1.29  root     2336:                                } else if(sio_port_number[2] == 0) {
                   2337:                                        sio_port_number[2] = port_number;
                   2338:                                } else if(sio_port_number[3] == 0) {
                   2339:                                        sio_port_number[3] = port_number;
1.1.1.27  root     2340:                                }
1.1.1.29  root     2341:                                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     2342:                                        break;
                   2343:                                }
                   2344:                        }
                   2345:                }
                   2346:        }
                   2347: }
                   2348: 
1.1.1.28  root     2349: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                   2350: 
1.1       root     2351: int main(int argc, char *argv[], char *envp[])
                   2352: {
1.1.1.9   root     2353:        int arg_offset = 0;
                   2354:        int standard_env = 0;
1.1.1.14  root     2355:        int buf_width = 0, buf_height = 0;
1.1.1.28  root     2356:        bool get_console_info_success = false;
                   2357:        bool screen_size_changed = false;
                   2358:        
                   2359:        _TCHAR path[MAX_PATH], full[MAX_PATH], *name = NULL;
                   2360:        GetModuleFileName(NULL, path, MAX_PATH);
                   2361:        GetFullPathName(path, MAX_PATH, full, &name);
1.1       root     2362:        
1.1.1.27  root     2363:        char dummy_argv_0[] = "msdos.exe";
                   2364:        char dummy_argv_1[MAX_PATH];
                   2365:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                   2366:        char new_exec_file[MAX_PATH];
                   2367:        bool convert_cmd_file = false;
1.1.1.28  root     2368:        unsigned int code_page = 0;
1.1.1.27  root     2369:        
                   2370:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root     2371:                // check if command file is embedded to this execution file
                   2372:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root     2373:                FILE* fp = fopen(full, "rb");
1.1.1.28  root     2374:                long offset = get_section_in_exec_file(fp, ".msdos");
                   2375:                if(offset != 0) {
1.1.1.30  root     2376:                        UINT8 buffer[16];
1.1.1.28  root     2377:                        fseek(fp, offset, SEEK_SET);
                   2378:                        fread(buffer, sizeof(buffer), 1, fp);
                   2379:                        
                   2380:                        // restore flags
                   2381:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                   2382:                        no_windows          = ((buffer[0] & 0x02) != 0);
                   2383:                        standard_env        = ((buffer[0] & 0x04) != 0);
                   2384:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                   2385:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                   2386:                        if((buffer[0] & 0x20) != 0) {
                   2387:                                get_sio_port_numbers();
                   2388:                        }
                   2389:                        if((buffer[0] & 0x40) != 0) {
                   2390:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                   2391:                                support_ems = true;
1.1.1.30  root     2392:                        }
1.1.1.27  root     2393: #ifdef SUPPORT_XMS
1.1.1.30  root     2394:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root     2395:                                support_xms = true;
                   2396:                        }
1.1.1.30  root     2397: #endif
1.1.1.28  root     2398:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                   2399:                                buf_width  = buffer[1] | (buffer[2] << 8);
                   2400:                                buf_height = buffer[3] | (buffer[4] << 8);
                   2401:                        }
                   2402:                        if(buffer[5] != 0) {
1.1.1.30  root     2403:                                dos_major_version = buffer[5];
                   2404:                                dos_minor_version = buffer[6];
                   2405:                        }
                   2406:                        if(buffer[7] != 0) {
                   2407:                                win_major_version = buffer[7];
                   2408:                                win_minor_version = buffer[8];
1.1.1.28  root     2409:                        }
1.1.1.30  root     2410:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root     2411:                                SetConsoleCP(code_page);
                   2412:                                SetConsoleOutputCP(code_page);
                   2413:                        }
1.1.1.30  root     2414:                        int name_len = buffer[11];
                   2415:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root     2416:                        
                   2417:                        // restore command file name
                   2418:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                   2419:                        fread(dummy_argv_1, name_len, 1, fp);
                   2420:                        
                   2421:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                   2422:                                // if original command file exists, create a temporary file name
                   2423:                                if(GetTempFileName(_T("."), _T("DOS"), 0, dummy_argv_1) != 0) {
                   2424:                                        // create a temporary command file in the current director
                   2425:                                        DeleteFile(dummy_argv_1);
1.1.1.27  root     2426:                                } else {
1.1.1.28  root     2427:                                        // create a temporary command file in the temporary folder
                   2428:                                        GetTempPath(MAX_PATH, path);
                   2429:                                        if(GetTempFileName(path, _T("DOS"), 0, dummy_argv_1) != 0) {
                   2430:                                                DeleteFile(dummy_argv_1);
                   2431:                                        } else {
                   2432:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                   2433:                                        }
1.1.1.27  root     2434:                                }
1.1.1.28  root     2435:                                // check the command file type
                   2436:                                fread(buffer, 2, 1, fp);
                   2437:                                fseek(fp, -2, SEEK_CUR);
                   2438:                                if(memcmp(buffer, "MZ", 2) != 0) {
                   2439:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                   2440:                                } else {
                   2441:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root     2442:                                }
                   2443:                        }
1.1.1.28  root     2444:                        
                   2445:                        // restore command file
                   2446:                        FILE* fo = fopen(dummy_argv_1, "wb");
                   2447:                        for(int i = 0; i < file_len; i++) {
                   2448:                                fputc(fgetc(fp), fo);
                   2449:                        }
                   2450:                        fclose(fo);
                   2451:                        
                   2452:                        GetFullPathName(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
                   2453:                        temp_file_created = true;
                   2454:                        SetFileAttributes(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
                   2455:                        
                   2456:                        // adjust argc/argv
                   2457:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                   2458:                                dummy_argv[i + 1] = argv[i];
                   2459:                        }
                   2460:                        argc++;
                   2461:                        argv = dummy_argv;
1.1.1.27  root     2462:                }
                   2463:                fclose(fp);
                   2464:        }
1.1.1.9   root     2465:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     2466:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   2467:                        stay_busy = true;
                   2468:                        arg_offset++;
1.1.1.27  root     2469:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   2470:                        if(argv[i][2] != '\0') {
                   2471:                                strcpy(new_exec_file, &argv[i][2]);
                   2472:                        } else {
                   2473:                                strcpy(new_exec_file, "new_exec_file.exe");
                   2474:                        }
                   2475:                        convert_cmd_file = true;
                   2476:                        arg_offset++;
1.1.1.28  root     2477:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   2478:                        if(IS_NUMERIC(argv[i][2])) {
                   2479:                                code_page = atoi(&argv[i][2]);
                   2480:                        } else {
                   2481:                                code_page = GetConsoleCP();
                   2482:                        }
                   2483:                        arg_offset++;
1.1.1.25  root     2484:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   2485:                        no_windows = true;
                   2486:                        arg_offset++;
                   2487:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     2488:                        standard_env = 1;
                   2489:                        arg_offset++;
1.1.1.14  root     2490:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   2491:                        ignore_illegal_insn = true;
                   2492:                        arg_offset++;
                   2493:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   2494:                        limit_max_memory = true;
                   2495:                        arg_offset++;
                   2496:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.17  root     2497:                        if(sscanf(argv[1] + 2, "%d,%d", &buf_height, &buf_width) != 2) {
                   2498:                                buf_width = buf_height = 0;
                   2499:                        }
                   2500:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   2501:                                buf_width = 80;
                   2502:                        }
                   2503:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   2504:                                buf_height = 25;
                   2505:                        }
1.1.1.14  root     2506:                        arg_offset++;
1.1.1.25  root     2507:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     2508:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     2509:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   2510:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   2511:                                        sio_port_number[1] = atoi(p1 + 1);
                   2512:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   2513:                                                sio_port_number[2] = atoi(p2 + 1);
                   2514:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   2515:                                                        sio_port_number[3] = atoi(p3 + 1);
                   2516:                                                }
                   2517:                                        }
1.1.1.25  root     2518:                                }
1.1.1.29  root     2519:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     2520:                        }
1.1.1.29  root     2521:                        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     2522:                                get_sio_port_numbers();
1.1.1.25  root     2523:                        }
                   2524:                        arg_offset++;
1.1.1.9   root     2525:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     2526:                        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     2527:                                dos_major_version = argv[i][2] - '0';
                   2528:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   2529:                        }
                   2530:                        arg_offset++;
                   2531:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   2532:                        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]))) {
                   2533:                                win_major_version = argv[i][2] - '0';
                   2534:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     2535:                        }
                   2536:                        arg_offset++;
1.1.1.25  root     2537:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   2538:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   2539:                        support_ems = true;
                   2540: #ifdef SUPPORT_XMS
                   2541:                        support_xms = true;
                   2542: #endif
                   2543:                        arg_offset++;
1.1.1.9   root     2544:                } else {
                   2545:                        break;
                   2546:                }
                   2547:        }
                   2548:        if(argc < 2 + arg_offset) {
1.1       root     2549: #ifdef _WIN64
1.1.1.14  root     2550:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     2551: #else
1.1.1.14  root     2552:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     2553: #endif
1.1.1.25  root     2554:                fprintf(stderr,
1.1.1.28  root     2555:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     2556:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     2557:                        "\n"
                   2558:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     2559: #ifdef _WIN64
1.1.1.27  root     2560:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     2561: #else
1.1.1.27  root     2562:                        "\t-c\tconvert command file to 32bit execution file\n"
                   2563: #endif
1.1.1.28  root     2564:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     2565:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   2566:                        "\t-e\tuse a reduced environment block\n"
                   2567:                        "\t-i\tignore invalid instructions\n"
                   2568:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   2569:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   2570:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   2571:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     2572:                        "\t-w\tset the Windows version\n"
1.1.1.19  root     2573: #ifdef SUPPORT_XMS
1.1.1.28  root     2574:                        "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19  root     2575: #else
1.1.1.28  root     2576:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2577: #endif
                   2578:                );
1.1.1.10  root     2579:                
                   2580:                if(!is_started_from_command_prompt()) {
                   2581:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2582:                        while(!_kbhit()) {
                   2583:                                Sleep(10);
                   2584:                        }
                   2585:                }
1.1.1.20  root     2586: #ifdef _DEBUG
                   2587:                _CrtDumpMemoryLeaks();
                   2588: #endif
1.1       root     2589:                return(EXIT_FAILURE);
                   2590:        }
1.1.1.27  root     2591:        if(convert_cmd_file) {
                   2592:                retval = EXIT_FAILURE;
1.1.1.28  root     2593:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2594:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2595:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2596:                        
1.1.1.28  root     2597:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2598:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2599:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2600:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2601:                        } else {
1.1.1.28  root     2602:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2603:                                if(offset != 0) {
                   2604:                                        UINT8 buffer[14];
                   2605:                                        fseek(fp, offset, SEEK_SET);
                   2606:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2607:                                        memset(path, 0, sizeof(path));
                   2608:                                        fread(path, buffer[9], 1, fp);
                   2609:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2610:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2611:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2612:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2613:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2614:                                } else {
                   2615:                                        // read pe header of msdos.exe
                   2616:                                        UINT8 header[0x400];
                   2617:                                        fseek(fp, 0, SEEK_SET);
                   2618:                                        fread(header, sizeof(header), 1, fp);
                   2619:                                        
                   2620:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2621:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2622:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2623:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2624:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2625:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2626:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2627:                                        
                   2628:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2629:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2630:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2631:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2632:                                        if(dwExtraLastSectionBytes != 0) {
                   2633:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2634:                                                dwLastSectionSize += dwRemain;
                   2635:                                        }
                   2636:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2637:                                        
                   2638:                                        // store msdos.exe
                   2639:                                        fseek(fp, 0, SEEK_SET);
                   2640:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2641:                                                if((data = fgetc(fp)) != EOF) {
                   2642:                                                        fputc(data, fo);
                   2643:                                                } else {
                   2644:                                                        // we should not reach here :-(
                   2645:                                                        fputc(0, fo);
                   2646:                                                }
                   2647:                                        }
                   2648:                                        
                   2649:                                        // store options
                   2650:                                        UINT8 flags = 0;
                   2651:                                        if(stay_busy) {
                   2652:                                                flags |= 0x01;
                   2653:                                        }
                   2654:                                        if(no_windows) {
                   2655:                                                flags |= 0x02;
                   2656:                                        }
                   2657:                                        if(standard_env) {
                   2658:                                                flags |= 0x04;
                   2659:                                        }
                   2660:                                        if(ignore_illegal_insn) {
                   2661:                                                flags |= 0x08;
                   2662:                                        }
                   2663:                                        if(limit_max_memory) {
                   2664:                                                flags |= 0x10;
                   2665:                                        }
1.1.1.29  root     2666:                                        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     2667:                                                flags |= 0x20;
                   2668:                                        }
                   2669:                                        if(support_ems) {
                   2670:                                                flags |= 0x40;
                   2671:                                        }
1.1.1.30  root     2672: #ifdef SUPPORT_XMS
                   2673:                                        if(support_xms) {
                   2674:                                                flags |= 0x80;
                   2675:                                        }
                   2676: #endif
1.1.1.28  root     2677:                                        
                   2678:                                        fputc(flags, fo);
                   2679:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2680:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2681:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2682:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2683:                                        fputc(dos_major_version, fo);
                   2684:                                        fputc(dos_minor_version, fo);
                   2685:                                        fputc(win_major_version, fo);
                   2686:                                        fputc(win_minor_version, fo);
1.1.1.28  root     2687:                                        fputc((code_page >> 0) & 0xff, fo);
                   2688:                                        fputc((code_page >> 8) & 0xff, fo);
                   2689:                                        
                   2690:                                        // store command file info
                   2691:                                        GetFullPathName(argv[arg_offset + 1], MAX_PATH, full, &name);
                   2692:                                        int name_len = strlen(name);
                   2693:                                        fseek(fs, 0, SEEK_END);
                   2694:                                        long file_size = ftell(fs);
                   2695:                                        
                   2696:                                        fputc(name_len, fo);
                   2697:                                        fputc((file_size >>  0) & 0xff, fo);
                   2698:                                        fputc((file_size >>  8) & 0xff, fo);
                   2699:                                        fputc((file_size >> 16) & 0xff, fo);
                   2700:                                        fputc((file_size >> 24) & 0xff, fo);
                   2701:                                        fwrite(name, name_len, 1, fo);
                   2702:                                        
                   2703:                                        // store command file
                   2704:                                        fseek(fs, 0, SEEK_SET);
                   2705:                                        for(int i = 0; i < file_size; i++) {
                   2706:                                                if((data = fgetc(fs)) != EOF) {
                   2707:                                                        fputc(data, fo);
                   2708:                                                } else {
                   2709:                                                        // we should not reach here :-(
                   2710:                                                        fputc(0, fo);
                   2711:                                                }
                   2712:                                        }
                   2713:                                        
                   2714:                                        // store padding data and update pe header
1.1.1.29  root     2715:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   2716:                                        coffHeader->NumberOfSections++;
                   2717:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   2718:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   2719:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   2720:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   2721:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     2722:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   2723:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   2724:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     2725:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     2726:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   2727:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     2728:                                                        if(i < 2) {
                   2729:                                                                fputc(padding[i & 15], fo);
                   2730:                                                        } else {
                   2731:                                                                fputc(padding[(i - 2) & 15], fo);
                   2732:                                                        }
1.1.1.28  root     2733:                                                }
                   2734:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   2735:                                        }
                   2736:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   2737:                                        
                   2738:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   2739:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   2740:                                        if(dwExtraNewSectionBytes != 0) {
                   2741:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   2742:                                                dwNewSectionSize += dwRemain;
                   2743:                                        }
                   2744:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   2745:                                        
                   2746:                                        fseek(fo, 0, SEEK_SET);
                   2747:                                        fwrite(header, sizeof(header), 1, fo);
                   2748:                                        
                   2749:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   2750:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     2751:                                }
                   2752:                        }
                   2753:                        if(fp != NULL) {
                   2754:                                fclose(fp);
                   2755:                        }
                   2756:                        if(fs != NULL) {
                   2757:                                fclose(fs);
                   2758:                        }
                   2759:                        if(fo != NULL) {
                   2760:                                fclose(fo);
                   2761:                        }
                   2762:                }
                   2763: #ifdef _DEBUG
                   2764:                _CrtDumpMemoryLeaks();
                   2765: #endif
                   2766:                return(retval);
                   2767:        }
1.1       root     2768:        
1.1.1.14  root     2769:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   2770:        
1.1.1.23  root     2771:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     2772:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     2773:        CONSOLE_CURSOR_INFO ci;
1.1.1.23  root     2774:        
1.1.1.28  root     2775:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     2776:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.24  root     2777:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1       root     2778:        
1.1.1.14  root     2779:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   2780:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   2781:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   2782:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     2783:                }
                   2784:        }
1.1.1.28  root     2785:        if(get_console_info_success) {
1.1.1.12  root     2786:                scr_width = csbi.dwSize.X;
1.1.1.14  root     2787:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2788:                
1.1.1.28  root     2789:                // v-text shadow buffer size must be lesser than 0x7fd0
                   2790:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     2791:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   2792:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   2793:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     2794:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     2795:                                scr_width = 80;
                   2796:                                scr_height = 25;
                   2797:                        }
1.1.1.28  root     2798:                        screen_size_changed = true;
1.1.1.14  root     2799:                }
1.1.1.12  root     2800:        } else {
                   2801:                // for a proof (not a console)
                   2802:                scr_width = 80;
                   2803:                scr_height = 25;
                   2804:        }
1.1.1.14  root     2805:        scr_buf_size.X = scr_width;
                   2806:        scr_buf_size.Y = scr_height;
                   2807:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   2808:        scr_top = csbi.srWindow.Top;
1.1       root     2809:        cursor_moved = false;
                   2810:        
1.1.1.35  root     2811: #ifdef USE_SERVICE_THREAD
                   2812:        InitializeCriticalSection(&input_crit_sect);
                   2813:        InitializeCriticalSection(&key_buf_crit_sect);
                   2814:        InitializeCriticalSection(&putch_crit_sect);
                   2815: #endif
1.1.1.25  root     2816:        key_buf_char = new FIFO(256);
                   2817:        key_buf_scan = new FIFO(256);
1.1       root     2818:        
                   2819:        hardware_init();
                   2820:        
1.1.1.33  root     2821: #ifdef USE_DEBUGGER
                   2822:        debugger_init();
                   2823: #endif
                   2824:        
1.1.1.9   root     2825:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     2826:                retval = EXIT_FAILURE;
                   2827:        } else {
1.1.1.27  root     2828: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     2829:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   2830: #endif
                   2831:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   2832:                
1.1.1.28  root     2833:                if(screen_size_changed) {
1.1.1.24  root     2834:                        change_console_size(scr_width, scr_height);
                   2835:                }
1.1.1.8   root     2836:                TIMECAPS caps;
                   2837:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   2838:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.35  root     2839: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2840:                InitializeCriticalSection(&vram_crit_sect);
                   2841:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   2842: #endif
1.1.1.33  root     2843: #ifdef USE_DEBUGGER
                   2844:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   2845:                // wait until telnet client starts and connects to me
                   2846:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   2847:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   2848:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   2849:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   2850:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   2851:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   2852:                                Sleep(100);
                   2853:                        }
                   2854:                }
                   2855: #endif
1.1       root     2856:                hardware_run();
1.1.1.35  root     2857: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2858:                vram_flush();
                   2859:                DeleteCriticalSection(&vram_crit_sect);
                   2860: #endif
1.1.1.24  root     2861:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     2862:                
1.1.1.24  root     2863:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.28  root     2864:                if(get_console_info_success) {
1.1.1.23  root     2865:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2866:                        if(restore_console_on_exit) {
1.1.1.14  root     2867:                                // window can't be bigger than buffer,
                   2868:                                // buffer can't be smaller than window,
                   2869:                                // so make a tiny window,
                   2870:                                // set the required buffer,
                   2871:                                // then set the required window
                   2872:                                SMALL_RECT rect;
                   2873:                                SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
                   2874:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     2875:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     2876:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12  root     2877:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   2878:                        }
1.1.1.14  root     2879:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   2880:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     2881:                }
1.1.1.24  root     2882:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   2883:                
1.1       root     2884:                msdos_finish();
1.1.1.14  root     2885:                
                   2886:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     2887:        }
1.1.1.35  root     2888:        if(temp_file_created) {
                   2889:                DeleteFile(temp_file_path);
                   2890:                temp_file_created = false;
                   2891:        }
1.1.1.10  root     2892:        hardware_finish();
                   2893:        
1.1.1.28  root     2894:        if(key_buf_char != NULL) {
                   2895:                key_buf_char->release();
                   2896:                delete key_buf_char;
                   2897:                key_buf_char = NULL;
                   2898:        }
                   2899:        if(key_buf_scan != NULL) {
                   2900:                key_buf_scan->release();
                   2901:                delete key_buf_scan;
                   2902:                key_buf_scan = NULL;
                   2903:        }
1.1.1.35  root     2904: #ifdef USE_SERVICE_THREAD
                   2905:        DeleteCriticalSection(&input_crit_sect);
                   2906:        DeleteCriticalSection(&key_buf_crit_sect);
                   2907:        DeleteCriticalSection(&putch_crit_sect);
                   2908: #endif
1.1.1.20  root     2909: #ifdef _DEBUG
                   2910:        _CrtDumpMemoryLeaks();
                   2911: #endif
1.1       root     2912:        return(retval);
                   2913: }
                   2914: 
1.1.1.20  root     2915: /* ----------------------------------------------------------------------------
                   2916:        console
                   2917: ---------------------------------------------------------------------------- */
                   2918: 
1.1.1.14  root     2919: void change_console_size(int width, int height)
1.1.1.12  root     2920: {
1.1.1.23  root     2921:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2922:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2923:        SMALL_RECT rect;
                   2924:        COORD co;
                   2925:        
                   2926:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     2927:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   2928:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
                   2929:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
                   2930:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2931:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2932:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   2933:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
                   2934:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2935:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2936:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     2937:                }
                   2938:        }
1.1.1.14  root     2939:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     2940:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     2941:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     2942:                SetConsoleCursorPosition(hStdout, co);
                   2943:                cursor_moved = true;
                   2944:        }
1.1.1.14  root     2945:        
                   2946:        // window can't be bigger than buffer,
                   2947:        // buffer can't be smaller than window,
                   2948:        // so make a tiny window,
                   2949:        // set the required buffer,
                   2950:        // then set the required window
                   2951:        SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12  root     2952:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     2953:        co.X = width;
                   2954:        co.Y = height;
1.1.1.12  root     2955:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     2956:        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2957:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   2958:        
                   2959:        scr_width = scr_buf_size.X = width;
                   2960:        scr_height = scr_buf_size.Y = height;
                   2961:        scr_top = 0;
                   2962:        
                   2963:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   2964:        
                   2965:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     2966:        text_vram_end_address = text_vram_top_address + regen;
                   2967:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   2968:        
1.1.1.14  root     2969:        if(regen > 0x4000) {
                   2970:                regen = 0x8000;
                   2971:                vram_pages = 1;
                   2972:        } else if(regen > 0x2000) {
                   2973:                regen = 0x4000;
                   2974:                vram_pages = 2;
                   2975:        } else if(regen > 0x1000) {
                   2976:                regen = 0x2000;
                   2977:                vram_pages = 4;
                   2978:        } else {
                   2979:                regen = 0x1000;
                   2980:                vram_pages = 8;
                   2981:        }
1.1.1.15  root     2982:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   2983:        *(UINT16 *)(mem + 0x44c) = regen;
                   2984:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   2985:        
1.1.1.24  root     2986:        mouse.min_position.x = 0;
                   2987:        mouse.min_position.y = 0;
1.1.1.34  root     2988:        mouse.max_position.x = 8 * (scr_width  - 1);
                   2989:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     2990:        
1.1.1.15  root     2991:        restore_console_on_exit = true;
1.1.1.14  root     2992: }
                   2993: 
                   2994: void clear_scr_buffer(WORD attr)
                   2995: {
                   2996:        for(int y = 0; y < scr_height; y++) {
                   2997:                for(int x = 0; x < scr_width; x++) {
                   2998:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   2999:                        SCR_BUF(y,x).Attributes = attr;
                   3000:                }
                   3001:        }
1.1.1.12  root     3002: }
                   3003: 
1.1.1.24  root     3004: bool update_console_input()
1.1       root     3005: {
1.1.1.35  root     3006: #ifdef USE_SERVICE_THREAD
                   3007:        EnterCriticalSection(&input_crit_sect);
                   3008: #endif
1.1.1.23  root     3009:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     3010:        DWORD dwNumberOfEvents = 0;
1.1       root     3011:        DWORD dwRead;
                   3012:        INPUT_RECORD ir[16];
1.1.1.24  root     3013:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   3014:        bool result = false;
1.1       root     3015:        
1.1.1.8   root     3016:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   3017:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   3018:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     3019:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.34  root     3020:                                        if(mouse.hidden == 0) {
                   3021:                                                // NOTE: if restore_console_on_exit, console is not scrolled
                   3022:                                                if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   3023:                                                        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   3024:                                                }
                   3025:                                                // FIXME: character size is always 8x8 ???
                   3026:                                                int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   3027:                                                int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
                   3028:                                                
                   3029:                                                if(mouse.position.x != x || mouse.position.y != y) {
                   3030:                                                        mouse.position.x = x;
                   3031:                                                        mouse.position.y = y;
                   3032:                                                        mouse.status |= 1;
1.1.1.43  root     3033:                                                        mouse.status_alt |= 1;
1.1.1.34  root     3034:                                                }
                   3035:                                        }
                   3036:                                        if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   3037:                                                for(int i = 0; i < MAX_MOUSE_BUTTONS; i++) {
                   3038:                                                        static const DWORD bits[] = {
                   3039:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   3040:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
                   3041:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
                   3042:                                                        };
                   3043:                                                        bool prev_status = mouse.buttons[i].status;
                   3044:                                                        mouse.buttons[i].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[i]) != 0);
                   3045:                                                        
                   3046:                                                        if(!prev_status && mouse.buttons[i].status) {
                   3047:                                                                mouse.buttons[i].pressed_times++;
                   3048:                                                                mouse.buttons[i].pressed_position.x = mouse.position.x;
                   3049:                                                                mouse.buttons[i].pressed_position.y = mouse.position.x;
1.1.1.43  root     3050:                                                                if(i < 2) {
                   3051:                                                                        mouse.status_alt |= 2 << (i * 2);
                   3052:                                                                }
1.1.1.34  root     3053:                                                                mouse.status |= 2 << (i * 2);
                   3054:                                                        } else if(prev_status && !mouse.buttons[i].status) {
                   3055:                                                                mouse.buttons[i].released_times++;
                   3056:                                                                mouse.buttons[i].released_position.x = mouse.position.x;
                   3057:                                                                mouse.buttons[i].released_position.y = mouse.position.x;
1.1.1.43  root     3058:                                                                if(i < 2) {
                   3059:                                                                        mouse.status_alt |= 4 << (i * 2);
                   3060:                                                                }
1.1.1.34  root     3061:                                                                mouse.status |= 4 << (i * 2);
1.1.1.14  root     3062:                                                        }
                   3063:                                                }
                   3064:                                        }
1.1.1.24  root     3065:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3066:                                        // update keyboard flags in bios data area
1.1.1.35  root     3067:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
                   3068:                                                mem[0x417] |= 0x40;
1.1.1.33  root     3069:                                        } else {
1.1.1.35  root     3070:                                                mem[0x417] &= ~0x40;
1.1.1.33  root     3071:                                        }
1.1.1.35  root     3072:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
                   3073:                                                mem[0x417] |= 0x20;
1.1.1.33  root     3074:                                        } else {
1.1.1.35  root     3075:                                                mem[0x417] &= ~0x20;
                   3076:                                        }
                   3077:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
                   3078:                                                mem[0x417] |= 0x10;
                   3079:                                        } else {
                   3080:                                                mem[0x417] &= ~0x10;
1.1.1.33  root     3081:                                        }
                   3082:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43  root     3083:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3084:                                                        mouse.status_alt |= 0x80;
                   3085:                                                }
1.1.1.33  root     3086:                                                mem[0x417] |= 0x08;
                   3087:                                        } else {
                   3088:                                                mem[0x417] &= ~0x08;
                   3089:                                        }
1.1.1.35  root     3090:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43  root     3091:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3092:                                                        mouse.status_alt |= 0x40;
                   3093:                                                }
1.1.1.35  root     3094:                                                mem[0x417] |= 0x04;
1.1.1.33  root     3095:                                        } else {
1.1.1.35  root     3096:                                                mem[0x417] &= ~0x04;
1.1.1.33  root     3097:                                        }
                   3098:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43  root     3099:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3100:                                                        mouse.status_alt |= 0x20;
                   3101:                                                }
1.1.1.33  root     3102:                                                if(!(mem[0x417] & 0x03)) {
                   3103:                                                        mem[0x417] |= 0x02; // left shift
                   3104:                                                }
                   3105:                                        } else {
                   3106:                                                mem[0x417] &= ~0x03;
                   3107:                                        }
1.1.1.35  root     3108:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3109:                                                mem[0x418] |= 0x02;
                   3110:                                        } else {
                   3111:                                                mem[0x418] &= ~0x02;
                   3112:                                        }
                   3113:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3114:                                                mem[0x418] |= 0x01;
                   3115:                                        } else {
                   3116:                                                mem[0x418] &= ~0x01;
                   3117:                                        }
1.1.1.33  root     3118:                                        
1.1.1.28  root     3119:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.24  root     3120:                                        kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3121:                                        kbd_status |= 1;
                   3122:                                        
                   3123:                                        // update dos key buffer
                   3124:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3125:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
                   3126:                                        
                   3127:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3128:                                                // make
1.1.1.24  root     3129:                                                kbd_data &= 0x7f;
                   3130:                                                
1.1.1.33  root     3131:                                                if(chr == 0x00) {
1.1.1.24  root     3132:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3133:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3134:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3135:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3136:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3137:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3138:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3139:                                                                } else if(scn == 0x35) {
                   3140:                                                                        scn = 0xa4;             // keypad /
                   3141:                                                                }
                   3142:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3143:                                                                if(scn == 0x07) {
                   3144:                                                                        chr = 0x1e;     // Ctrl+^
                   3145:                                                                } else if(scn == 0x0c) {
                   3146:                                                                        chr = 0x1f;     // Ctrl+_
                   3147:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3148:                                                                        static const UINT8 ctrl_map[] = {
                   3149:                                                                                0x95,   // keypad /
                   3150:                                                                                0,
                   3151:                                                                                0x96,   // keypad *
                   3152:                                                                                0, 0, 0,
                   3153:                                                                                0x5e,   // F1
                   3154:                                                                                0x5f,   // F2
                   3155:                                                                                0x60,   // F3
                   3156:                                                                                0x61,   // F4
                   3157:                                                                                0x62,   // F5
                   3158:                                                                                0x63,   // F6
                   3159:                                                                                0x64,   // F7
                   3160:                                                                                0x65,   // F8
                   3161:                                                                                0x66,   // F9
                   3162:                                                                                0x67,   // F10
                   3163:                                                                                0,
                   3164:                                                                                0,
                   3165:                                                                                0x77,   // Home
                   3166:                                                                                0x8d,   // Up
                   3167:                                                                                0x84,   // PgUp
                   3168:                                                                                0x8e,   // keypad -
                   3169:                                                                                0x73,   // Left
                   3170:                                                                                0x8f,   // keypad center
                   3171:                                                                                0x74,   // Right
                   3172:                                                                                0x90,   // keyapd +
                   3173:                                                                                0x75,   // End
                   3174:                                                                                0x91,   // Down
                   3175:                                                                                0x76,   // PgDn
                   3176:                                                                                0x92,   // Insert
                   3177:                                                                                0x93,   // Delete
                   3178:                                                                                0, 0, 0,
                   3179:                                                                                0x89,   // F11
                   3180:                                                                                0x8a,   // F12
                   3181:                                                                        };
                   3182:                                                                        scn = ctrl_map[scn - 0x35];
                   3183:                                                                }
                   3184:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3185:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3186:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3187:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3188:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3189:                                                                }
                   3190:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3191:                                                                scn += 0x85 - 0x57;
                   3192:                                                        }
                   3193:                                                        // ignore shift, ctrl, alt, win and menu keys
                   3194:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && (scn < 0x5b || scn > 0x5e)) {
1.1.1.32  root     3195:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3196: #ifdef USE_SERVICE_THREAD
                   3197:                                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3198: #endif
1.1.1.32  root     3199:                                                                        if(chr == 0) {
                   3200:                                                                                key_buf_char->write(0x00);
                   3201:                                                                                key_buf_scan->write(ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
                   3202:                                                                        }
                   3203:                                                                        key_buf_char->write(chr);
                   3204:                                                                        key_buf_scan->write(scn);
1.1.1.35  root     3205: #ifdef USE_SERVICE_THREAD
                   3206:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3207: #endif
1.1.1.24  root     3208:                                                                }
                   3209:                                                        }
                   3210:                                                } else {
                   3211:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3212:                                                                chr = 0;
                   3213:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3214:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3215:                                                                }
                   3216:                                                        }
1.1.1.32  root     3217:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3218: #ifdef USE_SERVICE_THREAD
                   3219:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3220: #endif
1.1.1.32  root     3221:                                                                key_buf_char->write(chr);
                   3222:                                                                key_buf_scan->write(scn);
1.1.1.35  root     3223: #ifdef USE_SERVICE_THREAD
                   3224:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3225: #endif
1.1.1.32  root     3226:                                                        }
1.1.1.24  root     3227:                                                }
1.1.1.33  root     3228:                                        } else if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
                   3229:                                                // ctrl-break, ctrl-c
                   3230:                                                if(scn == 0x46) {
                   3231:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3232: #ifdef USE_SERVICE_THREAD
                   3233:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3234: #endif
1.1.1.33  root     3235:                                                                key_buf_char->write(0x00);
                   3236:                                                                key_buf_scan->write(0x00);
1.1.1.35  root     3237: #ifdef USE_SERVICE_THREAD
                   3238:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3239: #endif
1.1.1.33  root     3240:                                                        }
                   3241:                                                        ctrl_break_pressed = true;
                   3242:                                                        mem[0x471] = 0x80;
                   3243:                                                        raise_int_1bh = true;
                   3244:                                                } else {
                   3245:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3246: #ifdef USE_SERVICE_THREAD
                   3247:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3248: #endif
1.1.1.33  root     3249:                                                                key_buf_char->write(chr);
                   3250:                                                                key_buf_scan->write(scn);
1.1.1.35  root     3251: #ifdef USE_SERVICE_THREAD
                   3252:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3253: #endif
1.1.1.33  root     3254:                                                        }
                   3255:                                                        ctrl_c_pressed = (scn == 0x2e);
                   3256:                                                }
                   3257:                                        } else {
                   3258:                                                // break
                   3259:                                                kbd_data |= 0x80;
1.1       root     3260:                                        }
1.1.1.24  root     3261:                                        result = key_changed = true;
1.1.1.36  root     3262:                                        // IME may be on and it may causes screen scroll up and cursor position change
                   3263:                                        cursor_moved = true;
1.1       root     3264:                                }
                   3265:                        }
                   3266:                }
                   3267:        }
1.1.1.35  root     3268: #ifdef USE_SERVICE_THREAD
                   3269:        LeaveCriticalSection(&input_crit_sect);
                   3270: #endif
1.1.1.24  root     3271:        return(result);
1.1.1.8   root     3272: }
                   3273: 
1.1.1.14  root     3274: bool update_key_buffer()
1.1.1.8   root     3275: {
1.1.1.35  root     3276:        if(update_console_input()) {
                   3277:                return(true);
                   3278:        }
                   3279:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3280: #ifdef USE_SERVICE_THREAD
                   3281:                EnterCriticalSection(&key_buf_crit_sect);
                   3282: #endif
                   3283:                bool empty = key_buf_char->empty();
                   3284: #ifdef USE_SERVICE_THREAD
                   3285:                LeaveCriticalSection(&key_buf_crit_sect);
                   3286: #endif
                   3287:                if(!empty) return(true);
                   3288:        }
                   3289:        return(false);
1.1.1.8   root     3290: }
                   3291: 
1.1.1.20  root     3292: /* ----------------------------------------------------------------------------
                   3293:        MS-DOS virtual machine
                   3294: ---------------------------------------------------------------------------- */
                   3295: 
1.1.1.32  root     3296: static const struct {
1.1.1.33  root     3297:        char *name;
                   3298:        DWORD lcid;
                   3299:        char *std;
                   3300:        char *dlt;
                   3301: } tz_table[] = {
                   3302:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3303: //     0       GMT             Greenwich Mean Time             GMT0
                   3304:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3305:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3306:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3307:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3308: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3309:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3310:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3311: //     3       BST             Brazil Standard Time            BST3
                   3312:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3313:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3314:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3315: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3316:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3317: //     3       GST             Greenland Standard Time         GST3
                   3318:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3319: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3320:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3321: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3322:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3323: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3324:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3325:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3326: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3327:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3328:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3329:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3330: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3331:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3332: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3333:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3334: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3335:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3336: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3337:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3338:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3339:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3340: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3341:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3342: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3343:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3344:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3345:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3346: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3347:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3348:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3349: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3350: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3351:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3352: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3353:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3354:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3355: //     11      SST             Samoa Standard Time             SST11
                   3356:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3357: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3358:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3359: //     -10     GST             Guam Standard Time              GST-10
                   3360:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3361: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3362:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3363:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3364:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3365: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3366:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3367:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3368: //     -9      JST             Japan Standard Time             JST-9
                   3369:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3370: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3371:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3372:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3373: //     -8      HKT             Hong Kong Time                  HKT-8
                   3374:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3375: //     -8      CCT             China Coast Time                CCT-8
                   3376:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3377:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3378: //     -8      SST             Singapore Standard Time         SST-8
                   3379:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3380: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3381:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3382:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3383: //     -7:30   JT              Java Standard Time              JST-7:30
                   3384: //     -7      NST             North Sumatra Time              NST-7
                   3385:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3386: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3387:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3388: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3389:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3390: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3391:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3392:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3393: //     -2      EET             Eastern Europe Time             EET-2
                   3394:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3395:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3396:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3397:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3398: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3399:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3400: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3401: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3402: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3403: //     -1      CET     CES     Central European Time           CET-1CES
                   3404:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3405:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3406:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3407:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3408: //     -1      WAT             West African Time               WAT-1
                   3409:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3410:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3411: //     0       UTC             Universal Coordinated Time      UTC0
                   3412:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3413:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3414:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3415:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3416:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3417:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3418: };
                   3419: 
                   3420: static const struct {
1.1.1.32  root     3421:        UINT16 code;
                   3422:        char *message_english;
                   3423:        char *message_japanese;
                   3424: } standard_error_table[] = {
                   3425:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3426:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3427:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3428:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3429:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3430:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3431:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3432:        {0x08,  "Insufficient memory", "������������܂���."},
                   3433:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3434:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3435:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3436:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3437:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3438:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3439:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3440:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3441:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3442:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3443:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3444:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3445:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3446:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3447:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3448:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3449:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3450:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3451:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3452:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3453:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3454:        {0x1F,  "General failure", "�G���[�ł�."},
                   3455:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3456:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3457:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3458:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3459:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3460:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3461:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3462:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3463: /*
                   3464:        {0x32,  "Network request not supported", NULL},
                   3465:        {0x33,  "Remote computer not listening", NULL},
                   3466:        {0x34,  "Duplicate name on network", NULL},
                   3467:        {0x35,  "Network name not found", NULL},
                   3468:        {0x36,  "Network busy", NULL},
                   3469:        {0x37,  "Network device no longer exists", NULL},
                   3470:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3471:        {0x39,  "Network adapter hardware error", NULL},
                   3472:        {0x3A,  "Incorrect response from network", NULL},
                   3473:        {0x3B,  "Unexpected network error", NULL},
                   3474:        {0x3C,  "Incompatible remote adapter", NULL},
                   3475:        {0x3D,  "Print queue full", NULL},
                   3476:        {0x3E,  "Queue not full", NULL},
                   3477:        {0x3F,  "Not enough space to print file", NULL},
                   3478:        {0x40,  "Network name was deleted", NULL},
                   3479:        {0x41,  "Network: Access denied", NULL},
                   3480:        {0x42,  "Network device type incorrect", NULL},
                   3481:        {0x43,  "Network name not found", NULL},
                   3482:        {0x44,  "Network name limit exceeded", NULL},
                   3483:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3484:        {0x46,  "Temporarily paused", NULL},
                   3485:        {0x47,  "Network request not accepted", NULL},
                   3486:        {0x48,  "Network print/disk redirection paused", NULL},
                   3487:        {0x49,  "Network software not installed", NULL},
                   3488:        {0x4A,  "Unexpected adapter close", NULL},
                   3489: */
                   3490:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3491:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3492:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3493:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3494:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3495:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3496:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3497:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3498:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3499:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
                   3500: /*
                   3501:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3502:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3503:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3504:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3505:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
                   3506: */
                   3507:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3508:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3509:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3510:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3511:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3512:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3513: };
                   3514: 
                   3515: static const struct {
                   3516:        UINT16 code;
                   3517:        char *message_english;
                   3518:        char *message_japanese;
                   3519: } param_error_table[] = {
                   3520:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3521:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3522:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3523:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3524:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3525:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3526:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3527:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3528:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3529:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3530:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3531: };
                   3532: 
                   3533: static const struct {
                   3534:        UINT16 code;
                   3535:        char *message_english;
                   3536:        char *message_japanese;
                   3537: } critical_error_table[] = {
                   3538:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3539:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3540:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3541:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3542:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3543:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3544:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3545:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3546:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3547:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3548:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3549:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3550:        {0x0C,  "General failure", "�G���[�ł�."},
                   3551:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3552:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3553:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3554:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3555:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3556:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3557:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3558:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3559:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3560: };
                   3561: 
1.1.1.20  root     3562: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3563: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3564: void msdos_putch(UINT8 data);
1.1.1.35  root     3565: #ifdef USE_SERVICE_THREAD
                   3566: void msdos_putch_tmp(UINT8 data);
                   3567: #endif
1.1.1.45! root     3568: const char *msdos_short_path(const char *path);
1.1.1.44  root     3569: bool msdos_is_valid_drive(int drv);
                   3570: bool msdos_is_removable_drive(int drv);
                   3571: bool msdos_is_cdrom_drive(int drv);
                   3572: bool msdos_is_remote_drive(int drv);
                   3573: bool msdos_is_subst_drive(int drv);
1.1.1.20  root     3574: 
1.1       root     3575: // process info
                   3576: 
                   3577: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3578: {
                   3579:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3580:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3581:                        memset(&process[i], 0, sizeof(process_t));
                   3582:                        process[i].psp = psp_seg;
                   3583:                        return(&process[i]);
                   3584:                }
                   3585:        }
                   3586:        fatalerror("too many processes\n");
                   3587:        return(NULL);
                   3588: }
                   3589: 
1.1.1.33  root     3590: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error)
1.1       root     3591: {
                   3592:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3593:                if(process[i].psp == psp_seg) {
                   3594:                        return(&process[i]);
                   3595:                }
                   3596:        }
1.1.1.33  root     3597:        if(show_error) {
                   3598:                fatalerror("invalid psp address\n");
                   3599:        }
1.1       root     3600:        return(NULL);
                   3601: }
                   3602: 
1.1.1.33  root     3603: process_t *msdos_process_info_get(UINT16 psp_seg)
                   3604: {
                   3605:        return(msdos_process_info_get(psp_seg, true));
                   3606: }
                   3607: 
1.1.1.23  root     3608: void msdos_sda_update(int psp_seg)
                   3609: {
                   3610:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3611:        
                   3612:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3613:                if(process[i].psp == psp_seg) {
                   3614:                        sda->switchar = process[i].switchar;
                   3615:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3616:                        sda->current_dta.w.h = process[i].dta.w.h;
                   3617:                        sda->current_psp = process[i].psp;
                   3618:                        break;
                   3619:                }
                   3620:        }
                   3621:        sda->malloc_strategy = malloc_strategy;
                   3622:        sda->return_code = retval;
                   3623:        sda->current_drive = _getdrive();
                   3624: }
                   3625: 
1.1.1.13  root     3626: // dta info
                   3627: 
                   3628: void msdos_dta_info_init()
                   3629: {
1.1.1.14  root     3630:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     3631:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3632:        }
                   3633: }
                   3634: 
                   3635: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   3636: {
                   3637:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     3638:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3639:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   3640:                        if(free_dta == NULL) {
1.1.1.13  root     3641:                                free_dta = &dtalist[i];
                   3642:                        }
1.1.1.14  root     3643:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     3644:                        return(&dtalist[i]);
                   3645:                }
                   3646:        }
1.1.1.14  root     3647:        if(free_dta) {
1.1.1.13  root     3648:                free_dta->psp = psp_seg;
                   3649:                free_dta->dta = dta_laddr;
                   3650:                return(free_dta);
                   3651:        }
                   3652:        fatalerror("too many dta\n");
                   3653:        return(NULL);
                   3654: }
                   3655: 
                   3656: void msdos_dta_info_free(UINT16 psp_seg)
                   3657: {
1.1.1.14  root     3658:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3659:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     3660:                        FindClose(dtalist[i].find_handle);
                   3661:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3662:                }
                   3663:        }
                   3664: }
                   3665: 
1.1       root     3666: void msdos_cds_update(int drv)
                   3667: {
1.1.1.44  root     3668:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1       root     3669:        
1.1.1.44  root     3670:        memset(cds, 0, 88);
                   3671:        
                   3672:        if(msdos_is_valid_drive(drv)) {
                   3673:                char path[MAX_PATH];
                   3674:                if(msdos_is_remote_drive(drv)) {
                   3675:                        cds->drive_attrib = 0xc000;     // network drive
                   3676:                } else if(msdos_is_subst_drive(drv)) {
                   3677:                        cds->drive_attrib = 0x5000;     // subst drive
                   3678:                } else {
                   3679:                        cds->drive_attrib = 0x4000;     // physical drive
                   3680:                }
                   3681:                if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
                   3682:                        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
                   3683:                }
                   3684:        }
                   3685:        if(cds->path_name[0] == '\0') {
                   3686:                sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   3687:        }
                   3688:        cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   3689:        cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
                   3690:        cds->word_1 = cds->word_2 = 0xffff;
                   3691:        cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
                   3692:        cds->bs_offset = 2;
                   3693: }
                   3694: 
1.1.1.45! root     3695: void msdos_cds_update(int drv, const char *path)
1.1.1.44  root     3696: {
                   3697:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
                   3698:        
                   3699:        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1       root     3700: }
                   3701: 
1.1.1.17  root     3702: // nls information tables
                   3703: 
                   3704: // uppercase table (func 6502h)
                   3705: void msdos_upper_table_update()
                   3706: {
                   3707:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3708:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3709:                UINT8 c[4];
1.1.1.33  root     3710:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3711:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     3712:                c[0] = 0x80 + i;
                   3713:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   3714:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3715:        }
                   3716: }
                   3717: 
1.1.1.23  root     3718: // lowercase table (func 6503h)
                   3719: void msdos_lower_table_update()
                   3720: {
                   3721:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   3722:        for(unsigned i = 0; i < 0x80; ++i) {
                   3723:                UINT8 c[4];
1.1.1.33  root     3724:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3725:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     3726:                c[0] = 0x80 + i;
                   3727:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   3728:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3729:        }
                   3730: }
                   3731: 
1.1.1.17  root     3732: // filename uppercase table (func 6504h)
                   3733: void msdos_filename_upper_table_init()
                   3734: {
                   3735:        // depended on (file)system, not on active codepage
                   3736:        // temporary solution: just filling data
                   3737:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3738:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3739:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   3740:        }
                   3741: }
                   3742: 
                   3743: // filaname terminator table (func 6505h)
                   3744: void msdos_filename_terminator_table_init()
                   3745: {
                   3746:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   3747:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   3748:        
                   3749:        data[2] = 1;            // marker? (permissible character value)
                   3750:        data[3] = 0x00;         // 00h...FFh
                   3751:        data[4] = 0xff;
                   3752:        data[5] = 0;            // marker? (excluded character)
                   3753:        data[6] = 0x00;         // 00h...20h
                   3754:        data[7] = 0x20;
                   3755:        data[8] = 2;            // marker? (illegal characters for filename)
                   3756:        data[9] = (UINT8)strlen(illegal_chars);
                   3757:        memcpy(data + 10, illegal_chars, data[9]);
                   3758:        
                   3759:        // total length
                   3760:        *(UINT16 *)data = (10 - 2) + data[9];
                   3761: }
                   3762: 
                   3763: // collating table (func 6506h)
                   3764: void msdos_collating_table_update()
                   3765: {
                   3766:        // temporary solution: just filling data
                   3767:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     3768:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     3769:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   3770:        }
                   3771: }
                   3772: 
1.1       root     3773: // dbcs
                   3774: 
                   3775: void msdos_dbcs_table_update()
                   3776: {
                   3777:        UINT8 dbcs_data[DBCS_SIZE];
                   3778:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   3779:        
                   3780:        CPINFO info;
                   3781:        GetCPInfo(active_code_page, &info);
                   3782:        
                   3783:        if(info.MaxCharSize != 1) {
                   3784:                for(int i = 0;; i += 2) {
                   3785:                        UINT8 lo = info.LeadByte[i + 0];
                   3786:                        UINT8 hi = info.LeadByte[i + 1];
                   3787:                        dbcs_data[2 + i + 0] = lo;
                   3788:                        dbcs_data[2 + i + 1] = hi;
                   3789:                        if(lo == 0 && hi == 0) {
                   3790:                                dbcs_data[0] = i + 2;
                   3791:                                break;
                   3792:                        }
                   3793:                }
                   3794:        } else {
                   3795:                dbcs_data[0] = 2;       // ???
                   3796:        }
                   3797:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   3798: }
                   3799: 
1.1.1.17  root     3800: void msdos_dbcs_table_finish()
                   3801: {
1.1.1.32  root     3802:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     3803:                _setmbcp(system_code_page);
                   3804:        }
1.1.1.32  root     3805:        if(console_code_page != GetConsoleCP()) {
                   3806:                SetConsoleCP(console_code_page);
                   3807:                SetConsoleOutputCP(console_code_page);
                   3808:        }
1.1.1.17  root     3809: }
                   3810: 
                   3811: void msdos_nls_tables_init()
1.1       root     3812: {
1.1.1.32  root     3813:        active_code_page = console_code_page = GetConsoleCP();
                   3814:        system_code_page = _getmbcp();
                   3815:        
                   3816:        if(active_code_page != system_code_page) {
                   3817:                if(_setmbcp(active_code_page) != 0) {
                   3818:                        active_code_page = system_code_page;
                   3819:                }
                   3820:        }
                   3821:        
1.1.1.17  root     3822:        msdos_upper_table_update();
1.1.1.23  root     3823:        msdos_lower_table_update();
1.1.1.17  root     3824:        msdos_filename_terminator_table_init();
                   3825:        msdos_filename_upper_table_init();
                   3826:        msdos_collating_table_update();
1.1       root     3827:        msdos_dbcs_table_update();
                   3828: }
                   3829: 
1.1.1.17  root     3830: void msdos_nls_tables_update()
1.1       root     3831: {
1.1.1.17  root     3832:        msdos_dbcs_table_update();
                   3833:        msdos_upper_table_update();
1.1.1.23  root     3834:        msdos_lower_table_update();
                   3835: //     msdos_collating_table_update();
1.1       root     3836: }
                   3837: 
                   3838: int msdos_lead_byte_check(UINT8 code)
                   3839: {
                   3840:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   3841:        
                   3842:        for(int i = 0;; i += 2) {
                   3843:                UINT8 lo = dbcs_table[i + 0];
                   3844:                UINT8 hi = dbcs_table[i + 1];
                   3845:                if(lo == 0 && hi == 0) {
                   3846:                        break;
                   3847:                }
                   3848:                if(lo <= code && code <= hi) {
                   3849:                        return(1);
                   3850:                }
                   3851:        }
                   3852:        return(0);
                   3853: }
                   3854: 
1.1.1.20  root     3855: int msdos_ctrl_code_check(UINT8 code)
                   3856: {
1.1.1.22  root     3857:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     3858: }
                   3859: 
1.1.1.36  root     3860: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
                   3861: {
                   3862:        int is_kanji_1st = 0;
                   3863:        int is_kanji_2nd = 0;
                   3864:        
                   3865:        for(int p = 0;; p++) {
                   3866:                if(is_kanji_1st) {
                   3867:                        is_kanji_1st = 0;
                   3868:                        is_kanji_2nd = 1;
                   3869:                } else if(msdos_lead_byte_check(buf[p])) {
                   3870:                        is_kanji_1st = 1;
                   3871:                }
                   3872:                if(p == n) {
                   3873:                        return(is_kanji_2nd);
                   3874:                }
                   3875:                is_kanji_2nd = 0;
                   3876:        }
                   3877: }
                   3878: 
1.1       root     3879: // file control
                   3880: 
1.1.1.45! root     3881: const char *msdos_remove_double_quote(const char *path)
1.1.1.14  root     3882: {
                   3883:        static char tmp[MAX_PATH];
                   3884:        
                   3885:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45! root     3886:                memset(tmp, 0, sizeof(tmp));
1.1.1.14  root     3887:                memcpy(tmp, path + 1, strlen(path) - 2);
                   3888:        } else {
                   3889:                strcpy(tmp, path);
                   3890:        }
                   3891:        return(tmp);
                   3892: }
                   3893: 
1.1.1.45! root     3894: const char *msdos_remove_end_separator(const char *path)
1.1.1.32  root     3895: {
                   3896:        static char tmp[MAX_PATH];
                   3897:        
                   3898:        strcpy(tmp, path);
1.1.1.45! root     3899:        
        !          3900:        // for example "C:\" case, the end separator should not be removed
        !          3901:        if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
        !          3902:                tmp[strlen(tmp) - 1] = '\0';
1.1.1.32  root     3903:        }
                   3904:        return(tmp);
                   3905: }
                   3906: 
1.1.1.45! root     3907: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14  root     3908: {
                   3909:        static char tmp[MAX_PATH];
1.1.1.45! root     3910:        const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14  root     3911:        
                   3912:        if(strlen(tmp_dir) == 0) {
                   3913:                strcpy(tmp, file);
                   3914:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   3915:                sprintf(tmp, "%s%s", tmp_dir, file);
                   3916:        } else {
                   3917:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   3918:        }
                   3919:        return(tmp);
                   3920: }
                   3921: 
1.1.1.45! root     3922: const char *msdos_trimmed_path(const char *path, int lfn)
1.1       root     3923: {
                   3924:        static char tmp[MAX_PATH];
                   3925:        
                   3926:        if(lfn) {
                   3927:                strcpy(tmp, path);
                   3928:        } else {
                   3929:                // remove space in the path
1.1.1.45! root     3930:                const char *src = path;
        !          3931:                char *dst = tmp;
1.1       root     3932:                
                   3933:                while(*src != '\0') {
                   3934:                        if(msdos_lead_byte_check(*src)) {
                   3935:                                *dst++ = *src++;
                   3936:                                *dst++ = *src++;
                   3937:                        } else if(*src != ' ') {
                   3938:                                *dst++ = *src++;
                   3939:                        } else {
                   3940:                                src++;  // skip space
                   3941:                        }
                   3942:                }
                   3943:                *dst = '\0';
                   3944:        }
1.1.1.14  root     3945:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   3946:                // redirect C:\COMMAND.COM to comspec_path
                   3947:                strcpy(tmp, comspec_path);
                   3948:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   3949:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   3950:                static int root_drive_protected = -1;
                   3951:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   3952:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   3953:                
                   3954:                if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
                   3955:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   3956:                        strcpy(name, name_temp);
                   3957:                        name_temp[0] = '\0';
                   3958:                        
                   3959:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   3960:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   3961:                                if(root_drive_protected == -1) {
                   3962:                                        FILE *fp = NULL;
                   3963:                                        
                   3964:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   3965:                                        root_drive_protected = 1;
                   3966:                                        try {
                   3967:                                                if((fp = fopen(temp, "w")) != NULL) {
                   3968:                                                        if(fprintf(fp, "TEST") == 4) {
                   3969:                                                                root_drive_protected = 0;
                   3970:                                                        }
                   3971:                                                }
                   3972:                                        } catch(...) {
                   3973:                                        }
                   3974:                                        if(fp != NULL) {
                   3975:                                                fclose(fp);
                   3976:                                        }
                   3977:                                        if(_access(temp, 0) == 0) {
                   3978:                                                remove(temp);
                   3979:                                        }
                   3980:                                }
                   3981:                                if(root_drive_protected == 1) {
                   3982:                                        if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
                   3983:                                           GetEnvironmentVariable("TMP",  temp, MAX_PATH) != 0) {
                   3984:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   3985:                                        }
                   3986:                                }
                   3987:                        }
                   3988:                }
                   3989:        }
1.1       root     3990:        return(tmp);
                   3991: }
                   3992: 
1.1.1.45! root     3993: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28  root     3994: {
1.1.1.32  root     3995:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     3996:        static char env_path[ENV_SIZE];
                   3997:        char tmp[ENV_SIZE], *token;
                   3998:        
                   3999:        memset(env_path, 0, sizeof(env_path));
                   4000:        strcpy(tmp, src);
                   4001:        token = my_strtok(tmp, ";");
                   4002:        
                   4003:        while(token != NULL) {
                   4004:                if(token[0] != '\0') {
1.1.1.45! root     4005:                        const char *path = msdos_remove_double_quote(token);
        !          4006:                        char short_path[MAX_PATH];
1.1.1.32  root     4007:                        if(path != NULL && strlen(path) != 0) {
                   4008:                                if(env_path[0] != '\0') {
                   4009:                                        strcat(env_path, ";");
                   4010:                                }
1.1.1.28  root     4011:                                if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     4012:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     4013:                                } else {
                   4014:                                        my_strupr(short_path);
1.1.1.32  root     4015:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     4016:                                }
                   4017:                        }
                   4018:                }
                   4019:                token = my_strtok(NULL, ";");
                   4020:        }
                   4021:        return(env_path);
                   4022: }
                   4023: 
1.1.1.45! root     4024: bool match(const char *text, const char *pattern)
1.1       root     4025: {
1.1.1.24  root     4026:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     4027:        switch(*pattern) {
1.1       root     4028:        case '\0':
                   4029:                return !*text;
                   4030:        case '*':
1.1.1.14  root     4031:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     4032:        case '?':
                   4033:                return *text && match(text + 1, pattern + 1);
                   4034:        default:
                   4035:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   4036:        }
                   4037: }
                   4038: 
1.1.1.45! root     4039: bool msdos_match_volume_label(const char *path, const char *volume)
1.1       root     4040: {
1.1.1.45! root     4041:        const char *p = NULL;
1.1       root     4042:        
1.1.1.14  root     4043:        if(!*volume) {
                   4044:                return false;
                   4045:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     4046:                return msdos_match_volume_label(p + 1, volume);
                   4047:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   4048:                return msdos_match_volume_label(p + 1, volume);
                   4049:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     4050:                char tmp[MAX_PATH];
                   4051:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   4052:                return match(volume, tmp);
1.1       root     4053:        } else {
                   4054:                return match(volume, path);
                   4055:        }
                   4056: }
                   4057: 
1.1.1.45! root     4058: const char *msdos_fcb_path(fcb_t *fcb)
1.1       root     4059: {
                   4060:        static char tmp[MAX_PATH];
                   4061:        char name[9], ext[4];
                   4062:        
                   4063:        memset(name, 0, sizeof(name));
                   4064:        memcpy(name, fcb->file_name, 8);
                   4065:        strcpy(name, msdos_trimmed_path(name, 0));
                   4066:        
                   4067:        memset(ext, 0, sizeof(ext));
                   4068:        memcpy(ext, fcb->file_name + 8, 3);
                   4069:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4070:        
                   4071:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4072:                strcpy(name, "*");
                   4073:        }
                   4074:        if(ext[0] == '\0') {
                   4075:                strcpy(tmp, name);
                   4076:        } else {
                   4077:                if(strcmp(ext, "???") == 0) {
                   4078:                        strcpy(ext, "*");
                   4079:                }
                   4080:                sprintf(tmp, "%s.%s", name, ext);
                   4081:        }
                   4082:        return(tmp);
                   4083: }
                   4084: 
1.1.1.45! root     4085: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1       root     4086: {
                   4087:        char *ext = my_strchr(path, '.');
                   4088:        
                   4089:        memset(fcb->file_name, 0x20, 8 + 3);
                   4090:        if(ext != NULL && path[0] != '.') {
                   4091:                *ext = '\0';
                   4092:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4093:        }
                   4094:        memcpy(fcb->file_name, path, strlen(path));
                   4095: }
                   4096: 
1.1.1.45! root     4097: const char *msdos_short_path(const char *path)
1.1       root     4098: {
                   4099:        static char tmp[MAX_PATH];
                   4100:        
1.1.1.24  root     4101:        if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
                   4102:                strcpy(tmp, path);
                   4103:        }
1.1       root     4104:        my_strupr(tmp);
                   4105:        return(tmp);
                   4106: }
                   4107: 
1.1.1.45! root     4108: const char *msdos_short_name(WIN32_FIND_DATA *fd)
1.1.1.13  root     4109: {
                   4110:        static char tmp[MAX_PATH];
1.1.1.45! root     4111:        
1.1.1.14  root     4112:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4113:                strcpy(tmp, fd->cAlternateFileName);
                   4114:        } else {
                   4115:                strcpy(tmp, fd->cFileName);
                   4116:        }
                   4117:        my_strupr(tmp);
                   4118:        return(tmp);
                   4119: }
                   4120: 
1.1.1.45! root     4121: const char *msdos_short_full_path(const char *path)
1.1       root     4122: {
                   4123:        static char tmp[MAX_PATH];
                   4124:        char full[MAX_PATH], *name;
                   4125:        
1.1.1.14  root     4126:        // Full works with non-existent files, but Short does not
1.1       root     4127:        GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14  root     4128:        *tmp = '\0';
                   4129:        if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
                   4130:                name[-1] = '\0';
                   4131:                DWORD len = GetShortPathName(full, tmp, MAX_PATH);
                   4132:                if(len == 0) {
                   4133:                        strcpy(tmp, full);
                   4134:                } else {
                   4135:                        tmp[len++] = '\\';
                   4136:                        strcpy(tmp + len, name);
                   4137:                }
                   4138:        }
1.1       root     4139:        my_strupr(tmp);
                   4140:        return(tmp);
                   4141: }
                   4142: 
1.1.1.45! root     4143: const char *msdos_short_full_dir(const char *path)
1.1       root     4144: {
                   4145:        static char tmp[MAX_PATH];
                   4146:        char full[MAX_PATH], *name;
                   4147:        
                   4148:        GetFullPathName(path, MAX_PATH, full, &name);
                   4149:        name[-1] = '\0';
1.1.1.24  root     4150:        if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
                   4151:                strcpy(tmp, full);
                   4152:        }
1.1       root     4153:        my_strupr(tmp);
                   4154:        return(tmp);
                   4155: }
                   4156: 
1.1.1.45! root     4157: const char *msdos_local_file_path(const char *path, int lfn)
1.1       root     4158: {
1.1.1.45! root     4159:        static char trimmed[MAX_PATH];
        !          4160:        
        !          4161:        strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14  root     4162: #if 0
                   4163:        // I have forgotten the reason of this routine... :-(
1.1       root     4164:        if(_access(trimmed, 0) != 0) {
                   4165:                process_t *process = msdos_process_info_get(current_psp);
                   4166:                static char tmp[MAX_PATH];
                   4167:                
                   4168:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4169:                if(_access(tmp, 0) == 0) {
                   4170:                        return(tmp);
                   4171:                }
                   4172:        }
1.1.1.14  root     4173: #endif
1.1       root     4174:        return(trimmed);
                   4175: }
                   4176: 
1.1.1.45! root     4177: bool msdos_is_device_path(const char *path)
1.1.1.11  root     4178: {
                   4179:        char full[MAX_PATH], *name;
                   4180:        
1.1.1.24  root     4181:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4182:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4183:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4184:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4185:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4186:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4187:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4188:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4189:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4190:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4191:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4192:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4193:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4194:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4195:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4196:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4197:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4198:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4199:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4200:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4201:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4202:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4203:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4204:                        return(true);
                   4205:                } else if(name != NULL) {
                   4206:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4207:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4208:                           _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43  root     4209: //                        _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30  root     4210:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4211:                                return(true);
                   4212:                        }
                   4213:                }
1.1.1.24  root     4214:        }
                   4215:        return(false);
1.1.1.11  root     4216: }
                   4217: 
1.1.1.45! root     4218: bool msdos_is_con_path(const char *path)
1.1.1.8   root     4219: {
1.1.1.14  root     4220:        char full[MAX_PATH], *name;
1.1.1.8   root     4221:        
1.1.1.24  root     4222:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4223:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4224:        }
                   4225:        return(false);
                   4226: }
                   4227: 
1.1.1.45! root     4228: int msdos_is_comm_path(const char *path)
1.1.1.24  root     4229: {
                   4230:        char full[MAX_PATH], *name;
                   4231:        
                   4232:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4233:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4234:                        return(1);
                   4235:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4236:                        return(2);
                   4237:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4238:                        return(3);
                   4239:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4240:                        return(4);
1.1.1.24  root     4241:                }
                   4242:        }
1.1.1.29  root     4243:        return(0);
                   4244: }
                   4245: 
1.1.1.45! root     4246: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37  root     4247: {
                   4248:        // 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     4249:        const char *p = NULL;
1.1.1.37  root     4250:        
                   4251:        if((p = strstr(path, ":")) != NULL) {
                   4252:                UINT8 selector = sio_read(sio_port - 1, 3);
                   4253:                
                   4254:                // baud rate
                   4255:                int baud = max(110, min(9600, atoi(p + 1)));
                   4256:                UINT16 divisor = 115200 / baud;
                   4257:                
                   4258:                if((p = strstr(p + 1, ",")) != NULL) {
                   4259:                        // parity
                   4260:                        if(p[1] == 'N' || p[1] == 'n') {
                   4261:                                selector = (selector & ~0x38) | 0x00;
                   4262:                        } else if(p[1] == 'O' || p[1] == 'o') {
                   4263:                                selector = (selector & ~0x38) | 0x08;
                   4264:                        } else if(p[1] == 'E' || p[1] == 'e') {
                   4265:                                selector = (selector & ~0x38) | 0x18;
                   4266:                        } else if(p[1] == 'M' || p[1] == 'm') {
                   4267:                                selector = (selector & ~0x38) | 0x28;
                   4268:                        } else if(p[1] == 'S' || p[1] == 's') {
                   4269:                                selector = (selector & ~0x38) | 0x38;
                   4270:                        }
                   4271:                        if((p = strstr(p + 1, ",")) != NULL) {
                   4272:                                // word length
                   4273:                                if(p[1] == '8') {
                   4274:                                        selector = (selector & ~0x03) | 0x03;
                   4275:                                } else if(p[1] == '7') {
                   4276:                                        selector = (selector & ~0x03) | 0x02;
                   4277:                                } else if(p[1] == '6') {
                   4278:                                        selector = (selector & ~0x03) | 0x01;
                   4279:                                } else if(p[1] == '5') {
                   4280:                                        selector = (selector & ~0x03) | 0x00;
                   4281:                                }
                   4282:                                if((p = strstr(p + 1, ",")) != NULL) {
                   4283:                                        // stop bits
                   4284:                                        float bits = atof(p + 1);
                   4285:                                        if(bits > 1.0F) {
                   4286:                                                selector |= 0x04;
                   4287:                                        } else {
                   4288:                                                selector &= ~0x04;
                   4289:                                        }
                   4290:                                }
                   4291:                        }
                   4292:                }
                   4293:                sio_write(sio_port - 1, 3, selector | 0x80);
                   4294:                sio_write(sio_port - 1, 0, divisor & 0xff);
                   4295:                sio_write(sio_port - 1, 1, divisor >> 8);
                   4296:                sio_write(sio_port - 1, 3, selector);
                   4297:        }
                   4298: }
                   4299: 
1.1.1.45! root     4300: int msdos_is_prn_path(const char *path)
1.1.1.30  root     4301: {
                   4302:        char full[MAX_PATH], *name;
                   4303:        
                   4304:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4305:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4306:                        return(1);
                   4307:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4308:                        return(1);
                   4309:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4310:                        return(2);
                   4311:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4312:                        return(3);
                   4313:                }
                   4314:        }
                   4315:        return(0);
                   4316: }
                   4317: 
1.1.1.44  root     4318: bool msdos_is_valid_drive(int drv)
                   4319: {
                   4320:        return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
                   4321: }
                   4322: 
                   4323: bool msdos_is_removable_drive(int drv)
                   4324: {
                   4325:        char volume[] = "A:\\";
                   4326:        
                   4327:        volume[0] = 'A' + drv;
                   4328:        
                   4329:        return(GetDriveType(volume) == DRIVE_REMOVABLE);
                   4330: }
                   4331: 
                   4332: bool msdos_is_cdrom_drive(int drv)
                   4333: {
                   4334:        char volume[] = "A:\\";
                   4335:        
                   4336:        volume[0] = 'A' + drv;
                   4337:        
                   4338:        return(GetDriveType(volume) == DRIVE_CDROM);
                   4339: }
                   4340: 
                   4341: bool msdos_is_remote_drive(int drv)
                   4342: {
                   4343:        char volume[] = "A:\\";
                   4344:        
                   4345:        volume[0] = 'A' + drv;
                   4346:        
                   4347:        return(GetDriveType(volume) == DRIVE_REMOTE);
                   4348: }
                   4349: 
                   4350: bool msdos_is_subst_drive(int drv)
                   4351: {
                   4352:        char device[] = "A:", path[MAX_PATH];
                   4353:        
                   4354:        device[0] = 'A' + drv;
                   4355:        
                   4356:        if(QueryDosDevice(device, path, MAX_PATH)) {
                   4357:                if(strncmp(path, "\\??\\", 4) == 0) {
                   4358:                        return(true);
                   4359:                }
                   4360:        }
                   4361:        return(false);
                   4362: }
                   4363: 
1.1.1.45! root     4364: bool msdos_is_existing_file(const char *path)
1.1.1.24  root     4365: {
                   4366:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   4367:        WIN32_FIND_DATA FindData;
                   4368:        HANDLE hFind;
                   4369:        
                   4370:        if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
                   4371:                FindClose(hFind);
                   4372:                return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
                   4373:        }
                   4374:        return(false);
1.1.1.8   root     4375: }
                   4376: 
1.1.1.45! root     4377: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9   root     4378: {
                   4379:        static char tmp[MAX_PATH];
1.1.1.28  root     4380:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4381:        
1.1.1.28  root     4382:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.9   root     4383:        if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
                   4384:                sprintf(file_name, "COMMAND.COM");
                   4385:                if(_access(tmp, 0) == 0) {
                   4386:                        return(tmp);
                   4387:                }
                   4388:        }
1.1.1.28  root     4389:        
                   4390:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.9   root     4391:        if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
                   4392:                sprintf(file_name, "COMMAND.COM");
                   4393:                if(_access(tmp, 0) == 0) {
                   4394:                        return(tmp);
                   4395:                }
                   4396:        }
1.1.1.28  root     4397:        
                   4398:        // check if COMMAND.COM is in the current directory
1.1.1.9   root     4399:        if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
                   4400:                if(_access(tmp, 0) == 0) {
                   4401:                        return(tmp);
                   4402:                }
                   4403:        }
1.1.1.28  root     4404:        
                   4405:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4406:        strcpy(path, env_path);
                   4407:        char *token = my_strtok(path, ";");
1.1.1.9   root     4408:        while(token != NULL) {
1.1.1.14  root     4409:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4410:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4411:                        if(_access(tmp, 0) == 0) {
                   4412:                                return(tmp);
                   4413:                        }
                   4414:                }
                   4415:                token = my_strtok(NULL, ";");
                   4416:        }
                   4417:        return(NULL);
                   4418: }
                   4419: 
1.1.1.14  root     4420: int msdos_drive_number(const char *path)
1.1       root     4421: {
                   4422:        char tmp[MAX_PATH], *name;
                   4423:        
1.1.1.45! root     4424:        if(GetFullPathName(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
        !          4425:                if(tmp[0] >= 'a' && tmp[0] <= 'z') {
        !          4426:                        return(tmp[0] - 'a');
        !          4427:                } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
        !          4428:                        return(tmp[0] - 'A');
        !          4429:                }
1.1       root     4430:        }
1.1.1.45! root     4431: //     return(msdos_drive_number("."));
        !          4432:        return(_getdrive() - 1);
1.1       root     4433: }
                   4434: 
1.1.1.45! root     4435: const char *msdos_volume_label(const char *path)
1.1       root     4436: {
                   4437:        static char tmp[MAX_PATH];
                   4438:        char volume[] = "A:\\";
                   4439:        
                   4440:        if(path[1] == ':') {
                   4441:                volume[0] = path[0];
                   4442:        } else {
                   4443:                volume[0] = 'A' + _getdrive() - 1;
                   4444:        }
                   4445:        if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
                   4446:                memset(tmp, 0, sizeof(tmp));
                   4447:        }
                   4448:        return(tmp);
                   4449: }
                   4450: 
1.1.1.45! root     4451: const char *msdos_short_volume_label(const char *label)
1.1       root     4452: {
                   4453:        static char tmp[(8 + 1 + 3) + 1];
1.1.1.45! root     4454:        const char *src = label;
1.1       root     4455:        int remain = strlen(label);
                   4456:        char *dst_n = tmp;
                   4457:        char *dst_e = tmp + 9;
                   4458:        
                   4459:        strcpy(tmp, "        .   ");
                   4460:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4461:                if(msdos_lead_byte_check(*src)) {
                   4462:                        if(++i == 8) {
                   4463:                                break;
                   4464:                        }
                   4465:                        *dst_n++ = *src++;
                   4466:                        remain--;
                   4467:                }
                   4468:                *dst_n++ = *src++;
                   4469:                remain--;
                   4470:        }
                   4471:        if(remain > 0) {
                   4472:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4473:                        if(msdos_lead_byte_check(*src)) {
                   4474:                                if(++i == 3) {
                   4475:                                        break;
                   4476:                                }
                   4477:                                *dst_e++ = *src++;
                   4478:                                remain--;
                   4479:                        }
                   4480:                        *dst_e++ = *src++;
                   4481:                        remain--;
                   4482:                }
                   4483:                *dst_e = '\0';
                   4484:        } else {
                   4485:                *dst_n = '\0';
                   4486:        }
                   4487:        my_strupr(tmp);
                   4488:        return(tmp);
                   4489: }
                   4490: 
1.1.1.13  root     4491: errno_t msdos_maperr(unsigned long oserrno)
                   4492: {
                   4493:        _doserrno = oserrno;
1.1.1.14  root     4494:        switch(oserrno) {
1.1.1.13  root     4495:        case ERROR_FILE_NOT_FOUND:         // 2
                   4496:        case ERROR_PATH_NOT_FOUND:         // 3
                   4497:        case ERROR_INVALID_DRIVE:          // 15
                   4498:        case ERROR_NO_MORE_FILES:          // 18
                   4499:        case ERROR_BAD_NETPATH:            // 53
                   4500:        case ERROR_BAD_NET_NAME:           // 67
                   4501:        case ERROR_BAD_PATHNAME:           // 161
                   4502:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4503:                return ENOENT;
                   4504:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4505:                return EMFILE;
                   4506:        case ERROR_ACCESS_DENIED:          // 5
                   4507:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4508:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4509:        case ERROR_CANNOT_MAKE:            // 82
                   4510:        case ERROR_FAIL_I24:               // 83
                   4511:        case ERROR_DRIVE_LOCKED:           // 108
                   4512:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4513:        case ERROR_NOT_LOCKED:             // 158
                   4514:        case ERROR_LOCK_FAILED:            // 167
                   4515:                return EACCES;
                   4516:        case ERROR_INVALID_HANDLE:         // 6
                   4517:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4518:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4519:                return EBADF;
                   4520:        case ERROR_ARENA_TRASHED:          // 7
                   4521:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4522:        case ERROR_INVALID_BLOCK:          // 9
                   4523:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4524:                return ENOMEM;
                   4525:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4526:                return E2BIG;
                   4527:        case ERROR_BAD_FORMAT:             // 11
                   4528:                return ENOEXEC;
                   4529:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4530:                return EXDEV;
                   4531:        case ERROR_FILE_EXISTS:            // 80
                   4532:        case ERROR_ALREADY_EXISTS:         // 183
                   4533:                return EEXIST;
                   4534:        case ERROR_NO_PROC_SLOTS:          // 89
                   4535:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4536:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4537:                return EAGAIN;
                   4538:        case ERROR_BROKEN_PIPE:            // 109
                   4539:                return EPIPE;
                   4540:        case ERROR_DISK_FULL:              // 112
                   4541:                return ENOSPC;
                   4542:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4543:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4544:                return ECHILD;
                   4545:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4546:                return ENOTEMPTY;
                   4547:        }
1.1.1.14  root     4548:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4549:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4550:                return EACCES;
                   4551:        }
1.1.1.14  root     4552:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4553:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4554:                return ENOEXEC;
                   4555:        }
                   4556:        return EINVAL;
                   4557: }
                   4558: 
1.1.1.45! root     4559: int msdos_open(const char *path, int oflag)
1.1.1.13  root     4560: {
1.1.1.14  root     4561:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45! root     4562:                return(_open(path, oflag));
1.1.1.13  root     4563:        }
1.1.1.14  root     4564:        
                   4565:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4566:        DWORD disposition;
1.1.1.14  root     4567:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4568:        default:
1.1.1.13  root     4569:        case _O_EXCL:
                   4570:                disposition = OPEN_EXISTING;
                   4571:                break;
                   4572:        case _O_CREAT:
                   4573:                disposition = OPEN_ALWAYS;
                   4574:                break;
                   4575:        case _O_CREAT | _O_EXCL:
                   4576:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4577:                disposition = CREATE_NEW;
                   4578:                break;
                   4579:        case _O_TRUNC:
                   4580:        case _O_TRUNC | _O_EXCL:
                   4581:                disposition = TRUNCATE_EXISTING;
                   4582:                break;
                   4583:        case _O_CREAT | _O_TRUNC:
                   4584:                disposition = CREATE_ALWAYS;
                   4585:                break;
                   4586:        }
1.1.1.14  root     4587:        
1.1.1.45! root     4588:        HANDLE h = CreateFile(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
1.1.1.13  root     4589:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4590:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4591:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4592:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4593:                // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.45! root     4594:                h = CreateFile(path, GENERIC_READ,
1.1.1.13  root     4595:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4596:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4597:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4598:                        errno = msdos_maperr(GetLastError());
1.1.1.45! root     4599:                        return(-1);
1.1.1.13  root     4600:                }
                   4601:        }
1.1.1.14  root     4602:        
1.1.1.13  root     4603:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     4604:        if(fd == -1) {
1.1.1.13  root     4605:                CloseHandle(h);
                   4606:        }
1.1.1.45! root     4607:        return(fd);
        !          4608: }
        !          4609: 
        !          4610: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
        !          4611: {
        !          4612:        int fd = -1;
        !          4613:        
        !          4614:        *sio_port = *lpt_port = 0;
        !          4615:        
        !          4616:        if(msdos_is_con_path(path)) {
        !          4617:                // MODE.COM opens CON device with read/write mode :-(
        !          4618:                if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
        !          4619:                        oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
        !          4620:                        oflag |= _O_RDONLY;
        !          4621:                }
        !          4622:                if((fd = msdos_open("CON", oflag)) == -1) {
        !          4623: //                     fd = msdos_open("NUL", oflag);
        !          4624:                }
        !          4625:        } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
        !          4626:                fd = msdos_open("NUL", oflag);
        !          4627:                msdos_set_comm_params(*sio_port, path);
        !          4628:        } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
        !          4629:                fd = msdos_open("NUL", oflag);
        !          4630:        } else if(msdos_is_device_path(path)) {
        !          4631:                fd = msdos_open("NUL", oflag);
        !          4632: //     } else if(oflag & _O_CREAT) {
        !          4633: //             fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
        !          4634: //     } else {
        !          4635: //             fd = _open(path, oflag);
        !          4636:        }
        !          4637:        return(fd);
        !          4638: }
        !          4639: 
        !          4640: UINT16 msdos_device_info(const char *path)
        !          4641: {
        !          4642:        if(msdos_is_con_path(path)) {
        !          4643:                return(0x80d3);
        !          4644:        } else if(msdos_is_comm_path(path)) {
        !          4645:                return(0x80a0);
        !          4646:        } else if(msdos_is_prn_path(path)) {
        !          4647: //             return(0xa8c0);
        !          4648:                return(0x80a0);
        !          4649:        } else if(msdos_is_device_path(path)) {
        !          4650:                if(strstr(path, "EMMXXXX0") != NULL) {
        !          4651:                        return(0xc0c0);
        !          4652:                } else if(strstr(path, "MSCD001") != NULL) {
        !          4653:                        return(0xc880);
        !          4654:                } else {
        !          4655:                        return(0x8084);
        !          4656:                }
        !          4657:        } else {
        !          4658:                return(msdos_drive_number(path));
        !          4659:        }
1.1.1.13  root     4660: }
                   4661: 
1.1.1.37  root     4662: 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     4663: {
                   4664:        static int id = 0;
                   4665:        char full[MAX_PATH], *name;
                   4666:        
                   4667:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4668:                strcpy(file_handler[fd].path, full);
                   4669:        } else {
                   4670:                strcpy(file_handler[fd].path, path);
                   4671:        }
1.1.1.14  root     4672:        // isatty makes no distinction between CON & NUL
                   4673:        // GetFileSize fails on CON, succeeds on NUL
                   4674:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45! root     4675:                if(info == 0x80d3) {
        !          4676:                        info = 0x8084;
        !          4677:                }
1.1.1.14  root     4678:                atty = 0;
                   4679:        } else if(!atty && info == 0x80d3) {
1.1.1.45! root     4680: //             info = msdos_drive_number(".");
        !          4681:                info = msdos_drive_number(path);
1.1.1.14  root     4682:        }
1.1       root     4683:        file_handler[fd].valid = 1;
                   4684:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37  root     4685:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     4686:        file_handler[fd].mode = mode;
                   4687:        file_handler[fd].info = info;
                   4688:        file_handler[fd].psp = psp_seg;
1.1.1.37  root     4689:        file_handler[fd].sio_port = sio_port;
                   4690:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     4691:        
                   4692:        // init system file table
                   4693:        if(fd < 20) {
                   4694:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   4695:                
                   4696:                memset(sft, 0, 0x3b);
                   4697:                
                   4698:                *(UINT16 *)(sft + 0x00) = 1;
                   4699:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
                   4700:                *(UINT8  *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
                   4701:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   4702:                
                   4703:                if(!(file_handler[fd].info & 0x80)) {
                   4704:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   4705:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   4706:                        
                   4707:                        FILETIME time, local;
                   4708:                        HANDLE hHandle;
                   4709:                        WORD dos_date = 0, dos_time = 0;
                   4710:                        DWORD file_size = 0;
                   4711:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   4712:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   4713:                                        FileTimeToLocalFileTime(&time, &local);
                   4714:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   4715:                                }
                   4716:                                file_size = GetFileSize(hHandle, NULL);
                   4717:                        }
                   4718:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   4719:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   4720:                        *(UINT32 *)(sft + 0x11) = file_size;
                   4721:                }
                   4722:                
                   4723:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   4724:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   4725:                my_strupr(fname);
                   4726:                my_strupr(ext);
                   4727:                memset(sft + 0x20, 0x20, 11);
                   4728:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   4729:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   4730:                
                   4731:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   4732:        }
1.1       root     4733: }
                   4734: 
1.1.1.37  root     4735: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg)
                   4736: {
                   4737:         msdos_file_handler_open(fd, path, atty, mode, info, psp_seg, 0, 0);
                   4738: }
                   4739: 
1.1       root     4740: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   4741: {
                   4742:        strcpy(file_handler[dst].path, file_handler[src].path);
                   4743:        file_handler[dst].valid = 1;
                   4744:        file_handler[dst].id = file_handler[src].id;
                   4745:        file_handler[dst].atty = file_handler[src].atty;
                   4746:        file_handler[dst].mode = file_handler[src].mode;
                   4747:        file_handler[dst].info = file_handler[src].info;
                   4748:        file_handler[dst].psp = psp_seg;
1.1.1.37  root     4749:        file_handler[dst].sio_port = file_handler[src].sio_port;
                   4750:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     4751: }
                   4752: 
1.1.1.20  root     4753: void msdos_file_handler_close(int fd)
1.1       root     4754: {
                   4755:        file_handler[fd].valid = 0;
1.1.1.21  root     4756:        
                   4757:        if(fd < 20) {
                   4758:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   4759:        }
1.1       root     4760: }
                   4761: 
1.1.1.14  root     4762: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     4763: {
1.1.1.14  root     4764:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   4765:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   4766:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     4767: }
                   4768: 
                   4769: // find file
                   4770: 
                   4771: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   4772: {
                   4773:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4774:                return(0);      // search directory only !!!
                   4775:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   4776:                return(0);
                   4777:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   4778:                return(0);
                   4779:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4780:                return(0);
                   4781:        } else if((attribute & required_mask) != required_mask) {
                   4782:                return(0);
                   4783:        } else {
                   4784:                return(1);
                   4785:        }
                   4786: }
                   4787: 
1.1.1.13  root     4788: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
                   4789: {
1.1.1.14  root     4790:        if(fd->cAlternateFileName[0]) {
1.1.1.42  root     4791:                return(1);
1.1.1.13  root     4792:        }
                   4793:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     4794:        if(len > 12) {
1.1.1.42  root     4795:                return(0);
1.1.1.13  root     4796:        }
                   4797:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     4798:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42  root     4799:                return(0);
1.1.1.13  root     4800:        }
1.1.1.42  root     4801:        return(1);
1.1.1.13  root     4802: }
                   4803: 
1.1       root     4804: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
                   4805: {
                   4806:        FILETIME local;
                   4807:        
                   4808:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   4809:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   4810:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   4811:        
                   4812:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   4813:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   4814:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   4815:        
                   4816:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   4817:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   4818:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   4819: }
                   4820: 
                   4821: // i/o
                   4822: 
                   4823: void msdos_stdio_reopen()
                   4824: {
                   4825:        if(!file_handler[0].valid) {
                   4826:                _dup2(DUP_STDIN, 0);
                   4827:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   4828:        }
                   4829:        if(!file_handler[1].valid) {
                   4830:                _dup2(DUP_STDOUT, 1);
                   4831:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   4832:        }
                   4833:        if(!file_handler[2].valid) {
                   4834:                _dup2(DUP_STDERR, 2);
                   4835:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   4836:        }
1.1.1.21  root     4837:        if(!file_handler[3].valid) {
                   4838:                _dup2(DUP_STDAUX, 3);
                   4839:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   4840:        }
                   4841:        if(!file_handler[4].valid) {
                   4842:                _dup2(DUP_STDPRN, 4);
1.1.1.45! root     4843: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
        !          4844:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     4845:        }
                   4846:        for(int i = 0; i < 5; i++) {
                   4847:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   4848:                        msdos_psp_set_file_table(i, i, current_psp);
                   4849:                }
                   4850:        }
1.1       root     4851: }
                   4852: 
1.1.1.37  root     4853: int msdos_read(int fd, void *buffer, unsigned int count)
                   4854: {
                   4855:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   4856:                // read from serial port
                   4857:                int read = 0;
                   4858:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   4859:                        UINT8 *buf = (UINT8 *)buffer;
                   4860:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   4861:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     4862:                        DWORD timeout = timeGetTime() + 1000;
                   4863:                        while(read < count) {
                   4864:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
                   4865:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
                   4866:                                        timeout = timeGetTime() + 1000;
                   4867:                                } else {
                   4868:                                        if(timeGetTime() > timeout) {
                   4869:                                                break;
                   4870:                                        }
                   4871:                                        Sleep(10);
1.1.1.37  root     4872:                                }
                   4873:                        }
                   4874:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   4875:                }
                   4876:                return(read);
                   4877:        }
                   4878:        return(_read(fd, buffer, count));
                   4879: }
                   4880: 
1.1       root     4881: int msdos_kbhit()
                   4882: {
                   4883:        msdos_stdio_reopen();
                   4884:        
1.1.1.20  root     4885:        process_t *process = msdos_process_info_get(current_psp);
                   4886:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4887:        
                   4888:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4889:                // stdin is redirected to file
1.1.1.20  root     4890:                return(eof(fd) == 0);
1.1       root     4891:        }
                   4892:        
                   4893:        // check keyboard status
1.1.1.35  root     4894:        if(key_recv != 0) {
1.1       root     4895:                return(1);
                   4896:        }
1.1.1.35  root     4897:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4898: #ifdef USE_SERVICE_THREAD
                   4899:                EnterCriticalSection(&key_buf_crit_sect);
                   4900: #endif
                   4901:                bool empty = key_buf_char->empty();
                   4902: #ifdef USE_SERVICE_THREAD
                   4903:                LeaveCriticalSection(&key_buf_crit_sect);
                   4904: #endif
                   4905:                if(!empty) return(1);
                   4906:        }
                   4907:        return(_kbhit());
1.1       root     4908: }
                   4909: 
                   4910: int msdos_getch_ex(int echo)
                   4911: {
                   4912:        static char prev = 0;
                   4913:        
                   4914:        msdos_stdio_reopen();
                   4915:        
1.1.1.20  root     4916:        process_t *process = msdos_process_info_get(current_psp);
                   4917:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4918:        
                   4919:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4920:                // stdin is redirected to file
                   4921: retry:
                   4922:                char data;
1.1.1.37  root     4923:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     4924:                        char tmp = data;
                   4925:                        if(data == 0x0a) {
                   4926:                                if(prev == 0x0d) {
                   4927:                                        goto retry; // CRLF -> skip LF
                   4928:                                } else {
                   4929:                                        data = 0x0d; // LF only -> CR
                   4930:                                }
                   4931:                        }
                   4932:                        prev = tmp;
                   4933:                        return(data);
                   4934:                }
                   4935:                return(EOF);
                   4936:        }
                   4937:        
                   4938:        // input from console
1.1.1.5   root     4939:        int key_char, key_scan;
1.1.1.33  root     4940:        if(key_recv != 0) {
1.1.1.5   root     4941:                key_char = (key_code >> 0) & 0xff;
                   4942:                key_scan = (key_code >> 8) & 0xff;
                   4943:                key_code >>= 16;
1.1.1.33  root     4944:                key_recv >>= 16;
1.1.1.5   root     4945:        } else {
1.1.1.35  root     4946:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_halted) {
                   4947:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4948: #ifdef USE_SERVICE_THREAD
                   4949:                                EnterCriticalSection(&key_buf_crit_sect);
                   4950: #endif
                   4951:                                bool empty = key_buf_char->empty();
                   4952: #ifdef USE_SERVICE_THREAD
                   4953:                                LeaveCriticalSection(&key_buf_crit_sect);
                   4954: #endif
                   4955:                                if(!empty) break;
                   4956:                        }
1.1.1.23  root     4957:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   4958:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   4959:                                if(_kbhit()) {
1.1.1.32  root     4960:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     4961: #ifdef USE_SERVICE_THREAD
                   4962:                                                EnterCriticalSection(&key_buf_crit_sect);
                   4963: #endif
1.1.1.32  root     4964:                                                key_buf_char->write(_getch());
1.1.1.35  root     4965:                                                key_buf_scan->write(0x00);
                   4966: #ifdef USE_SERVICE_THREAD
                   4967:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   4968: #endif
1.1.1.32  root     4969:                                        }
1.1.1.23  root     4970:                                } else {
                   4971:                                        Sleep(10);
                   4972:                                }
                   4973:                        } else {
                   4974:                                if(!update_key_buffer()) {
                   4975:                                        Sleep(10);
                   4976:                                }
1.1.1.14  root     4977:                        }
                   4978:                }
                   4979:                if(m_halted) {
1.1.1.33  root     4980:                        // insert CR to terminate input loops
1.1.1.14  root     4981:                        key_char = 0x0d;
                   4982:                        key_scan = 0;
1.1.1.32  root     4983:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     4984: #ifdef USE_SERVICE_THREAD
                   4985:                        EnterCriticalSection(&key_buf_crit_sect);
                   4986: #endif
1.1.1.14  root     4987:                        key_char = key_buf_char->read();
                   4988:                        key_scan = key_buf_scan->read();
1.1.1.41  root     4989:                        // write to bottom of key buffer
                   4990:                        mem[0x43c] = (UINT8)key_char;
                   4991:                        mem[0x43d] = (UINT8)key_scan;
1.1.1.35  root     4992: #ifdef USE_SERVICE_THREAD
                   4993:                        LeaveCriticalSection(&key_buf_crit_sect);
                   4994: #endif
1.1.1.5   root     4995:                }
1.1       root     4996:        }
                   4997:        if(echo && key_char) {
                   4998:                msdos_putch(key_char);
                   4999:        }
                   5000:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   5001: }
                   5002: 
                   5003: inline int msdos_getch()
                   5004: {
                   5005:        return(msdos_getch_ex(0));
                   5006: }
                   5007: 
                   5008: inline int msdos_getche()
                   5009: {
                   5010:        return(msdos_getch_ex(1));
                   5011: }
                   5012: 
                   5013: int msdos_write(int fd, const void *buffer, unsigned int count)
                   5014: {
1.1.1.37  root     5015:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5016:                // write to serial port
1.1.1.38  root     5017:                int written = 0;
1.1.1.37  root     5018:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5019:                        UINT8 *buf = (UINT8 *)buffer;
                   5020:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5021:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5022:                        DWORD timeout = timeGetTime() + 1000;
                   5023:                        while(written < count) {
                   5024:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
                   5025:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
                   5026:                                        timeout = timeGetTime() + 1000;
                   5027:                                } else {
                   5028:                                        if(timeGetTime() > timeout) {
                   5029:                                                break;
                   5030:                                        }
                   5031:                                        Sleep(10);
                   5032:                                }
1.1.1.37  root     5033:                        }
                   5034:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5035:                }
1.1.1.38  root     5036:                return(written);
1.1.1.37  root     5037:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   5038:                // write to printer port
                   5039:                UINT8 *buf = (UINT8 *)buffer;
                   5040:                for(unsigned int i = 0; i < count; i++) {
                   5041: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5042:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5043:                }
                   5044:                return(count);
                   5045:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     5046:                // CR+LF -> LF
1.1.1.37  root     5047:                static int is_cr = 0;
1.1       root     5048:                UINT8 *buf = (UINT8 *)buffer;
                   5049:                for(unsigned int i = 0; i < count; i++) {
                   5050:                        UINT8 data = buf[i];
                   5051:                        if(is_cr) {
                   5052:                                if(data != 0x0a) {
                   5053:                                        UINT8 tmp = 0x0d;
                   5054:                                        _write(1, &tmp, 1);
                   5055:                                }
                   5056:                                _write(1, &data, 1);
                   5057:                                is_cr = 0;
                   5058:                        } else if(data == 0x0d) {
                   5059:                                is_cr = 1;
                   5060:                        } else {
                   5061:                                _write(1, &data, 1);
                   5062:                        }
                   5063:                }
                   5064:                return(count);
                   5065:        }
1.1.1.14  root     5066:        vram_flush();
1.1       root     5067:        return(_write(fd, buffer, count));
                   5068: }
                   5069: 
                   5070: void msdos_putch(UINT8 data)
1.1.1.35  root     5071: #ifdef USE_SERVICE_THREAD
                   5072: {
                   5073:        EnterCriticalSection(&putch_crit_sect);
                   5074:        msdos_putch_tmp(data);
                   5075:        LeaveCriticalSection(&putch_crit_sect);
                   5076: }
                   5077: void msdos_putch_tmp(UINT8 data)
                   5078: #endif
1.1       root     5079: {
1.1.1.34  root     5080:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5081:        SMALL_RECT rect;
                   5082:        COORD co;
1.1       root     5083:        static int p = 0;
                   5084:        static int is_kanji = 0;
                   5085:        static int is_esc = 0;
                   5086:        static int stored_x;
                   5087:        static int stored_y;
                   5088:        static WORD stored_a;
1.1.1.20  root     5089:        static char tmp[64], out[64];
1.1       root     5090:        
                   5091:        msdos_stdio_reopen();
                   5092:        
1.1.1.20  root     5093:        process_t *process = msdos_process_info_get(current_psp);
                   5094:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5095:        
                   5096:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5097:                // stdout is redirected to file
1.1.1.20  root     5098:                msdos_write(fd, &data, 1);
1.1       root     5099:                return;
                   5100:        }
1.1.1.23  root     5101:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     5102:        
                   5103:        // output to console
                   5104:        tmp[p++] = data;
                   5105:        
1.1.1.14  root     5106:        vram_flush();
                   5107:        
1.1       root     5108:        if(is_kanji) {
                   5109:                // kanji character
                   5110:                is_kanji = 0;
                   5111:        } else if(is_esc) {
                   5112:                // escape sequense
                   5113:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   5114:                        p = is_esc = 0;
                   5115:                } else if(tmp[1] == '=' && p == 4) {
                   5116:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     5117:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     5118:                        SetConsoleCursorPosition(hStdout, co);
                   5119:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5120:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     5121:                        cursor_moved = false;
                   5122:                        p = is_esc = 0;
                   5123:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
                   5124:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5125:                        co.X = csbi.dwCursorPosition.X;
                   5126:                        co.Y = csbi.dwCursorPosition.Y;
                   5127:                        WORD wAttributes = csbi.wAttributes;
                   5128:                        
                   5129:                        if(tmp[1] == 'D') {
                   5130:                                co.Y++;
                   5131:                        } else if(tmp[1] == 'E') {
                   5132:                                co.X = 0;
                   5133:                                co.Y++;
                   5134:                        } else if(tmp[1] == 'M') {
                   5135:                                co.Y--;
                   5136:                        } else if(tmp[1] == '*') {
1.1.1.14  root     5137:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5138:                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5139:                                co.X = 0;
                   5140:                                co.Y = csbi.srWindow.Top;
1.1       root     5141:                        } else if(tmp[1] == '[') {
                   5142:                                int param[256], params = 0;
                   5143:                                memset(param, 0, sizeof(param));
                   5144:                                for(int i = 2; i < p; i++) {
                   5145:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   5146:                                                param[params] *= 10;
                   5147:                                                param[params] += tmp[i] - '0';
                   5148:                                        } else {
                   5149:                                                params++;
                   5150:                                        }
                   5151:                                }
                   5152:                                if(data == 'A') {
1.1.1.14  root     5153:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     5154:                                } else if(data == 'B') {
1.1.1.14  root     5155:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     5156:                                } else if(data == 'C') {
1.1.1.14  root     5157:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     5158:                                } else if(data == 'D') {
1.1.1.14  root     5159:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     5160:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     5161:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   5162:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     5163:                                } else if(data == 'J') {
1.1.1.14  root     5164:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5165:                                        if(param[0] == 0) {
                   5166:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5167:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5168:                                                if(co.Y < csbi.srWindow.Bottom) {
                   5169:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5170:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5171:                                                }
                   5172:                                        } else if(param[0] == 1) {
1.1.1.14  root     5173:                                                if(co.Y > csbi.srWindow.Top) {
                   5174:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
                   5175:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5176:                                                }
                   5177:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5178:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5179:                                        } else if(param[0] == 2) {
1.1.1.14  root     5180:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5181:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5182:                                                co.X = co.Y = 0;
                   5183:                                        }
                   5184:                                } else if(data == 'K') {
1.1.1.14  root     5185:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5186:                                        if(param[0] == 0) {
                   5187:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5188:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5189:                                        } else if(param[0] == 1) {
                   5190:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5191:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5192:                                        } else if(param[0] == 2) {
                   5193:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5194:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5195:                                        }
                   5196:                                } else if(data == 'L') {
1.1.1.14  root     5197:                                        if(params == 0) {
                   5198:                                                param[0] = 1;
1.1       root     5199:                                        }
1.1.1.14  root     5200:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5201:                                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5202:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5203:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5204:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5205:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14  root     5206:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5207:                                        co.X = 0;
                   5208:                                } else if(data == 'M') {
1.1.1.14  root     5209:                                        if(params == 0) {
                   5210:                                                param[0] = 1;
                   5211:                                        }
                   5212:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5213:                                                clear_scr_buffer(csbi.wAttributes);
                   5214:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5215:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5216:                                        } else {
1.1.1.14  root     5217:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5218:                                                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5219:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5220:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5221:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5222:                                        }
                   5223:                                        co.X = 0;
                   5224:                                } else if(data == 'h') {
                   5225:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5226:                                                CONSOLE_CURSOR_INFO cur;
                   5227:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5228:                                                if(cur.bVisible) {
                   5229:                                                        cur.bVisible = FALSE;
1.1.1.14  root     5230: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5231:                                                }
                   5232:                                        }
                   5233:                                } else if(data == 'l') {
                   5234:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5235:                                                CONSOLE_CURSOR_INFO cur;
                   5236:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5237:                                                if(!cur.bVisible) {
                   5238:                                                        cur.bVisible = TRUE;
1.1.1.14  root     5239: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5240:                                                }
                   5241:                                        }
                   5242:                                } else if(data == 'm') {
                   5243:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5244:                                        int reverse = 0, hidden = 0;
                   5245:                                        for(int i = 0; i < params; i++) {
                   5246:                                                if(param[i] == 1) {
                   5247:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5248:                                                } else if(param[i] == 4) {
                   5249:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5250:                                                } else if(param[i] == 7) {
                   5251:                                                        reverse = 1;
                   5252:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5253:                                                        hidden = 1;
                   5254:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5255:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5256:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5257:                                                                param[i] -= 16;
                   5258:                                                        } else {
                   5259:                                                                param[i] -= 30;
                   5260:                                                        }
                   5261:                                                        if(param[i] & 1) {
                   5262:                                                                wAttributes |= FOREGROUND_RED;
                   5263:                                                        }
                   5264:                                                        if(param[i] & 2) {
                   5265:                                                                wAttributes |= FOREGROUND_GREEN;
                   5266:                                                        }
                   5267:                                                        if(param[i] & 4) {
                   5268:                                                                wAttributes |= FOREGROUND_BLUE;
                   5269:                                                        }
                   5270:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5271:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5272:                                                        if((param[i] - 40) & 1) {
                   5273:                                                                wAttributes |= BACKGROUND_RED;
                   5274:                                                        }
                   5275:                                                        if((param[i] - 40) & 2) {
                   5276:                                                                wAttributes |= BACKGROUND_GREEN;
                   5277:                                                        }
                   5278:                                                        if((param[i] - 40) & 4) {
                   5279:                                                                wAttributes |= BACKGROUND_BLUE;
                   5280:                                                        }
                   5281:                                                }
                   5282:                                        }
                   5283:                                        if(reverse) {
                   5284:                                                wAttributes &= ~0xff;
                   5285:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5286:                                        }
                   5287:                                        if(hidden) {
                   5288:                                                wAttributes &= ~0x0f;
                   5289:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5290:                                        }
                   5291:                                } else if(data == 'n') {
                   5292:                                        if(param[0] == 6) {
                   5293:                                                char tmp[16];
                   5294:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5295:                                                int len = strlen(tmp);
1.1.1.32  root     5296:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5297: #ifdef USE_SERVICE_THREAD
                   5298:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5299: #endif
1.1.1.32  root     5300:                                                        for(int i = 0; i < len; i++) {
                   5301:                                                                key_buf_char->write(tmp[i]);
                   5302:                                                                key_buf_scan->write(0x00);
                   5303:                                                        }
1.1.1.35  root     5304: #ifdef USE_SERVICE_THREAD
                   5305:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5306: #endif
1.1       root     5307:                                                }
                   5308:                                        }
                   5309:                                } else if(data == 's') {
                   5310:                                        stored_x = co.X;
                   5311:                                        stored_y = co.Y;
                   5312:                                        stored_a = wAttributes;
                   5313:                                } else if(data == 'u') {
                   5314:                                        co.X = stored_x;
                   5315:                                        co.Y = stored_y;
                   5316:                                        wAttributes = stored_a;
                   5317:                                }
                   5318:                        }
                   5319:                        if(co.X < 0) {
                   5320:                                co.X = 0;
                   5321:                        } else if(co.X >= csbi.dwSize.X) {
                   5322:                                co.X = csbi.dwSize.X - 1;
                   5323:                        }
1.1.1.14  root     5324:                        if(co.Y < csbi.srWindow.Top) {
                   5325:                                co.Y = csbi.srWindow.Top;
                   5326:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5327:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5328:                        }
                   5329:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5330:                                SetConsoleCursorPosition(hStdout, co);
                   5331:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5332:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5333:                                cursor_moved = false;
                   5334:                        }
                   5335:                        if(wAttributes != csbi.wAttributes) {
                   5336:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5337:                        }
                   5338:                        p = is_esc = 0;
                   5339:                }
                   5340:                return;
                   5341:        } else {
                   5342:                if(msdos_lead_byte_check(data)) {
                   5343:                        is_kanji = 1;
                   5344:                        return;
                   5345:                } else if(data == 0x1b) {
                   5346:                        is_esc = 1;
                   5347:                        return;
                   5348:                }
                   5349:        }
1.1.1.20  root     5350:        
                   5351:        DWORD q = 0, num;
                   5352:        is_kanji = 0;
                   5353:        for(int i = 0; i < p; i++) {
                   5354:                UINT8 c = tmp[i];
                   5355:                if(is_kanji) {
                   5356:                        is_kanji = 0;
                   5357:                } else if(msdos_lead_byte_check(data)) {
                   5358:                        is_kanji = 1;
                   5359:                } else if(msdos_ctrl_code_check(data)) {
                   5360:                        out[q++] = '^';
                   5361:                        c += 'A' - 1;
                   5362:                }
                   5363:                out[q++] = c;
                   5364:        }
1.1.1.34  root     5365:        if(q == 1 && out[0] == 0x08) {
                   5366:                // back space
                   5367:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5368:                if(csbi.dwCursorPosition.X > 0) {
                   5369:                        co.X = csbi.dwCursorPosition.X - 1;
                   5370:                        co.Y = csbi.dwCursorPosition.Y;
                   5371:                        SetConsoleCursorPosition(hStdout, co);
                   5372:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5373:                        co.X = csbi.dwSize.X - 1;
                   5374:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5375:                        SetConsoleCursorPosition(hStdout, co);
                   5376:                } else {
                   5377:                        WriteConsole(hStdout, out, q, &num, NULL); // to make sure
                   5378:                }
                   5379:        } else {
                   5380:                WriteConsole(hStdout, out, q, &num, NULL);
                   5381:        }
1.1       root     5382:        p = 0;
1.1.1.14  root     5383:        
1.1.1.15  root     5384:        if(!restore_console_on_exit) {
                   5385:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5386:                scr_top = csbi.srWindow.Top;
                   5387:        }
1.1       root     5388:        cursor_moved = true;
                   5389: }
                   5390: 
                   5391: int msdos_aux_in()
                   5392: {
1.1.1.21  root     5393:        msdos_stdio_reopen();
                   5394:        
1.1.1.20  root     5395:        process_t *process = msdos_process_info_get(current_psp);
                   5396:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5397:        
                   5398:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5399:                char data = 0;
1.1.1.37  root     5400:                msdos_read(fd, &data, 1);
1.1       root     5401:                return(data);
                   5402:        } else {
                   5403:                return(EOF);
                   5404:        }
                   5405: }
                   5406: 
                   5407: void msdos_aux_out(char data)
                   5408: {
1.1.1.21  root     5409:        msdos_stdio_reopen();
                   5410:        
1.1.1.20  root     5411:        process_t *process = msdos_process_info_get(current_psp);
                   5412:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5413:        
                   5414:        if(fd < process->max_files && file_handler[fd].valid) {
                   5415:                msdos_write(fd, &data, 1);
1.1       root     5416:        }
                   5417: }
                   5418: 
                   5419: void msdos_prn_out(char data)
                   5420: {
1.1.1.21  root     5421:        msdos_stdio_reopen();
                   5422:        
1.1.1.20  root     5423:        process_t *process = msdos_process_info_get(current_psp);
                   5424:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5425:        
                   5426:        if(fd < process->max_files && file_handler[fd].valid) {
                   5427:                msdos_write(fd, &data, 1);
1.1       root     5428:        }
                   5429: }
                   5430: 
                   5431: // memory control
                   5432: 
1.1.1.45! root     5433: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name)
1.1       root     5434: {
                   5435:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5436:        
                   5437:        mcb->mz = mz;
                   5438:        mcb->psp = psp;
1.1.1.30  root     5439:        mcb->paragraphs = paragraphs;
1.1.1.39  root     5440:        
                   5441:        if(prog_name != NULL) {
                   5442:                memset(mcb->prog_name, 0, 8);
                   5443:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
                   5444:        }
1.1       root     5445:        return(mcb);
                   5446: }
                   5447: 
1.1.1.39  root     5448: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs)
                   5449: {
                   5450:        return(msdos_mcb_create(mcb_seg, mz, psp, paragraphs, NULL));
                   5451: }
                   5452: 
1.1       root     5453: void msdos_mcb_check(mcb_t *mcb)
                   5454: {
                   5455:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5456:                #if 0
                   5457:                        // shutdown now !!!
                   5458:                        fatalerror("broken memory control block\n");
                   5459:                #else
                   5460:                        // return error code and continue
                   5461:                        throw(0x07); // broken memory control block
                   5462:                #endif
1.1       root     5463:        }
                   5464: }
                   5465: 
1.1.1.39  root     5466: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5467: {
                   5468:        int mcb_seg = seg - 1;
                   5469:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5470:        msdos_mcb_check(mcb);
                   5471:        
1.1.1.30  root     5472:        if(mcb->paragraphs > paragraphs) {
1.1       root     5473:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5474:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5475:                
                   5476:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5477:                mcb->mz = 'M';
1.1.1.30  root     5478:                mcb->paragraphs = paragraphs;
1.1       root     5479:        }
                   5480: }
                   5481: 
                   5482: void msdos_mem_merge(int seg)
                   5483: {
                   5484:        int mcb_seg = seg - 1;
                   5485:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5486:        msdos_mcb_check(mcb);
                   5487:        
                   5488:        while(1) {
                   5489:                if(mcb->mz == 'Z') {
                   5490:                        break;
                   5491:                }
1.1.1.30  root     5492:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5493:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5494:                msdos_mcb_check(next_mcb);
                   5495:                
                   5496:                if(next_mcb->psp != 0) {
                   5497:                        break;
                   5498:                }
                   5499:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5500:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5501:        }
                   5502: }
                   5503: 
1.1.1.8   root     5504: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5505: {
                   5506:        while(1) {
                   5507:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5508:                bool last_block;
1.1       root     5509:                
1.1.1.14  root     5510:                if(mcb->psp == 0) {
                   5511:                        msdos_mem_merge(mcb_seg + 1);
                   5512:                } else {
                   5513:                        msdos_mcb_check(mcb);
                   5514:                }
1.1.1.33  root     5515:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5516:                        // check if the next is dummy mcb to link to umb
                   5517:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5518:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5519:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5520:                }
                   5521:                if(!(new_process && !last_block)) {
1.1.1.30  root     5522:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5523:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5524:                                mcb->psp = current_psp;
                   5525:                                return(mcb_seg + 1);
                   5526:                        }
                   5527:                }
                   5528:                if(mcb->mz == 'Z') {
                   5529:                        break;
                   5530:                }
1.1.1.30  root     5531:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5532:        }
                   5533:        return(-1);
                   5534: }
                   5535: 
                   5536: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5537: {
                   5538:        int mcb_seg = seg - 1;
                   5539:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5540:        msdos_mcb_check(mcb);
1.1.1.30  root     5541:        int current_paragraphs = mcb->paragraphs;
1.1       root     5542:        
                   5543:        msdos_mem_merge(seg);
1.1.1.30  root     5544:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5545:                if(max_paragraphs) {
1.1.1.30  root     5546:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5547:                }
1.1       root     5548:                msdos_mem_split(seg, current_paragraphs);
                   5549:                return(-1);
                   5550:        }
                   5551:        msdos_mem_split(seg, paragraphs);
                   5552:        return(0);
                   5553: }
                   5554: 
                   5555: void msdos_mem_free(int seg)
                   5556: {
                   5557:        int mcb_seg = seg - 1;
                   5558:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5559:        msdos_mcb_check(mcb);
                   5560:        
                   5561:        mcb->psp = 0;
                   5562:        msdos_mem_merge(seg);
                   5563: }
                   5564: 
1.1.1.8   root     5565: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5566: {
                   5567:        int max_paragraphs = 0;
                   5568:        
                   5569:        while(1) {
                   5570:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5571:                bool last_block;
                   5572:                
1.1       root     5573:                msdos_mcb_check(mcb);
                   5574:                
1.1.1.33  root     5575:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5576:                        // check if the next is dummy mcb to link to umb
                   5577:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5578:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5579:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5580:                }
                   5581:                if(!(new_process && !last_block)) {
1.1.1.30  root     5582:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   5583:                                max_paragraphs = mcb->paragraphs;
1.1       root     5584:                        }
                   5585:                }
                   5586:                if(mcb->mz == 'Z') {
                   5587:                        break;
                   5588:                }
1.1.1.30  root     5589:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5590:        }
1.1.1.14  root     5591:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     5592: }
                   5593: 
1.1.1.8   root     5594: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   5595: {
                   5596:        int last_seg = -1;
                   5597:        
                   5598:        while(1) {
                   5599:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5600:                msdos_mcb_check(mcb);
                   5601:                
1.1.1.14  root     5602:                if(mcb->psp == psp) {
1.1.1.8   root     5603:                        last_seg = mcb_seg;
                   5604:                }
1.1.1.14  root     5605:                if(mcb->mz == 'Z') {
                   5606:                        break;
                   5607:                }
1.1.1.30  root     5608:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     5609:        }
                   5610:        return(last_seg);
                   5611: }
                   5612: 
1.1.1.19  root     5613: int msdos_mem_get_umb_linked()
                   5614: {
1.1.1.33  root     5615:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5616:        msdos_mcb_check(mcb);
1.1.1.19  root     5617:        
1.1.1.33  root     5618:        if(mcb->mz == 'M') {
                   5619:                return(-1);
1.1.1.19  root     5620:        }
                   5621:        return(0);
                   5622: }
                   5623: 
1.1.1.33  root     5624: void msdos_mem_link_umb()
1.1.1.19  root     5625: {
1.1.1.33  root     5626:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5627:        msdos_mcb_check(mcb);
1.1.1.19  root     5628:        
1.1.1.33  root     5629:        mcb->mz = 'M';
                   5630:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39  root     5631:        
                   5632:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     5633: }
                   5634: 
1.1.1.33  root     5635: void msdos_mem_unlink_umb()
1.1.1.19  root     5636: {
1.1.1.33  root     5637:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5638:        msdos_mcb_check(mcb);
1.1.1.19  root     5639:        
1.1.1.33  root     5640:        mcb->mz = 'Z';
                   5641:        mcb->paragraphs = 0;
1.1.1.39  root     5642:        
                   5643:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     5644: }
                   5645: 
1.1.1.29  root     5646: #ifdef SUPPORT_HMA
                   5647: 
                   5648: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   5649: {
                   5650:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5651:        
                   5652:        mcb->ms[0] = 'M';
                   5653:        mcb->ms[1] = 'S';
                   5654:        mcb->owner = owner;
                   5655:        mcb->size = size;
                   5656:        mcb->next = next;
                   5657:        return(mcb);
                   5658: }
                   5659: 
                   5660: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   5661: {
                   5662:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   5663: }
                   5664: 
                   5665: int msdos_hma_mem_split(int offset, int size)
                   5666: {
                   5667:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5668:        
                   5669:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5670:                return(-1);
                   5671:        }
                   5672:        if(mcb->size >= size + 0x10) {
                   5673:                int new_offset = offset + 0x10 + size;
                   5674:                int new_size = mcb->size - 0x10 - size;
                   5675:                
                   5676:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   5677:                mcb->size = size;
                   5678:                mcb->next = new_offset;
                   5679:                return(0);
                   5680:        }
                   5681:        return(-1);
                   5682: }
                   5683: 
                   5684: void msdos_hma_mem_merge(int offset)
                   5685: {
                   5686:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5687:        
                   5688:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5689:                return;
                   5690:        }
                   5691:        while(1) {
                   5692:                if(mcb->next == 0) {
                   5693:                        break;
                   5694:                }
                   5695:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   5696:                
                   5697:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   5698:                        return;
                   5699:                }
                   5700:                if(next_mcb->owner != 0) {
                   5701:                        break;
                   5702:                }
                   5703:                mcb->size += 0x10 + next_mcb->size;
                   5704:                mcb->next = next_mcb->next;
                   5705:        }
                   5706: }
                   5707: 
                   5708: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   5709: {
                   5710:        int offset = 0x10; // first mcb in HMA
                   5711:        
                   5712:        while(1) {
                   5713:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5714:                
                   5715:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5716:                        return(-1);
                   5717:                }
                   5718:                if(mcb->owner == 0) {
                   5719:                        msdos_hma_mem_merge(offset);
                   5720:                }
                   5721:                if(mcb->owner == 0 && mcb->size >= size) {
                   5722:                        msdos_hma_mem_split(offset, size);
                   5723:                        mcb->owner = owner;
                   5724:                        return(offset);
                   5725:                }
                   5726:                if(mcb->next == 0) {
                   5727:                        break;
                   5728:                }
                   5729:                offset = mcb->next;
                   5730:        }
                   5731:        return(-1);
                   5732: }
                   5733: 
                   5734: int msdos_hma_mem_realloc(int offset, int size)
                   5735: {
                   5736:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5737:        
                   5738:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5739:                return(-1);
                   5740:        }
                   5741:        if(mcb->size < size) {
                   5742:                return(-1);
                   5743:        }
                   5744:        msdos_hma_mem_split(offset, size);
                   5745:        return(0);
                   5746: }
                   5747: 
                   5748: void msdos_hma_mem_free(int offset)
                   5749: {
                   5750:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5751:        
                   5752:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5753:                return;
                   5754:        }
                   5755:        mcb->owner = 0;
                   5756:        msdos_hma_mem_merge(offset);
                   5757: }
                   5758: 
                   5759: int msdos_hma_mem_get_free(int *available_offset)
                   5760: {
                   5761:        int offset = 0x10; // first mcb in HMA
                   5762:        int size = 0;
                   5763:        
                   5764:        while(1) {
                   5765:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5766:                
                   5767:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5768:                        return(0);
                   5769:                }
                   5770:                if(mcb->owner == 0 && size < mcb->size) {
                   5771:                        if(available_offset != NULL) {
                   5772:                                *available_offset = offset;
                   5773:                        }
                   5774:                        size = mcb->size;
                   5775:                }
                   5776:                if(mcb->next == 0) {
                   5777:                        break;
                   5778:                }
                   5779:                offset = mcb->next;
                   5780:        }
                   5781:        return(size);
                   5782: }
                   5783: 
                   5784: #endif
                   5785: 
1.1       root     5786: // environment
                   5787: 
1.1.1.45! root     5788: void msdos_env_set_argv(int env_seg, const char *argv)
1.1       root     5789: {
                   5790:        char *dst = (char *)(mem + (env_seg << 4));
                   5791:        
                   5792:        while(1) {
                   5793:                if(dst[0] == 0) {
                   5794:                        break;
                   5795:                }
                   5796:                dst += strlen(dst) + 1;
                   5797:        }
                   5798:        *dst++ = 0; // end of environment
                   5799:        *dst++ = 1; // top of argv[0]
                   5800:        *dst++ = 0;
                   5801:        memcpy(dst, argv, strlen(argv));
                   5802:        dst += strlen(argv);
                   5803:        *dst++ = 0;
                   5804:        *dst++ = 0;
                   5805: }
                   5806: 
1.1.1.45! root     5807: const char *msdos_env_get_argv(int env_seg)
1.1       root     5808: {
                   5809:        static char env[ENV_SIZE];
                   5810:        char *src = env;
                   5811:        
                   5812:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5813:        while(1) {
                   5814:                if(src[0] == 0) {
                   5815:                        if(src[1] == 1) {
                   5816:                                return(src + 3);
                   5817:                        }
                   5818:                        break;
                   5819:                }
                   5820:                src += strlen(src) + 1;
                   5821:        }
                   5822:        return(NULL);
                   5823: }
                   5824: 
1.1.1.45! root     5825: const char *msdos_env_get(int env_seg, const char *name)
1.1       root     5826: {
                   5827:        static char env[ENV_SIZE];
                   5828:        char *src = env;
                   5829:        
                   5830:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5831:        while(1) {
                   5832:                if(src[0] == 0) {
                   5833:                        break;
                   5834:                }
                   5835:                int len = strlen(src);
                   5836:                char *n = my_strtok(src, "=");
                   5837:                char *v = src + strlen(n) + 1;
                   5838:                
                   5839:                if(_stricmp(name, n) == 0) {
                   5840:                        return(v);
                   5841:                }
                   5842:                src += len + 1;
                   5843:        }
                   5844:        return(NULL);
                   5845: }
                   5846: 
1.1.1.45! root     5847: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1       root     5848: {
                   5849:        char env[ENV_SIZE];
                   5850:        char *src = env;
                   5851:        char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45! root     5852:        const char *argv = msdos_env_get_argv(env_seg);
1.1       root     5853:        int done = 0;
                   5854:        
                   5855:        memcpy(src, dst, ENV_SIZE);
                   5856:        memset(dst, 0, ENV_SIZE);
                   5857:        while(1) {
                   5858:                if(src[0] == 0) {
                   5859:                        break;
                   5860:                }
                   5861:                int len = strlen(src);
                   5862:                char *n = my_strtok(src, "=");
                   5863:                char *v = src + strlen(n) + 1;
                   5864:                char tmp[1024];
                   5865:                
                   5866:                if(_stricmp(name, n) == 0) {
                   5867:                        sprintf(tmp, "%s=%s", n, value);
                   5868:                        done = 1;
                   5869:                } else {
                   5870:                        sprintf(tmp, "%s=%s", n, v);
                   5871:                }
                   5872:                memcpy(dst, tmp, strlen(tmp));
                   5873:                dst += strlen(tmp) + 1;
                   5874:                src += len + 1;
                   5875:        }
                   5876:        if(!done) {
                   5877:                char tmp[1024];
                   5878:                
                   5879:                sprintf(tmp, "%s=%s", name, value);
                   5880:                memcpy(dst, tmp, strlen(tmp));
                   5881:                dst += strlen(tmp) + 1;
                   5882:        }
                   5883:        if(argv) {
                   5884:                *dst++ = 0; // end of environment
                   5885:                *dst++ = 1; // top of argv[0]
                   5886:                *dst++ = 0;
                   5887:                memcpy(dst, argv, strlen(argv));
                   5888:                dst += strlen(argv);
                   5889:                *dst++ = 0;
                   5890:                *dst++ = 0;
                   5891:        }
                   5892: }
                   5893: 
                   5894: // process
                   5895: 
1.1.1.8   root     5896: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     5897: {
                   5898:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5899:        
                   5900:        memset(psp, 0, PSP_SIZE);
                   5901:        psp->exit[0] = 0xcd;
                   5902:        psp->exit[1] = 0x20;
1.1.1.8   root     5903:        psp->first_mcb = mcb_seg;
1.1       root     5904:        psp->far_call = 0xea;
                   5905:        psp->cpm_entry.w.l = 0xfff1;    // int 21h, retf
                   5906:        psp->cpm_entry.w.h = 0xf000;
                   5907:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   5908:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   5909:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   5910:        psp->parent_psp = parent_psp;
1.1.1.20  root     5911:        if(parent_psp == (UINT16)-1) {
                   5912:                for(int i = 0; i < 20; i++) {
                   5913:                        if(file_handler[i].valid) {
                   5914:                                psp->file_table[i] = i;
                   5915:                        } else {
                   5916:                                psp->file_table[i] = 0xff;
                   5917:                        }
1.1       root     5918:                }
1.1.1.20  root     5919:        } else {
                   5920:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     5921:        }
                   5922:        psp->env_seg = env_seg;
                   5923:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     5924:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     5925:        psp->file_table_size = 20;
                   5926:        psp->file_table_ptr.w.l = 0x18;
                   5927:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     5928:        psp->service[0] = 0xcd;
                   5929:        psp->service[1] = 0x21;
                   5930:        psp->service[2] = 0xcb;
                   5931:        return(psp);
                   5932: }
                   5933: 
1.1.1.20  root     5934: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   5935: {
                   5936:        if(psp_seg && fd < 20) {
                   5937:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5938:                psp->file_table[fd] = value;
                   5939:        }
                   5940: }
                   5941: 
                   5942: int msdos_psp_get_file_table(int fd, int psp_seg)
                   5943: {
                   5944:        if(psp_seg && fd < 20) {
                   5945:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5946:                fd = psp->file_table[fd];
                   5947:        }
                   5948:        return fd;
                   5949: }
                   5950: 
1.1.1.45! root     5951: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al)
1.1       root     5952: {
                   5953:        // load command file
                   5954:        int fd = -1;
1.1.1.45! root     5955:        int sio_port = 0;
        !          5956:        int lpt_port = 0;
1.1       root     5957:        int dos_command = 0;
1.1.1.24  root     5958:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     5959:        char pipe_stdin_path[MAX_PATH] = {0};
                   5960:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39  root     5961:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     5962:        
                   5963:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   5964:        int opt_len = mem[opt_ofs];
                   5965:        memset(opt, 0, sizeof(opt));
                   5966:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   5967:        
1.1.1.14  root     5968:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   5969:                // this is a batch file, run command.com
                   5970:                char tmp[MAX_PATH];
                   5971:                if(opt_len != 0) {
                   5972:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   5973:                } else {
                   5974:                        sprintf(tmp, "/C %s", cmd);
                   5975:                }
                   5976:                strcpy(opt, tmp);
                   5977:                opt_len = strlen(opt);
                   5978:                mem[opt_ofs] = opt_len;
                   5979:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5980:                strcpy(command, comspec_path);
                   5981:                strcpy(name_tmp, "COMMAND.COM");
                   5982:        } else {
                   5983:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   5984:                        // redirect C:\COMMAND.COM to comspec_path
                   5985:                        strcpy(command, comspec_path);
                   5986:                } else {
                   5987:                        strcpy(command, cmd);
                   5988:                }
1.1.1.24  root     5989:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   5990:                        return(-1);
                   5991:                }
1.1.1.14  root     5992:                memset(name_tmp, 0, sizeof(name_tmp));
                   5993:                strcpy(name_tmp, name);
                   5994:                
                   5995:                // check command.com
1.1.1.38  root     5996:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   5997:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     5998:                        if(opt_len == 0) {
                   5999: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   6000:                                process_t *current_process = NULL;
                   6001:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   6002:                                        if(process[i].psp == current_psp) {
                   6003:                                                current_process = &process[i];
                   6004:                                                break;
                   6005:                                        }
                   6006:                                }
                   6007:                                if(current_process != NULL) {
                   6008:                                        param->cmd_line.dw = current_process->dta.dw;
                   6009:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6010:                                        opt_len = mem[opt_ofs];
                   6011:                                        memset(opt, 0, sizeof(opt));
                   6012:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6013:                                }
                   6014:                        }
                   6015:                        for(int i = 0; i < opt_len; i++) {
                   6016:                                if(opt[i] == ' ') {
                   6017:                                        continue;
                   6018:                                }
                   6019:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   6020:                                        for(int j = i + 3; j < opt_len; j++) {
                   6021:                                                if(opt[j] == ' ') {
                   6022:                                                        continue;
                   6023:                                                }
                   6024:                                                char *token = my_strtok(opt + j, " ");
                   6025:                                                
1.1.1.38  root     6026:                                                strcpy(command, token);
                   6027:                                                char tmp[MAX_PATH];
                   6028:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39  root     6029:                                                strcpy(opt, "");
                   6030:                                                for(int i = 0; i < strlen(tmp); i++) {
                   6031:                                                        if(tmp[i] != ' ') {
                   6032:                                                                strcpy(opt, tmp + i);
                   6033:                                                                break;
                   6034:                                                        }
                   6035:                                                }
                   6036:                                                strcpy(tmp, opt);
1.1.1.38  root     6037:                                                
                   6038:                                                if(al == 0x00) {
1.1.1.39  root     6039:                                                        #define GET_FILE_PATH() { \
                   6040:                                                                if(token[0] != '>' && token[0] != '<') { \
                   6041:                                                                        token++; \
                   6042:                                                                } \
                   6043:                                                                token++; \
                   6044:                                                                while(*token == ' ') { \
                   6045:                                                                        token++; \
                   6046:                                                                } \
                   6047:                                                                char *ptr = token; \
                   6048:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
                   6049:                                                                        ptr++; \
                   6050:                                                                } \
                   6051:                                                                *ptr = '\0'; \
                   6052:                                                        }
                   6053:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
                   6054:                                                                GET_FILE_PATH();
1.1.1.38  root     6055:                                                                strcpy(pipe_stdin_path, token);
                   6056:                                                                strcpy(opt, tmp);
                   6057:                                                        }
1.1.1.39  root     6058:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
                   6059:                                                                GET_FILE_PATH();
1.1.1.38  root     6060:                                                                strcpy(pipe_stdout_path, token);
                   6061:                                                                strcpy(opt, tmp);
                   6062:                                                        }
1.1.1.39  root     6063:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6064:                                                                GET_FILE_PATH();
                   6065:                                                                strcpy(pipe_stderr_path, token);
                   6066:                                                                strcpy(opt, tmp);
                   6067:                                                        }
                   6068:                                                        #undef GET_FILE_PATH
                   6069:                                                        
                   6070:                                                        if((token = strstr(opt, "0<")) != NULL) {
                   6071:                                                                *token = '\0';
                   6072:                                                        }
                   6073:                                                        if((token = strstr(opt, "1>")) != NULL) {
                   6074:                                                                *token = '\0';
                   6075:                                                        }
                   6076:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6077:                                                                *token = '\0';
                   6078:                                                        }
1.1.1.38  root     6079:                                                        if((token = strstr(opt, "<")) != NULL) {
                   6080:                                                                *token = '\0';
                   6081:                                                        }
                   6082:                                                        if((token = strstr(opt, ">")) != NULL) {
                   6083:                                                                *token = '\0';
                   6084:                                                        }
1.1.1.14  root     6085:                                                }
1.1.1.39  root     6086:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
                   6087:                                                        opt[i] = '\0';
                   6088:                                                }
1.1.1.38  root     6089:                                                opt_len = strlen(opt);
                   6090:                                                mem[opt_ofs] = opt_len;
                   6091:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6092:                                                dos_command = 1;
1.1.1.14  root     6093:                                                break;
1.1       root     6094:                                        }
                   6095:                                }
1.1.1.14  root     6096:                                break;
1.1       root     6097:                        }
                   6098:                }
                   6099:        }
                   6100:        
                   6101:        // load command file
                   6102:        strcpy(path, command);
                   6103:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6104:                sprintf(path, "%s.COM", command);
                   6105:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6106:                        sprintf(path, "%s.EXE", command);
                   6107:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     6108:                                sprintf(path, "%s.BAT", command);
                   6109:                                if(_access(path, 0) == 0) {
                   6110:                                        // this is a batch file, run command.com
                   6111:                                        char tmp[MAX_PATH];
                   6112:                                        if(opt_len != 0) {
                   6113:                                                sprintf(tmp, "/C %s %s", path, opt);
                   6114:                                        } else {
                   6115:                                                sprintf(tmp, "/C %s", path);
                   6116:                                        }
                   6117:                                        strcpy(opt, tmp);
                   6118:                                        opt_len = strlen(opt);
                   6119:                                        mem[opt_ofs] = opt_len;
                   6120:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6121:                                        strcpy(path, comspec_path);
                   6122:                                        strcpy(name_tmp, "COMMAND.COM");
                   6123:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6124:                                } else {
                   6125:                                        // search path in parent environments
                   6126:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45! root     6127:                                        const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14  root     6128:                                        if(env != NULL) {
                   6129:                                                char env_path[4096];
                   6130:                                                strcpy(env_path, env);
                   6131:                                                char *token = my_strtok(env_path, ";");
                   6132:                                                
                   6133:                                                while(token != NULL) {
                   6134:                                                        if(strlen(token) != 0) {
                   6135:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   6136:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6137:                                                                        break;
                   6138:                                                                }
                   6139:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   6140:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6141:                                                                        break;
                   6142:                                                                }
                   6143:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   6144:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6145:                                                                        break;
                   6146:                                                                }
                   6147:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   6148:                                                                if(_access(path, 0) == 0) {
                   6149:                                                                        // this is a batch file, run command.com
                   6150:                                                                        char tmp[MAX_PATH];
                   6151:                                                                        if(opt_len != 0) {
                   6152:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   6153:                                                                        } else {
                   6154:                                                                                sprintf(tmp, "/C %s", path);
                   6155:                                                                        }
                   6156:                                                                        strcpy(opt, tmp);
                   6157:                                                                        opt_len = strlen(opt);
                   6158:                                                                        mem[opt_ofs] = opt_len;
                   6159:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6160:                                                                        strcpy(path, comspec_path);
                   6161:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   6162:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6163:                                                                        break;
                   6164:                                                                }
1.1.1.8   root     6165:                                                        }
1.1.1.14  root     6166:                                                        token = my_strtok(NULL, ";");
1.1       root     6167:                                                }
                   6168:                                        }
                   6169:                                }
                   6170:                        }
                   6171:                }
                   6172:        }
                   6173:        if(fd == -1) {
1.1.1.38  root     6174:                // we can not find command.com in the path, so open comspec_path
                   6175:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6176:                        strcpy(command, comspec_path);
                   6177:                        strcpy(path, command);
                   6178:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6179:                }
                   6180:        }
                   6181:        if(fd == -1) {
1.1       root     6182:                if(dos_command) {
                   6183:                        // may be dos command
                   6184:                        char tmp[MAX_PATH];
                   6185:                        sprintf(tmp, "%s %s", command, opt);
                   6186:                        system(tmp);
                   6187:                        return(0);
                   6188:                } else {
                   6189:                        return(-1);
                   6190:                }
                   6191:        }
                   6192:        _read(fd, file_buffer, sizeof(file_buffer));
                   6193:        _close(fd);
                   6194:        
                   6195:        // copy environment
1.1.1.29  root     6196:        int umb_linked, env_seg, psp_seg;
1.1       root     6197:        
1.1.1.29  root     6198:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6199:                msdos_mem_unlink_umb();
                   6200:        }
1.1.1.8   root     6201:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6202:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6203:                        if(umb_linked != 0) {
                   6204:                                msdos_mem_link_umb();
                   6205:                        }
                   6206:                        return(-1);
                   6207:                }
1.1       root     6208:        }
                   6209:        if(param->env_seg == 0) {
                   6210:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6211:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6212:        } else {
                   6213:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6214:        }
                   6215:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6216:        
                   6217:        // check exe header
                   6218:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6219:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6220:        UINT16 cs, ss, ip, sp;
                   6221:        
                   6222:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6223:                // memory allocation
                   6224:                int header_size = header->header_size * 16;
                   6225:                int load_size = header->pages * 512 - header_size;
                   6226:                if(header_size + load_size < 512) {
                   6227:                        load_size = 512 - header_size;
                   6228:                }
                   6229:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6230:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6231:                        msdos_mem_free(env_seg);
                   6232:                        return(-1);
                   6233:                }
                   6234:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6235:                if(paragraphs > free_paragraphs) {
                   6236:                        paragraphs = free_paragraphs;
                   6237:                }
1.1.1.8   root     6238:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6239:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6240:                                if(umb_linked != 0) {
                   6241:                                        msdos_mem_link_umb();
                   6242:                                }
                   6243:                                msdos_mem_free(env_seg);
                   6244:                                return(-1);
                   6245:                        }
1.1       root     6246:                }
                   6247:                // relocation
                   6248:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6249:                for(int i = 0; i < header->relocations; i++) {
                   6250:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6251:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6252:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6253:                }
                   6254:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6255:                // segments
                   6256:                cs = header->init_cs + start_seg;
                   6257:                ss = header->init_ss + start_seg;
                   6258:                ip = header->init_ip;
                   6259:                sp = header->init_sp - 2; // for symdeb
                   6260:        } else {
                   6261:                // memory allocation
                   6262:                paragraphs = free_paragraphs;
1.1.1.8   root     6263:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6264:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6265:                                if(umb_linked != 0) {
                   6266:                                        msdos_mem_link_umb();
                   6267:                                }
                   6268:                                msdos_mem_free(env_seg);
                   6269:                                return(-1);
                   6270:                        }
1.1       root     6271:                }
                   6272:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6273:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6274:                // segments
                   6275:                cs = ss = psp_seg;
                   6276:                ip = 0x100;
                   6277:                sp = 0xfffe;
                   6278:        }
1.1.1.29  root     6279:        if(umb_linked != 0) {
                   6280:                msdos_mem_link_umb();
                   6281:        }
1.1       root     6282:        
                   6283:        // create psp
1.1.1.3   root     6284:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6285:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6286:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6287:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6288:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6289:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6290:        
                   6291:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6292:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6293:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6294:        
1.1.1.4   root     6295:        for(int i = 0; i < 8; i++) {
                   6296:                if(name_tmp[i] == '.') {
                   6297:                        mcb_psp->prog_name[i] = '\0';
                   6298:                        break;
                   6299:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6300:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6301:                        i++;
                   6302:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6303:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6304:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6305:                } else {
                   6306:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6307:                }
                   6308:        }
                   6309:        
1.1       root     6310:        // process info
                   6311:        process_t *process = msdos_process_info_create(psp_seg);
                   6312:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6313: #ifdef USE_DEBUGGER
                   6314:        strcpy(process->module_path, path);
                   6315: #endif
1.1       root     6316:        process->dta.w.l = 0x80;
                   6317:        process->dta.w.h = psp_seg;
                   6318:        process->switchar = '/';
                   6319:        process->max_files = 20;
                   6320:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6321:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6322:        process->parent_ds = SREG(DS);
1.1.1.31  root     6323:        process->parent_es = SREG(ES);
1.1       root     6324:        
                   6325:        current_psp = psp_seg;
1.1.1.23  root     6326:        msdos_sda_update(current_psp);
1.1       root     6327:        
                   6328:        if(al == 0x00) {
                   6329:                int_10h_feh_called = int_10h_ffh_called = false;
                   6330:                
1.1.1.38  root     6331:                // pipe
                   6332:                if(pipe_stdin_path[0] != '\0') {
1.1.1.45! root     6333: //                     if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
        !          6334:                        if(msdos_is_device_path(pipe_stdin_path)) {
        !          6335:                                fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
        !          6336:                        } else {
        !          6337:                                fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
        !          6338:                        }
        !          6339:                        if(fd != -1) {
        !          6340:                                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     6341:                                psp->file_table[0] = fd;
                   6342:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6343:                        }
                   6344:                }
                   6345:                if(pipe_stdout_path[0] != '\0') {
                   6346:                        if(_access(pipe_stdout_path, 0) == 0) {
                   6347:                                SetFileAttributes(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6348:                                DeleteFile(pipe_stdout_path);
                   6349:                        }
1.1.1.45! root     6350: //                     if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
        !          6351:                        if(msdos_is_device_path(pipe_stdout_path)) {
        !          6352:                                fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
        !          6353:                        } else {
        !          6354:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
        !          6355:                        }
        !          6356:                        if(fd != -1) {
        !          6357:                                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     6358:                                psp->file_table[1] = fd;
                   6359:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6360:                        }
                   6361:                }
1.1.1.39  root     6362:                if(pipe_stderr_path[0] != '\0') {
                   6363:                        if(_access(pipe_stderr_path, 0) == 0) {
                   6364:                                SetFileAttributes(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
                   6365:                                DeleteFile(pipe_stderr_path);
                   6366:                        }
1.1.1.45! root     6367: //                     if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
        !          6368:                        if(msdos_is_device_path(pipe_stderr_path)) {
        !          6369:                                fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
        !          6370:                        } else {
        !          6371:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
        !          6372:                        }
        !          6373:                        if(fd != -1) {
        !          6374:                                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     6375:                                psp->file_table[2] = fd;
                   6376:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6377:                        }
                   6378:                }
1.1.1.38  root     6379:                
1.1       root     6380:                // registers and segments
                   6381:                REG16(AX) = REG16(BX) = 0x00;
                   6382:                REG16(CX) = 0xff;
                   6383:                REG16(DX) = psp_seg;
                   6384:                REG16(SI) = ip;
                   6385:                REG16(DI) = sp;
                   6386:                REG16(SP) = sp;
1.1.1.3   root     6387:                SREG(DS) = SREG(ES) = psp_seg;
                   6388:                SREG(SS) = ss;
                   6389:                i386_load_segment_descriptor(DS);
                   6390:                i386_load_segment_descriptor(ES);
                   6391:                i386_load_segment_descriptor(SS);
1.1       root     6392:                
                   6393:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6394:                i386_jmp_far(cs, ip);
                   6395:        } else if(al == 0x01) {
                   6396:                // copy ss:sp and cs:ip to param block
                   6397:                param->sp = sp;
                   6398:                param->ss = ss;
                   6399:                param->ip = ip;
                   6400:                param->cs = cs;
1.1.1.31  root     6401:                
                   6402:                // the AX value to be passed to the child program is put on top of the child's stack
                   6403:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6404:        }
                   6405:        return(0);
                   6406: }
                   6407: 
                   6408: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6409: {
                   6410:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6411:        
                   6412:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6413:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6414:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6415:        
1.1.1.3   root     6416:        SREG(SS) = psp->stack.w.h;
                   6417:        i386_load_segment_descriptor(SS);
1.1       root     6418:        REG16(SP) = psp->stack.w.l;
                   6419:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6420:        
1.1.1.28  root     6421: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6422:        process_t *current_process = NULL;
                   6423:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6424:                if(process[i].psp == psp_seg) {
                   6425:                        current_process = &process[i];
                   6426:                        break;
                   6427:                }
                   6428:        }
                   6429:        if(current_process == NULL) {
                   6430:                throw(0x1f); // general failure
                   6431:        }
                   6432:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6433:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6434:        if(current_process->called_by_int2eh) {
                   6435:                REG16(AX) = ret;
                   6436:        }
                   6437:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6438:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6439:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6440:        i386_load_segment_descriptor(ES);
1.1       root     6441:        
                   6442:        if(mem_free) {
1.1.1.8   root     6443:                int mcb_seg;
                   6444:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6445:                        msdos_mem_free(mcb_seg + 1);
                   6446:                }
                   6447:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6448:                        msdos_mem_free(mcb_seg + 1);
                   6449:                }
1.1       root     6450:                
                   6451:                for(int i = 0; i < MAX_FILES; i++) {
                   6452:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6453:                                _close(i);
1.1.1.20  root     6454:                                msdos_file_handler_close(i);
                   6455:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6456:                        }
                   6457:                }
1.1.1.13  root     6458:                msdos_dta_info_free(psp_seg);
1.1       root     6459:        }
1.1.1.14  root     6460:        msdos_stdio_reopen();
1.1       root     6461:        
1.1.1.28  root     6462:        memset(current_process, 0, sizeof(process_t));
1.1       root     6463:        
                   6464:        current_psp = psp->parent_psp;
                   6465:        retval = ret;
1.1.1.23  root     6466:        msdos_sda_update(current_psp);
1.1       root     6467: }
                   6468: 
                   6469: // drive
                   6470: 
1.1.1.42  root     6471: int pcbios_update_drive_param(int drive_num, int force_update);
                   6472: 
1.1       root     6473: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6474: {
1.1.1.41  root     6475:        if(!(drive_num >= 0 && drive_num < 26)) {
                   6476:                return(0);
                   6477:        }
1.1.1.42  root     6478:        pcbios_update_drive_param(drive_num, force_update);
                   6479:        
                   6480:        drive_param_t *drive_param = &drive_params[drive_num];
1.1       root     6481:        *seg = DPB_TOP >> 4;
                   6482:        *ofs = sizeof(dpb_t) * drive_num;
                   6483:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6484:        
                   6485:        memset(dpb, 0, sizeof(dpb_t));
                   6486:        
1.1.1.41  root     6487:        dpb->drive_num = drive_num;
                   6488:        dpb->unit_num = drive_num;
1.1.1.42  root     6489:        
                   6490:        if(drive_param->valid) {
                   6491:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   6492:                
                   6493:                dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
                   6494:                dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
                   6495:                dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6496:                dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6497:                switch(geo->MediaType) {
                   6498:                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6499:                        dpb->media_type = 0xff;
                   6500:                        break;
                   6501:                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6502:                        dpb->media_type = 0xfe;
                   6503:                        break;
                   6504:                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6505:                        dpb->media_type = 0xfd;
                   6506:                        break;
                   6507:                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6508:                        dpb->media_type = 0xfc;
                   6509:                        break;
                   6510:                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6511:                case F3_1Pt2_512:
                   6512:                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6513:                case F5_720_512:
                   6514:                        dpb->media_type = 0xf9;
                   6515:                        break;
                   6516:                case FixedMedia:        // hard disk
                   6517:                case RemovableMedia:
                   6518:                case Unknown:
                   6519:                        dpb->media_type = 0xf8;
                   6520:                        break;
                   6521:                default:
                   6522:                        dpb->media_type = 0xf0;
                   6523:                        break;
                   6524:                }
                   6525:        }
1.1.1.41  root     6526:        dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
                   6527:        dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42  root     6528:        dpb->info_sector = 0xffff;
                   6529:        dpb->backup_boot_sector = 0xffff;
                   6530:        dpb->free_clusters = 0xffff;
                   6531:        dpb->free_search_cluster = 0xffffffff;
                   6532:        
                   6533:        return(drive_param->valid);
1.1       root     6534: }
                   6535: 
                   6536: // pc bios
                   6537: 
1.1.1.35  root     6538: #ifdef USE_SERVICE_THREAD
                   6539: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   6540: {
                   6541: #if defined(HAS_I386)
                   6542:        if(m_SF != 0) {
                   6543:                m_SF = 0;
                   6544:                mem[0xfffd0 + 0x15] = 0x79;     // jns -4
                   6545:        } else {
                   6546:                m_SF = 1;
                   6547:                mem[0xfffd0 + 0x15] = 0x78;     // js -4
                   6548:        }
                   6549: #else
                   6550:        if(m_SignVal < 0) {
                   6551:                m_SignVal = 0;
                   6552:                mem[0xfffd0 + 0x15] = 0x79;     // jns -4
                   6553:        } else {
                   6554:                m_SignVal = -1;
                   6555:                mem[0xfffd0 + 0x15] = 0x78;     // js -4
                   6556:        }
                   6557: #endif
                   6558:        i386_call_far(0xfffd, 0x0013);
                   6559:        in_service = true;
                   6560:        service_exit = false;
                   6561:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   6562: }
                   6563: 
                   6564: void finish_service_loop()
                   6565: {
                   6566:        if(in_service && service_exit) {
                   6567: #if defined(HAS_I386)
                   6568:                if(m_SF != 0) {
                   6569:                        m_SF = 0;
                   6570:                } else {
                   6571:                        m_SF = 1;
                   6572:                }
                   6573: #else
                   6574:                if(m_SignVal < 0) {
                   6575:                        m_SignVal = 0;
                   6576:                } else {
                   6577:                        m_SignVal = -1;
                   6578:                }
                   6579: #endif
                   6580:                in_service = false;
                   6581:        }
                   6582: }
                   6583: #endif
                   6584: 
1.1.1.19  root     6585: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   6586: {
                   6587:        static unsigned __int64 start_msec_since_midnight = 0;
                   6588:        static unsigned __int64 start_msec_since_hostboot = 0;
                   6589:        
                   6590:        if(start_msec_since_midnight == 0) {
                   6591:                SYSTEMTIME time;
                   6592:                GetLocalTime(&time);
                   6593:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   6594:                start_msec_since_hostboot = cur_msec;
                   6595:        }
                   6596:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   6597:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   6598:        return (UINT32)tick;
                   6599: }
                   6600: 
                   6601: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   6602: {
                   6603:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   6604:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   6605:        
                   6606:        if(prev_tick > next_tick) {
                   6607:                mem[0x470] = 1;
                   6608:        }
                   6609:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   6610: }
                   6611: 
1.1.1.14  root     6612: inline void pcbios_irq0()
                   6613: {
                   6614:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     6615:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     6616: }
                   6617: 
1.1.1.16  root     6618: int pcbios_get_text_vram_address(int page)
1.1       root     6619: {
                   6620:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6621:                return TEXT_VRAM_TOP;
1.1       root     6622:        } else {
1.1.1.14  root     6623:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     6624:        }
                   6625: }
                   6626: 
1.1.1.16  root     6627: int pcbios_get_shadow_buffer_address(int page)
1.1       root     6628: {
1.1.1.14  root     6629:        if(!int_10h_feh_called) {
1.1.1.16  root     6630:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     6631:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6632:                return SHADOW_BUF_TOP;
                   6633:        } else {
1.1.1.14  root     6634:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     6635:        }
                   6636: }
                   6637: 
1.1.1.16  root     6638: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     6639: {
1.1.1.16  root     6640:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     6641: }
                   6642: 
1.1.1.16  root     6643: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     6644: {
1.1.1.14  root     6645:        // clear the existing screen, not just the new one
                   6646:        int clr_height = max(height, scr_height);
                   6647:        
1.1.1.16  root     6648:        if(scr_width != width || scr_height != height) {
                   6649:                change_console_size(width, height);
1.1.1.14  root     6650:        }
                   6651:        mem[0x462] = 0;
                   6652:        *(UINT16 *)(mem + 0x44e) = 0;
                   6653:        
1.1.1.16  root     6654:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   6655:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   6656:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   6657:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1       root     6658:        
1.1.1.23  root     6659:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     6660:        if(clr_screen) {
1.1.1.14  root     6661:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   6662:                        mem[ofs++] = 0x20;
                   6663:                        mem[ofs++] = 0x07;
                   6664:                }
                   6665:                
1.1.1.35  root     6666: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6667:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6668: #endif
1.1.1.14  root     6669:                for(int y = 0; y < clr_height; y++) {
                   6670:                        for(int x = 0; x < scr_width; x++) {
                   6671:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6672:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     6673:                        }
                   6674:                }
                   6675:                SMALL_RECT rect;
1.1.1.14  root     6676:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   6677:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6678:                vram_length_char = vram_last_length_char = 0;
                   6679:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     6680: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6681:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6682: #endif
1.1       root     6683:        }
1.1.1.14  root     6684:        COORD co;
                   6685:        co.X = 0;
                   6686:        co.Y = scr_top;
                   6687:        SetConsoleCursorPosition(hStdout, co);
                   6688:        cursor_moved = true;
                   6689:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     6690: }
                   6691: 
1.1.1.36  root     6692: void pcbios_update_cursor_position()
                   6693: {
                   6694:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6695:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   6696:        if(!restore_console_on_exit) {
                   6697:                scr_top = csbi.srWindow.Top;
                   6698:        }
                   6699:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   6700:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   6701: }
                   6702: 
1.1.1.16  root     6703: inline void pcbios_int_10h_00h()
                   6704: {
                   6705:        switch(REG8(AL) & 0x7f) {
                   6706:        case 0x70: // v-text mode
                   6707:        case 0x71: // extended cga v-text mode
                   6708:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   6709:                break;
                   6710:        default:
                   6711:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   6712:                break;
                   6713:        }
                   6714:        if(REG8(AL) & 0x80) {
                   6715:                mem[0x487] |= 0x80;
                   6716:        } else {
                   6717:                mem[0x487] &= ~0x80;
                   6718:        }
                   6719:        mem[0x449] = REG8(AL) & 0x7f;
                   6720: }
                   6721: 
1.1       root     6722: inline void pcbios_int_10h_01h()
                   6723: {
1.1.1.13  root     6724:        mem[0x460] = REG8(CL);
                   6725:        mem[0x461] = REG8(CH);
1.1.1.14  root     6726:        
1.1.1.23  root     6727:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6728:        CONSOLE_CURSOR_INFO ci;
                   6729:        GetConsoleCursorInfo(hStdout, &ci);
                   6730: //     ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
                   6731: //     if(ci.bVisible) {
                   6732:                int lines = max(8, REG8(CL) + 1);
                   6733:                ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
                   6734: //     }
                   6735:        SetConsoleCursorInfo(hStdout, &ci);
1.1       root     6736: }
                   6737: 
                   6738: inline void pcbios_int_10h_02h()
                   6739: {
1.1.1.14  root     6740:        // continuously setting the cursor effectively stops it blinking
                   6741:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     6742:                COORD co;
                   6743:                co.X = REG8(DL);
1.1.1.14  root     6744:                co.Y = REG8(DH) + scr_top;
                   6745:                
                   6746:                // some programs hide the cursor by moving it off screen
                   6747:                static bool hidden = false;
1.1.1.23  root     6748:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6749:                CONSOLE_CURSOR_INFO ci;
                   6750:                GetConsoleCursorInfo(hStdout, &ci);
                   6751:                
                   6752:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   6753:                        if(ci.bVisible) {
                   6754:                                ci.bVisible = FALSE;
                   6755: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6756:                                hidden = true;
                   6757:                        }
                   6758:                } else if(hidden) {
                   6759:                        if(!ci.bVisible) {
                   6760:                                ci.bVisible = TRUE;
                   6761: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6762:                        }
                   6763:                        hidden = false;
                   6764:                }
1.1       root     6765:        }
1.1.1.14  root     6766:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   6767:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     6768: }
                   6769: 
                   6770: inline void pcbios_int_10h_03h()
                   6771: {
1.1.1.14  root     6772:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6773:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6774:        REG8(CL) = mem[0x460];
                   6775:        REG8(CH) = mem[0x461];
                   6776: }
                   6777: 
                   6778: inline void pcbios_int_10h_05h()
                   6779: {
1.1.1.14  root     6780:        if(REG8(AL) >= vram_pages) {
                   6781:                return;
                   6782:        }
                   6783:        if(mem[0x462] != REG8(AL)) {
                   6784:                vram_flush();
                   6785:                
1.1.1.23  root     6786:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6787:                SMALL_RECT rect;
1.1.1.14  root     6788:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6789:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6790:                
1.1.1.16  root     6791:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     6792:                        for(int x = 0; x < scr_width; x++) {
                   6793:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6794:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6795:                        }
                   6796:                }
1.1.1.16  root     6797:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     6798:                        for(int x = 0; x < scr_width; x++) {
                   6799:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   6800:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     6801:                        }
                   6802:                }
1.1.1.14  root     6803:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6804:                
                   6805:                COORD co;
1.1.1.14  root     6806:                co.X = mem[0x450 + REG8(AL) * 2];
                   6807:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   6808:                if(co.Y < scr_top + scr_height) {
                   6809:                        SetConsoleCursorPosition(hStdout, co);
                   6810:                }
1.1       root     6811:        }
1.1.1.14  root     6812:        mem[0x462] = REG8(AL);
                   6813:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   6814:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     6815:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     6816:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     6817:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     6818:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1       root     6819: }
                   6820: 
                   6821: inline void pcbios_int_10h_06h()
                   6822: {
1.1.1.14  root     6823:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6824:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6825:                return;
                   6826:        }
                   6827:        vram_flush();
                   6828:        
1.1.1.23  root     6829:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6830:        SMALL_RECT rect;
1.1.1.14  root     6831:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6832:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6833:        
                   6834:        int right = min(REG8(DL), scr_width - 1);
                   6835:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6836:        
                   6837:        if(REG8(AL) == 0) {
1.1.1.14  root     6838:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6839:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6840:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6841:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6842:                        }
                   6843:                }
                   6844:        } else {
1.1.1.14  root     6845:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     6846:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6847:                                if(y2 <= bottom) {
                   6848:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6849:                                } else {
1.1.1.14  root     6850:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6851:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6852:                                }
1.1.1.14  root     6853:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6854:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6855:                        }
                   6856:                }
                   6857:        }
1.1.1.14  root     6858:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6859: }
                   6860: 
                   6861: inline void pcbios_int_10h_07h()
                   6862: {
1.1.1.14  root     6863:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6864:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6865:                return;
                   6866:        }
                   6867:        vram_flush();
                   6868:        
1.1.1.23  root     6869:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6870:        SMALL_RECT rect;
1.1.1.14  root     6871:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6872:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6873:        
                   6874:        int right = min(REG8(DL), scr_width - 1);
                   6875:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6876:        
                   6877:        if(REG8(AL) == 0) {
1.1.1.14  root     6878:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6879:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6880:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6881:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6882:                        }
                   6883:                }
                   6884:        } else {
1.1.1.14  root     6885:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     6886:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6887:                                if(y2 >= REG8(CH)) {
                   6888:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6889:                                } else {
1.1.1.14  root     6890:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6891:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6892:                                }
1.1.1.14  root     6893:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6894:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6895:                        }
                   6896:                }
                   6897:        }
1.1.1.14  root     6898:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6899: }
                   6900: 
                   6901: inline void pcbios_int_10h_08h()
                   6902: {
                   6903:        COORD co;
                   6904:        DWORD num;
                   6905:        
1.1.1.14  root     6906:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6907:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6908:        
                   6909:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6910:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6911:                co.Y += scr_top;
                   6912:                vram_flush();
1.1       root     6913:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   6914:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   6915:                REG8(AL) = scr_char[0];
                   6916:                REG8(AH) = scr_attr[0];
                   6917:        } else {
1.1.1.16  root     6918:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     6919:        }
                   6920: }
                   6921: 
                   6922: inline void pcbios_int_10h_09h()
                   6923: {
                   6924:        COORD co;
                   6925:        
1.1.1.14  root     6926:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6927:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6928:        
1.1.1.16  root     6929:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   6930:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     6931:        
                   6932:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6933: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6934:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6935: #endif
1.1.1.16  root     6936:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6937:                while(dest < end) {
                   6938:                        write_text_vram_char(dest - vram, REG8(AL));
                   6939:                        mem[dest++] = REG8(AL);
                   6940:                        write_text_vram_attr(dest - vram, REG8(BL));
                   6941:                        mem[dest++] = REG8(BL);
1.1       root     6942:                }
1.1.1.35  root     6943: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6944:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6945: #endif
1.1       root     6946:        } else {
1.1.1.14  root     6947:                while(dest < end) {
1.1       root     6948:                        mem[dest++] = REG8(AL);
                   6949:                        mem[dest++] = REG8(BL);
                   6950:                }
                   6951:        }
                   6952: }
                   6953: 
                   6954: inline void pcbios_int_10h_0ah()
                   6955: {
                   6956:        COORD co;
                   6957:        
1.1.1.14  root     6958:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6959:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6960:        
1.1.1.16  root     6961:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   6962:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     6963:        
                   6964:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6965: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6966:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6967: #endif
1.1.1.16  root     6968:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6969:                while(dest < end) {
                   6970:                        write_text_vram_char(dest - vram, REG8(AL));
                   6971:                        mem[dest++] = REG8(AL);
                   6972:                        dest++;
1.1       root     6973:                }
1.1.1.35  root     6974: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6975:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6976: #endif
1.1       root     6977:        } else {
1.1.1.14  root     6978:                while(dest < end) {
1.1       root     6979:                        mem[dest++] = REG8(AL);
                   6980:                        dest++;
                   6981:                }
                   6982:        }
                   6983: }
                   6984: 
1.1.1.40  root     6985: HDC get_console_window_device_context()
                   6986: {
                   6987:        static HWND hwndFound = 0;
                   6988:        
                   6989:        if(hwndFound == 0) {
                   6990:                // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
                   6991:                char pszNewWindowTitle[1024];
                   6992:                char pszOldWindowTitle[1024];
                   6993:                
                   6994:                GetConsoleTitle(pszOldWindowTitle, 1024);
                   6995:                wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
                   6996:                SetConsoleTitle(pszNewWindowTitle);
                   6997:                Sleep(100);
                   6998:                hwndFound = FindWindow(NULL, pszNewWindowTitle);
                   6999:                SetConsoleTitle(pszOldWindowTitle);
                   7000:        }
                   7001:        return GetDC(hwndFound);
                   7002: }
                   7003: 
                   7004: inline void pcbios_int_10h_0ch()
                   7005: {
                   7006:        HDC hdc = get_console_window_device_context();
                   7007:        
                   7008:        if(hdc != NULL) {
                   7009:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
                   7010:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
                   7011:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
                   7012:                
                   7013:                if(REG8(AL) & 0x80) {
                   7014:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7015:                        if(color != CLR_INVALID) {
                   7016:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7017:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7018:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
                   7019:                        }
                   7020:                }
                   7021:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
                   7022:        }
                   7023: }
                   7024: 
                   7025: inline void pcbios_int_10h_0dh()
                   7026: {
                   7027:        HDC hdc = get_console_window_device_context();
                   7028:        BYTE r = 0;
                   7029:        BYTE g = 0;
                   7030:        BYTE b = 0;
                   7031:        
                   7032:        if(hdc != NULL) {
                   7033:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7034:                if(color != CLR_INVALID) {
                   7035:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7036:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7037:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
                   7038:                }
                   7039:        }
                   7040:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
                   7041: }
                   7042: 
1.1       root     7043: inline void pcbios_int_10h_0eh()
                   7044: {
1.1.1.14  root     7045:        DWORD num;
                   7046:        COORD co;
                   7047:        
                   7048:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7049:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7050:        
                   7051:        if(REG8(AL) == 7) {
                   7052:                //MessageBeep(-1);
                   7053:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   7054:                if(REG8(AL) == 10) {
                   7055:                        vram_flush();
                   7056:                }
1.1.1.23  root     7057:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     7058:                cursor_moved = true;
                   7059:        } else {
1.1.1.16  root     7060:                int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
1.1.1.14  root     7061:                if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7062: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7063:                        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7064: #endif
1.1.1.16  root     7065:                        int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7066:                        write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     7067: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7068:                        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7069: #endif
1.1.1.14  root     7070:                        
1.1.1.23  root     7071:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7072:                        if(++co.X == scr_width) {
                   7073:                                co.X = 0;
                   7074:                                if(++co.Y == scr_height) {
                   7075:                                        vram_flush();
                   7076:                                        WriteConsole(hStdout, "\n", 1, &num, NULL);
                   7077:                                        cursor_moved = true;
                   7078:                                }
                   7079:                        }
                   7080:                        if(!cursor_moved) {
                   7081:                                co.Y += scr_top;
                   7082:                                SetConsoleCursorPosition(hStdout, co);
                   7083:                                cursor_moved = true;
                   7084:                        }
                   7085:                }
                   7086:                mem[dest] = REG8(AL);
                   7087:        }
1.1       root     7088: }
                   7089: 
                   7090: inline void pcbios_int_10h_0fh()
                   7091: {
                   7092:        REG8(AL) = mem[0x449];
                   7093:        REG8(AH) = mem[0x44a];
                   7094:        REG8(BH) = mem[0x462];
                   7095: }
                   7096: 
1.1.1.14  root     7097: inline void pcbios_int_10h_11h()
                   7098: {
                   7099:        switch(REG8(AL)) {
1.1.1.16  root     7100:        case 0x01:
1.1.1.14  root     7101:        case 0x11:
1.1.1.16  root     7102:                pcbios_set_console_size(80, 28, true);
1.1.1.14  root     7103:                break;
1.1.1.16  root     7104:        case 0x02:
1.1.1.14  root     7105:        case 0x12:
1.1.1.16  root     7106:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     7107:                break;
1.1.1.16  root     7108:        case 0x04:
1.1.1.14  root     7109:        case 0x14:
1.1.1.16  root     7110:                pcbios_set_console_size(80, 25, true);
                   7111:                break;
                   7112:        case 0x18:
                   7113:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     7114:                break;
                   7115:        case 0x30:
                   7116:                SREG(ES) = 0;
                   7117:                i386_load_segment_descriptor(ES);
                   7118:                REG16(BP) = 0;
                   7119:                REG16(CX) = mem[0x485];
                   7120:                REG8(DL) = mem[0x484];
                   7121:                break;
                   7122:        }
                   7123: }
                   7124: 
                   7125: inline void pcbios_int_10h_12h()
                   7126: {
1.1.1.16  root     7127:        switch(REG8(BL)) {
                   7128:        case 0x10:
1.1.1.14  root     7129:                REG16(BX) = 0x0003;
                   7130:                REG16(CX) = 0x0009;
1.1.1.16  root     7131:                break;
1.1.1.14  root     7132:        }
                   7133: }
                   7134: 
1.1       root     7135: inline void pcbios_int_10h_13h()
                   7136: {
1.1.1.3   root     7137:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     7138:        COORD co;
                   7139:        DWORD num;
                   7140:        
                   7141:        co.X = REG8(DL);
1.1.1.14  root     7142:        co.Y = REG8(DH) + scr_top;
                   7143:        
                   7144:        vram_flush();
1.1       root     7145:        
                   7146:        switch(REG8(AL)) {
                   7147:        case 0x00:
                   7148:        case 0x01:
                   7149:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7150:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7151:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7152:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7153:                        SetConsoleCursorPosition(hStdout, co);
                   7154:                        
                   7155:                        if(csbi.wAttributes != REG8(BL)) {
                   7156:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   7157:                        }
1.1.1.14  root     7158:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   7159:                        
1.1       root     7160:                        if(csbi.wAttributes != REG8(BL)) {
                   7161:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7162:                        }
                   7163:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     7164:                                if(!restore_console_on_exit) {
                   7165:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7166:                                        scr_top = csbi.srWindow.Top;
                   7167:                                }
1.1.1.14  root     7168:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7169:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7170:                                SetConsoleCursorPosition(hStdout, co);
                   7171:                        } else {
                   7172:                                cursor_moved = true;
                   7173:                        }
                   7174:                } else {
1.1.1.3   root     7175:                        m_CF = 1;
1.1       root     7176:                }
                   7177:                break;
                   7178:        case 0x02:
                   7179:        case 0x03:
                   7180:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7181:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7182:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7183:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7184:                        SetConsoleCursorPosition(hStdout, co);
                   7185:                        
                   7186:                        WORD wAttributes = csbi.wAttributes;
                   7187:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7188:                                if(wAttributes != mem[ofs + 1]) {
                   7189:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7190:                                        wAttributes = mem[ofs + 1];
                   7191:                                }
1.1.1.14  root     7192:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7193:                        }
                   7194:                        if(csbi.wAttributes != wAttributes) {
                   7195:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7196:                        }
                   7197:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7198:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7199:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7200:                                SetConsoleCursorPosition(hStdout, co);
                   7201:                        } else {
                   7202:                                cursor_moved = true;
                   7203:                        }
                   7204:                } else {
1.1.1.3   root     7205:                        m_CF = 1;
1.1       root     7206:                }
                   7207:                break;
                   7208:        case 0x10:
                   7209:        case 0x11:
                   7210:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7211:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7212:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   7213:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7214:                        for(int i = 0; i < num; i++) {
                   7215:                                mem[ofs++] = scr_char[i];
                   7216:                                mem[ofs++] = scr_attr[i];
1.1.1.45! root     7217:                                if(REG8(AL) & 0x01) {
1.1       root     7218:                                        mem[ofs++] = 0;
                   7219:                                        mem[ofs++] = 0;
                   7220:                                }
                   7221:                        }
                   7222:                } else {
1.1.1.16  root     7223:                        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     7224:                                mem[ofs++] = mem[src++];
                   7225:                                mem[ofs++] = mem[src++];
1.1.1.45! root     7226:                                if(REG8(AL) & 0x01) {
1.1       root     7227:                                        mem[ofs++] = 0;
                   7228:                                        mem[ofs++] = 0;
                   7229:                                }
1.1.1.14  root     7230:                                if(++co.X == scr_width) {
                   7231:                                        if(++co.Y == scr_height) {
1.1       root     7232:                                                break;
                   7233:                                        }
                   7234:                                        co.X = 0;
                   7235:                                }
                   7236:                        }
                   7237:                }
                   7238:                break;
1.1.1.45! root     7239:        case 0x12: // ???
        !          7240:        case 0x13: // ???
1.1       root     7241:        case 0x20:
                   7242:        case 0x21:
                   7243:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7244:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7245:                        int len = min(REG16(CX), scr_width * scr_height);
                   7246:                        for(int i = 0; i < len; i++) {
1.1       root     7247:                                scr_char[i] = mem[ofs++];
                   7248:                                scr_attr[i] = mem[ofs++];
1.1.1.45! root     7249:                                if(REG8(AL) & 0x01) {
1.1       root     7250:                                        ofs += 2;
                   7251:                                }
                   7252:                        }
1.1.1.14  root     7253:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7254:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7255:                } else {
1.1.1.16  root     7256:                        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     7257:                                mem[dest++] = mem[ofs++];
                   7258:                                mem[dest++] = mem[ofs++];
1.1.1.45! root     7259:                                if(REG8(AL) & 0x01) {
1.1       root     7260:                                        ofs += 2;
                   7261:                                }
1.1.1.14  root     7262:                                if(++co.X == scr_width) {
                   7263:                                        if(++co.Y == scr_height) {
1.1       root     7264:                                                break;
                   7265:                                        }
                   7266:                                        co.X = 0;
                   7267:                                }
                   7268:                        }
                   7269:                }
                   7270:                break;
                   7271:        default:
1.1.1.22  root     7272:                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     7273:                m_CF = 1;
1.1       root     7274:                break;
                   7275:        }
                   7276: }
                   7277: 
1.1.1.30  root     7278: inline void pcbios_int_10h_18h()
                   7279: {
                   7280:        switch(REG8(AL)) {
                   7281:        case 0x00:
                   7282:        case 0x01:
                   7283: //             REG8(AL) = 0x86;
                   7284:                REG8(AL) = 0x00;
                   7285:                break;
                   7286:        default:
                   7287:                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));
                   7288:                m_CF = 1;
                   7289:                break;
                   7290:        }
                   7291: }
                   7292: 
1.1.1.14  root     7293: inline void pcbios_int_10h_1ah()
                   7294: {
                   7295:        switch(REG8(AL)) {
                   7296:        case 0x00:
                   7297:                REG8(AL) = 0x1a;
                   7298:                REG8(BL) = 0x08;
                   7299:                REG8(BH) = 0x00;
                   7300:                break;
                   7301:        default:
1.1.1.22  root     7302:                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     7303:                m_CF = 1;
                   7304:                break;
                   7305:        }
                   7306: }
                   7307: 
1.1       root     7308: inline void pcbios_int_10h_1dh()
                   7309: {
                   7310:        switch(REG8(AL)) {
1.1.1.43  root     7311:        case 0x00:
                   7312:                // DOS/V Shift Status Line Control is not supported
                   7313:                m_CF = 1;
                   7314:                break;
1.1       root     7315:        case 0x01:
                   7316:                break;
                   7317:        case 0x02:
                   7318:                REG16(BX) = 0;
                   7319:                break;
                   7320:        default:
1.1.1.22  root     7321:                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));
                   7322:                m_CF = 1;
                   7323:                break;
                   7324:        }
                   7325: }
                   7326: 
                   7327: inline void pcbios_int_10h_4fh()
                   7328: {
                   7329:        switch(REG8(AL)) {
                   7330:        case 0x00:
                   7331:                REG8(AH) = 0x02; // not supported
                   7332:                break;
                   7333:        case 0x01:
                   7334:        case 0x02:
                   7335:        case 0x03:
                   7336:        case 0x04:
                   7337:        case 0x05:
                   7338:        case 0x06:
                   7339:        case 0x07:
                   7340:        case 0x08:
                   7341:        case 0x09:
                   7342:        case 0x0a:
                   7343:        case 0x0b:
                   7344:        case 0x0c:
                   7345:                REG8(AH) = 0x01; // failed
                   7346:                break;
                   7347:        default:
                   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: 
                   7354: inline void pcbios_int_10h_82h()
                   7355: {
                   7356:        static UINT8 mode = 0;
                   7357:        
                   7358:        switch(REG8(AL)) {
1.1.1.22  root     7359:        case 0x00:
1.1       root     7360:                if(REG8(BL) != 0xff) {
                   7361:                        mode = REG8(BL);
                   7362:                }
                   7363:                REG8(AL) = mode;
                   7364:                break;
                   7365:        default:
1.1.1.22  root     7366:                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     7367:                m_CF = 1;
1.1       root     7368:                break;
                   7369:        }
                   7370: }
                   7371: 
1.1.1.22  root     7372: inline void pcbios_int_10h_83h()
                   7373: {
                   7374:        static UINT8 mode = 0;
                   7375:        
                   7376:        switch(REG8(AL)) {
                   7377:        case 0x00:
                   7378:                REG16(AX) = 0; // offset???
                   7379:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7380:                i386_load_segment_descriptor(ES);
                   7381:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7382:                break;
                   7383:        default:
                   7384:                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));
                   7385:                m_CF = 1;
                   7386:                break;
                   7387:        }
                   7388: }
                   7389: 
                   7390: inline void pcbios_int_10h_90h()
                   7391: {
                   7392:        REG8(AL) = mem[0x449];
                   7393: }
                   7394: 
                   7395: inline void pcbios_int_10h_91h()
                   7396: {
                   7397:        REG8(AL) = 0x04; // VGA
                   7398: }
                   7399: 
                   7400: inline void pcbios_int_10h_efh()
                   7401: {
                   7402:        REG16(DX) = 0xffff;
                   7403: }
                   7404: 
1.1       root     7405: inline void pcbios_int_10h_feh()
                   7406: {
                   7407:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7408:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7409:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7410:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7411:        }
                   7412:        int_10h_feh_called = true;
                   7413: }
                   7414: 
                   7415: inline void pcbios_int_10h_ffh()
                   7416: {
                   7417:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7418:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7419:                COORD co;
                   7420:                DWORD num;
                   7421:                
1.1.1.14  root     7422:                vram_flush();
                   7423:                
                   7424:                co.X = (REG16(DI) >> 1) % scr_width;
                   7425:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7426:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7427:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7428:                int len;
                   7429:                for(len = 0; ofs < end; len++) {
                   7430:                        scr_char[len] = mem[ofs++];
                   7431:                        scr_attr[len] = mem[ofs++];
                   7432:                }
                   7433:                co.Y += scr_top;
                   7434:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7435:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7436:        }
                   7437:        int_10h_ffh_called = true;
                   7438: }
                   7439: 
1.1.1.42  root     7440: int pcbios_update_drive_param(int drive_num, int force_update)
                   7441: {
                   7442:        if(drive_num >= 0 && drive_num < 26) {
                   7443:                drive_param_t *drive_param = &drive_params[drive_num];
                   7444:                
                   7445:                if(force_update || !drive_param->initialized) {
                   7446:                        drive_param->valid = 0;
                   7447:                        char dev[64];
                   7448:                        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7449:                        
                   7450:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   7451:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7452:                                DWORD dwSize;
                   7453:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
                   7454:                                        drive_param->valid = 1;
                   7455:                                }
                   7456:                                CloseHandle(hFile);
                   7457:                        }
                   7458:                        drive_param->initialized = 1;
                   7459:                }
                   7460:                return(drive_param->valid);
                   7461:        }
                   7462:        return(0);
                   7463: }
                   7464: 
                   7465: inline void pcbios_int_13h_00h()
                   7466: {
                   7467:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7468:        
                   7469:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7470:                REG8(AH) = 0x00; // successful completion
                   7471:        } else {
                   7472:                if(REG8(DL) & 0x80) {
                   7473:                        REG8(AH) = 0x05; // reset failed (hard disk)
                   7474:                } else {
                   7475:                        REG8(AH) = 0x80; // timeout (not ready)
                   7476:                }
                   7477:                m_CF = 1;
                   7478:        }
                   7479: }
                   7480: 
                   7481: inline void pcbios_int_13h_02h()
                   7482: {
                   7483:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7484:        
                   7485:        if(REG8(AL) == 0) {
                   7486:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7487:                m_CF = 1;
                   7488:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7489:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7490:                m_CF = 1;
                   7491:        } else {
                   7492:                drive_param_t *drive_param = &drive_params[drive_num];
                   7493:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7494:                char dev[64];
                   7495:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7496:                
                   7497:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7498:                if(hFile == INVALID_HANDLE_VALUE) {
                   7499:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7500:                        m_CF = 1;
                   7501:                } else {
                   7502:                        UINT32 sector_num = REG8(AL);
                   7503:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7504:                        UINT32 head = REG8(DH);
                   7505:                        UINT32 sector = REG8(CL) & 0x3f;
                   7506:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7507:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7508:                        DWORD dwSize;
                   7509:                        
                   7510: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7511: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7512: //                             m_CF = 1;
                   7513: //                     } else 
                   7514:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7515:                                REG8(AH) = 0x04; // sector not found/read error
                   7516:                                m_CF = 1;
                   7517:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7518:                                REG8(AH) = 0x04; // sector not found/read error
                   7519:                                m_CF = 1;
                   7520:                        } else {
                   7521:                                REG8(AH) = 0x00; // successful completion
                   7522:                        }
                   7523:                        CloseHandle(hFile);
                   7524:                }
                   7525:        }
                   7526: }
                   7527: 
                   7528: inline void pcbios_int_13h_03h()
                   7529: {
                   7530:        // this operation may cause serious damage for drives, so support only floppy disk...
                   7531:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7532:        
                   7533:        if(REG8(AL) == 0) {
                   7534:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7535:                m_CF = 1;
                   7536:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7537:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7538:                m_CF = 1;
                   7539:        } else if(!drive_params[drive_num].is_fdd()) {
                   7540:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7541:                m_CF = 1;
                   7542:        } else {
                   7543:                drive_param_t *drive_param = &drive_params[drive_num];
                   7544:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7545:                char dev[64];
                   7546:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7547:                
                   7548:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7549:                if(hFile == INVALID_HANDLE_VALUE) {
                   7550:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7551:                        m_CF = 1;
                   7552:                } else {
                   7553:                        UINT32 sector_num = REG8(AL);
                   7554:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7555:                        UINT32 head = REG8(DH);
                   7556:                        UINT32 sector = REG8(CL) & 0x3f;
                   7557:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7558:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7559:                        DWORD dwSize;
                   7560:                        
                   7561: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7562: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7563: //                             m_CF = 1;
                   7564: //                     } else 
                   7565:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7566:                                REG8(AH) = 0x04; // sector not found/read error
                   7567:                                m_CF = 1;
                   7568:                        } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7569:                                REG8(AH) = 0x04; // sector not found/read error
                   7570:                                m_CF = 1;
                   7571:                        } else {
                   7572:                                REG8(AH) = 0x00; // successful completion
                   7573:                        }
                   7574:                        CloseHandle(hFile);
                   7575:                }
                   7576:        }
                   7577: }
                   7578: 
                   7579: inline void pcbios_int_13h_04h()
                   7580: {
                   7581:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7582:        
                   7583:        if(REG8(AL) == 0) {
                   7584:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7585:                m_CF = 1;
                   7586:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7587:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7588:                m_CF = 1;
                   7589:        } else {
                   7590:                drive_param_t *drive_param = &drive_params[drive_num];
                   7591:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7592:                char dev[64];
                   7593:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7594:                
                   7595:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7596:                if(hFile == INVALID_HANDLE_VALUE) {
                   7597:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7598:                        m_CF = 1;
                   7599:                } else {
                   7600:                        UINT32 sector_num = REG8(AL);
                   7601:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7602:                        UINT32 head = REG8(DH);
                   7603:                        UINT32 sector = REG8(CL) & 0x3f;
                   7604:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7605:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7606:                        DWORD dwSize;
                   7607:                        UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
                   7608:                        
                   7609: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7610: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7611: //                             m_CF = 1;
                   7612: //                     } else 
                   7613:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7614:                                REG8(AH) = 0x04; // sector not found/read error
                   7615:                                m_CF = 1;
                   7616:                        } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7617:                                REG8(AH) = 0x04; // sector not found/read error
                   7618:                                m_CF = 1;
                   7619:                        } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
                   7620:                                REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
                   7621:                                m_CF = 1;
                   7622:                        } else {
                   7623:                                REG8(AH) = 0x00; // successful completion
                   7624:                        }
                   7625:                        free(tmp_buffer);
                   7626:                        CloseHandle(hFile);
                   7627:                }
                   7628:        }
                   7629: }
                   7630: 
                   7631: inline void pcbios_int_13h_08h()
                   7632: {
                   7633:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7634:        
                   7635:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7636:                drive_param_t *drive_param = &drive_params[drive_num];
                   7637:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7638:                
                   7639:                REG16(AX) = 0x0000;
                   7640:                switch(geo->MediaType) {
                   7641:                case F5_360_512:
                   7642:                case F5_320_512:
                   7643:                case F5_320_1024:
                   7644:                case F5_180_512:
                   7645:                case F5_160_512:
                   7646:                        REG8(BL) = 0x01; // 320K/360K disk
                   7647:                        break;
                   7648:                case F5_1Pt2_512:
                   7649:                case F3_1Pt2_512:
                   7650:                case F3_1Pt23_1024:
                   7651:                case F5_1Pt23_1024:
                   7652:                        REG8(BL) = 0x02; // 1.2M disk
                   7653:                        break;
                   7654:                case F3_720_512:
                   7655:                case F3_640_512:
                   7656:                case F5_640_512:
                   7657:                case F5_720_512:
                   7658:                        REG8(BL) = 0x03; // 720K disk
                   7659:                        break;
                   7660:                case F3_1Pt44_512:
                   7661:                        REG8(BL) = 0x04; // 1.44M disk
                   7662:                        break;
                   7663:                case F3_2Pt88_512:
                   7664:                        REG8(BL) = 0x06; // 2.88M disk
                   7665:                        break;
                   7666:                case RemovableMedia:
                   7667:                        REG8(BL) = 0x10; // ATAPI Removable Media Device
                   7668:                        break;
                   7669:                default:
                   7670:                        REG8(BL) = 0x00; // unknown
                   7671:                        break;
                   7672:                }
                   7673:                if(REG8(DL) & 0x80) {
                   7674:                        switch(GetLogicalDrives() & 0x0c) {
                   7675:                        case 0x00: REG8(DL) = 0x00; break;
                   7676:                        case 0x04:
                   7677:                        case 0x08: REG8(DL) = 0x01; break;
                   7678:                        case 0x0c: REG8(DL) = 0x02; break;
                   7679:                        }
                   7680:                } else {
                   7681:                        switch(GetLogicalDrives() & 0x03) {
                   7682:                        case 0x00: REG8(DL) = 0x00; break;
                   7683:                        case 0x01:
                   7684:                        case 0x02: REG8(DL) = 0x01; break;
                   7685:                        case 0x03: REG8(DL) = 0x02; break;
                   7686:                        }
                   7687:                }
                   7688:                REG8(DH) = drive_param->head_num();
                   7689:                int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
                   7690:                int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
                   7691:                REG8(CH) = cyl & 0xff;
                   7692:                REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
                   7693:        } else {
                   7694:                REG8(AH) = 0x07;
                   7695:                m_CF = 1;
                   7696:        }
                   7697: }
                   7698: 
                   7699: inline void pcbios_int_13h_10h()
                   7700: {
                   7701:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7702:        
                   7703:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7704:                REG8(AH) = 0x00; // successful completion
                   7705:        } else {
                   7706:                if(REG8(DL) & 0x80) {
                   7707:                        REG8(AH) = 0xaa; // drive not ready (hard disk)
                   7708:                } else {
                   7709:                        REG8(AH) = 0x80; // timeout (not ready)
                   7710:                }
                   7711:                m_CF = 1;
                   7712:        }
                   7713: }
                   7714: 
                   7715: inline void pcbios_int_13h_15h()
                   7716: {
                   7717:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7718:        
                   7719:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7720:                if(REG8(DL) & 0x80) {
                   7721:                        REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
                   7722:                } else {
                   7723:                        REG8(AH) = 0x03; // hard disk
                   7724:                }
                   7725:        } else {
                   7726:                REG8(AH) = 0x00; // no such drive
                   7727:        }
                   7728: }
                   7729: 
1.1.1.43  root     7730: inline void pcbios_int_13h_41h()
                   7731: {
                   7732:        if(REG16(BX) == 0x55aa) {
                   7733:                // IBM/MS INT 13 Extensions is not installed
                   7734:                REG8(AH) = 0x01;
                   7735:                m_CF = 1;
                   7736:        } else {
                   7737:                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));
                   7738:                REG8(AH) = 0x01;
                   7739:                m_CF = 1;
                   7740:        }
                   7741: }
                   7742: 
1.1.1.25  root     7743: inline void pcbios_int_14h_00h()
                   7744: {
1.1.1.29  root     7745:        if(REG16(DX) < 4) {
1.1.1.25  root     7746:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   7747:                UINT8 selector = sio_read(REG16(DX), 3);
                   7748:                selector &= ~0x3f;
                   7749:                selector |= REG8(AL) & 0x1f;
                   7750:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   7751:                sio_write(REG16(DX), 3, selector | 0x80);
                   7752:                sio_write(REG16(DX), 0, divisor & 0xff);
                   7753:                sio_write(REG16(DX), 1, divisor >> 8);
                   7754:                sio_write(REG16(DX), 3, selector);
                   7755:                REG8(AH) = sio_read(REG16(DX), 5);
                   7756:                REG8(AL) = sio_read(REG16(DX), 6);
                   7757:        } else {
                   7758:                REG8(AH) = 0x80;
                   7759:        }
                   7760: }
                   7761: 
                   7762: inline void pcbios_int_14h_01h()
                   7763: {
1.1.1.29  root     7764:        if(REG16(DX) < 4) {
1.1.1.25  root     7765:                UINT8 selector = sio_read(REG16(DX), 3);
                   7766:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7767:                sio_write(REG16(DX), 0, REG8(AL));
                   7768:                sio_write(REG16(DX), 3, selector);
                   7769:                REG8(AH) = sio_read(REG16(DX), 5);
                   7770:        } else {
                   7771:                REG8(AH) = 0x80;
                   7772:        }
                   7773: }
                   7774: 
                   7775: inline void pcbios_int_14h_02h()
                   7776: {
1.1.1.29  root     7777:        if(REG16(DX) < 4) {
1.1.1.25  root     7778:                UINT8 selector = sio_read(REG16(DX), 3);
                   7779:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7780:                REG8(AL) = sio_read(REG16(DX), 0);
                   7781:                sio_write(REG16(DX), 3, selector);
                   7782:                REG8(AH) = sio_read(REG16(DX), 5);
                   7783:        } else {
                   7784:                REG8(AH) = 0x80;
                   7785:        }
                   7786: }
                   7787: 
                   7788: inline void pcbios_int_14h_03h()
                   7789: {
1.1.1.29  root     7790:        if(REG16(DX) < 4) {
1.1.1.25  root     7791:                REG8(AH) = sio_read(REG16(DX), 5);
                   7792:                REG8(AL) = sio_read(REG16(DX), 6);
                   7793:        } else {
                   7794:                REG8(AH) = 0x80;
                   7795:        }
                   7796: }
                   7797: 
                   7798: inline void pcbios_int_14h_04h()
                   7799: {
1.1.1.29  root     7800:        if(REG16(DX) < 4) {
1.1.1.25  root     7801:                UINT8 selector = sio_read(REG16(DX), 3);
                   7802:                if(REG8(CH) <= 0x03) {
                   7803:                        selector = (selector & ~0x03) | REG8(CH);
                   7804:                }
                   7805:                if(REG8(BL) == 0x00) {
                   7806:                        selector &= ~0x04;
                   7807:                } else if(REG8(BL) == 0x01) {
                   7808:                        selector |= 0x04;
                   7809:                }
                   7810:                if(REG8(BH) == 0x00) {
                   7811:                        selector = (selector & ~0x38) | 0x00;
                   7812:                } else if(REG8(BH) == 0x01) {
                   7813:                        selector = (selector & ~0x38) | 0x08;
                   7814:                } else if(REG8(BH) == 0x02) {
                   7815:                        selector = (selector & ~0x38) | 0x18;
                   7816:                } else if(REG8(BH) == 0x03) {
                   7817:                        selector = (selector & ~0x38) | 0x28;
                   7818:                } else if(REG8(BH) == 0x04) {
                   7819:                        selector = (selector & ~0x38) | 0x38;
                   7820:                }
                   7821:                if(REG8(AL) == 0x00) {
                   7822:                        selector |= 0x40;
                   7823:                } else if(REG8(AL) == 0x01) {
                   7824:                        selector &= ~0x40;
                   7825:                }
                   7826:                if(REG8(CL) <= 0x0b) {
                   7827:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   7828:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   7829:                        sio_write(REG16(DX), 3, selector | 0x80);
                   7830:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   7831:                        sio_write(REG16(DX), 1, divisor >> 8);
                   7832:                }
                   7833:                sio_write(REG16(DX), 3, selector);
                   7834:                REG8(AH) = sio_read(REG16(DX), 5);
                   7835:                REG8(AL) = sio_read(REG16(DX), 6);
                   7836:        } else {
                   7837:                REG8(AH) = 0x80;
                   7838:        }
                   7839: }
                   7840: 
                   7841: inline void pcbios_int_14h_05h()
                   7842: {
1.1.1.29  root     7843:        if(REG16(DX) < 4) {
1.1.1.25  root     7844:                if(REG8(AL) == 0x00) {
                   7845:                        REG8(BL) = sio_read(REG16(DX), 4);
                   7846:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7847:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7848:                } else if(REG8(AL) == 0x01) {
                   7849:                        sio_write(REG16(DX), 4, REG8(BL));
                   7850:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7851:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7852:                } else {
                   7853:                        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));
                   7854:                }
                   7855:        } else {
                   7856:                REG8(AH) = 0x80;
                   7857:        }
                   7858: }
                   7859: 
1.1.1.14  root     7860: inline void pcbios_int_15h_10h()
                   7861: {
1.1.1.22  root     7862:        switch(REG8(AL)) {
                   7863:        case 0x00:
1.1.1.14  root     7864:                Sleep(10);
1.1.1.35  root     7865:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     7866:                break;
                   7867:        default:
                   7868:                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     7869:                REG8(AH) = 0x86;
                   7870:                m_CF = 1;
                   7871:        }
                   7872: }
                   7873: 
1.1       root     7874: inline void pcbios_int_15h_23h()
                   7875: {
                   7876:        switch(REG8(AL)) {
1.1.1.22  root     7877:        case 0x00:
1.1.1.8   root     7878:                REG8(CL) = cmos_read(0x2d);
                   7879:                REG8(CH) = cmos_read(0x2e);
1.1       root     7880:                break;
1.1.1.22  root     7881:        case 0x01:
1.1.1.8   root     7882:                cmos_write(0x2d, REG8(CL));
                   7883:                cmos_write(0x2e, REG8(CH));
1.1       root     7884:                break;
                   7885:        default:
1.1.1.22  root     7886:                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     7887:                REG8(AH) = 0x86;
1.1.1.3   root     7888:                m_CF = 1;
1.1       root     7889:                break;
                   7890:        }
                   7891: }
                   7892: 
                   7893: inline void pcbios_int_15h_24h()
                   7894: {
                   7895:        switch(REG8(AL)) {
1.1.1.22  root     7896:        case 0x00:
1.1.1.3   root     7897:                i386_set_a20_line(0);
1.1       root     7898:                REG8(AH) = 0;
                   7899:                break;
1.1.1.22  root     7900:        case 0x01:
1.1.1.3   root     7901:                i386_set_a20_line(1);
1.1       root     7902:                REG8(AH) = 0;
                   7903:                break;
1.1.1.22  root     7904:        case 0x02:
1.1       root     7905:                REG8(AH) = 0;
1.1.1.3   root     7906:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     7907:                REG16(CX) = 0;
                   7908:                break;
1.1.1.22  root     7909:        case 0x03:
1.1       root     7910:                REG16(AX) = 0;
                   7911:                REG16(BX) = 0;
                   7912:                break;
1.1.1.22  root     7913:        default:
                   7914:                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));
                   7915:                REG8(AH) = 0x86;
                   7916:                m_CF = 1;
                   7917:                break;
1.1       root     7918:        }
                   7919: }
                   7920: 
                   7921: inline void pcbios_int_15h_49h()
                   7922: {
1.1.1.27  root     7923:        REG8(AH) = 0x00;
                   7924:        REG8(BL) = 0x00; // DOS/V
1.1       root     7925: }
                   7926: 
1.1.1.22  root     7927: inline void pcbios_int_15h_50h()
                   7928: {
                   7929:        switch(REG8(AL)) {
                   7930:        case 0x00:
                   7931:        case 0x01:
                   7932:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   7933:                        REG8(AH) = 0x01; // invalid font type in bh
                   7934:                        m_CF = 1;
1.1.1.27  root     7935:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     7936:                        REG8(AH) = 0x02; // bl not zero
                   7937:                        m_CF = 1;
                   7938:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   7939:                        REG8(AH) = 0x04; // invalid code page
                   7940:                        m_CF = 1;
1.1.1.27  root     7941:                } else if(REG8(AL) == 0x01) {
                   7942:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     7943:                        m_CF = 1;
1.1.1.27  root     7944:                } else {
                   7945:                        // dummy font read routine is at fffd:000d
                   7946:                        SREG(ES) = 0xfffd;
                   7947:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     7948:                        REG16(BX) = 0x000d;
1.1.1.27  root     7949:                        REG8(AH) = 0x00; // success
1.1.1.22  root     7950:                }
                   7951:                break;
                   7952:        default:
                   7953:                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));
                   7954:                REG8(AH) = 0x86;
                   7955:                m_CF = 1;
                   7956:                break;
                   7957:        }
                   7958: }
                   7959: 
1.1.1.30  root     7960: inline void pcbios_int_15h_53h()
                   7961: {
                   7962:        switch(REG8(AL)) {
                   7963:        case 0x00:
                   7964:                // APM is not installed
                   7965:                REG8(AH) = 0x86;
                   7966:                m_CF = 1;
                   7967:                break;
                   7968:        default:
                   7969:                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));
                   7970:                REG8(AH) = 0x86;
                   7971:                m_CF = 1;
                   7972:                break;
                   7973:        }
                   7974: }
                   7975: 
1.1.1.43  root     7976: inline void pcbios_int_15h_84h()
                   7977: {
                   7978:        // joystick support (from DOSBox)
                   7979:        switch(REG16(DX)) {
                   7980:        case 0x00:
                   7981:                REG16(AX) = 0x00f0;
                   7982:                REG16(DX) = 0x0201;
                   7983:                m_CF = 1;
                   7984:                break;
                   7985:        case 0x01:
                   7986:                REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   7987:                m_CF = 1;
                   7988:                break;
                   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;
                   7994:        }
                   7995: }
1.1.1.35  root     7996: 
                   7997: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     7998: {
                   7999:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     8000:        UINT32 msec = usec / 1000;
                   8001:        
1.1.1.35  root     8002:        while(msec && !m_halted) {
1.1.1.14  root     8003:                UINT32 tmp = min(msec, 100);
                   8004:                if(msec - tmp < 10) {
                   8005:                        tmp = msec;
                   8006:                }
                   8007:                Sleep(tmp);
                   8008:                msec -= tmp;
                   8009:        }
1.1.1.35  root     8010:        
                   8011: #ifdef USE_SERVICE_THREAD
                   8012:        service_exit = true;
                   8013: #endif
                   8014:        return(0);
                   8015: }
                   8016: 
                   8017: inline void pcbios_int_15h_86h()
                   8018: {
                   8019:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   8020: #ifdef USE_SERVICE_THREAD
                   8021:                start_service_loop(pcbios_int_15h_86h_thread);
                   8022: #else
                   8023:                pcbios_int_15h_86h_thread(NULL);
                   8024:                REQUEST_HARDWRE_UPDATE();
                   8025: #endif
                   8026:        }
1.1       root     8027: }
                   8028: 
                   8029: inline void pcbios_int_15h_87h()
                   8030: {
                   8031:        // copy extended memory (from DOSBox)
                   8032:        int len = REG16(CX) * 2;
1.1.1.3   root     8033:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     8034:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   8035:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   8036:        memcpy(mem + dst, mem + src, len);
                   8037:        REG16(AX) = 0x00;
                   8038: }
                   8039: 
                   8040: inline void pcbios_int_15h_88h()
                   8041: {
1.1.1.17  root     8042:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     8043: }
                   8044: 
                   8045: inline void pcbios_int_15h_89h()
                   8046: {
1.1.1.21  root     8047: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     8048:        // switch to protected mode (from DOSBox)
                   8049:        write_io_byte(0x20, 0x10);
                   8050:        write_io_byte(0x21, REG8(BH));
                   8051:        write_io_byte(0x21, 0x00);
                   8052:        write_io_byte(0xa0, 0x10);
                   8053:        write_io_byte(0xa1, REG8(BL));
                   8054:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     8055:        i386_set_a20_line(1);
                   8056:        int ofs = SREG_BASE(ES) + REG16(SI);
                   8057:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   8058:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   8059:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   8060:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   8061: #if defined(HAS_I386)
                   8062:        m_cr[0] |= 1;
                   8063: #else
                   8064:        m_msw |= 1;
                   8065: #endif
                   8066:        SREG(DS) = 0x18;
                   8067:        SREG(ES) = 0x20;
                   8068:        SREG(SS) = 0x28;
                   8069:        i386_load_segment_descriptor(DS);
                   8070:        i386_load_segment_descriptor(ES);
                   8071:        i386_load_segment_descriptor(SS);
1.1.1.21  root     8072:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     8073:        REG16(SP) += 6;
1.1.1.3   root     8074: #if defined(HAS_I386)
1.1.1.21  root     8075:        UINT32 flags = get_flags();
                   8076:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8077:        set_flags(flags);
1.1.1.3   root     8078: #else
1.1.1.21  root     8079:        UINT32 flags = CompressFlags();
                   8080:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8081:        ExpandFlags(flags);
1.1.1.3   root     8082: #endif
1.1       root     8083:        REG16(AX) = 0x00;
1.1.1.21  root     8084:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     8085: #else
1.1.1.21  root     8086:        // i86/i186/v30: protected mode is not supported
1.1       root     8087:        REG8(AH) = 0x86;
1.1.1.3   root     8088:        m_CF = 1;
1.1       root     8089: #endif
                   8090: }
                   8091: 
1.1.1.21  root     8092: inline void pcbios_int_15h_8ah()
                   8093: {
                   8094:        UINT32 size = MAX_MEM - 0x100000;
                   8095:        REG16(AX) = size & 0xffff;
                   8096:        REG16(DX) = size >> 16;
                   8097: }
                   8098: 
1.1.1.3   root     8099: #if defined(HAS_I386)
1.1       root     8100: inline void pcbios_int_15h_c9h()
                   8101: {
                   8102:        REG8(AH) = 0x00;
                   8103:        REG8(CH) = cpu_type;
                   8104:        REG8(CL) = cpu_step;
                   8105: }
1.1.1.3   root     8106: #endif
1.1       root     8107: 
                   8108: inline void pcbios_int_15h_cah()
                   8109: {
                   8110:        switch(REG8(AL)) {
1.1.1.22  root     8111:        case 0x00:
1.1       root     8112:                if(REG8(BL) > 0x3f) {
                   8113:                        REG8(AH) = 0x03;
1.1.1.3   root     8114:                        m_CF = 1;
1.1       root     8115:                } else if(REG8(BL) < 0x0e) {
                   8116:                        REG8(AH) = 0x04;
1.1.1.3   root     8117:                        m_CF = 1;
1.1       root     8118:                } else {
1.1.1.8   root     8119:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     8120:                }
                   8121:                break;
1.1.1.22  root     8122:        case 0x01:
1.1       root     8123:                if(REG8(BL) > 0x3f) {
                   8124:                        REG8(AH) = 0x03;
1.1.1.3   root     8125:                        m_CF = 1;
1.1       root     8126:                } else if(REG8(BL) < 0x0e) {
                   8127:                        REG8(AH) = 0x04;
1.1.1.3   root     8128:                        m_CF = 1;
1.1       root     8129:                } else {
1.1.1.8   root     8130:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     8131:                }
                   8132:                break;
                   8133:        default:
1.1.1.22  root     8134:                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     8135:                REG8(AH) = 0x86;
1.1.1.3   root     8136:                m_CF = 1;
1.1       root     8137:                break;
                   8138:        }
                   8139: }
                   8140: 
1.1.1.22  root     8141: inline void pcbios_int_15h_e8h()
1.1.1.17  root     8142: {
1.1.1.22  root     8143:        switch(REG8(AL)) {
                   8144: #if defined(HAS_I386)
                   8145:        case 0x01:
                   8146:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   8147:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8148:                break;
1.1.1.17  root     8149: #endif
1.1.1.22  root     8150:        default:
                   8151:                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));
                   8152:                REG8(AH) = 0x86;
                   8153:                m_CF = 1;
                   8154:                break;
                   8155:        }
                   8156: }
1.1.1.17  root     8157: 
1.1.1.33  root     8158: void pcbios_update_key_code(bool wait)
1.1       root     8159: {
1.1.1.32  root     8160:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8161: #ifdef USE_SERVICE_THREAD
                   8162:                EnterCriticalSection(&key_buf_crit_sect);
                   8163: #endif
                   8164:                bool empty = key_buf_char->empty();
                   8165: #ifdef USE_SERVICE_THREAD
                   8166:                LeaveCriticalSection(&key_buf_crit_sect);
                   8167: #endif
                   8168:                if(empty) {
1.1.1.32  root     8169:                        if(!update_key_buffer()) {
1.1.1.33  root     8170:                                if(wait) {
1.1.1.32  root     8171:                                        Sleep(10);
                   8172:                                } else {
                   8173:                                        maybe_idle();
                   8174:                                }
1.1.1.14  root     8175:                        }
                   8176:                }
1.1.1.34  root     8177:        }
                   8178:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8179: #ifdef USE_SERVICE_THREAD
                   8180:                EnterCriticalSection(&key_buf_crit_sect);
                   8181: #endif
1.1.1.32  root     8182:                if(key_buf_char->count() != 0) {
1.1.1.41  root     8183:                        int key_char = key_buf_char->read();
                   8184:                        int key_scan = key_buf_scan->read();
                   8185:                        key_code  = key_char << 0;
                   8186:                        key_code |= key_scan << 8;
1.1.1.35  root     8187:                        key_recv  = 0x0000ffff;
1.1.1.41  root     8188:                        // write to bottom of key buffer
                   8189:                        mem[0x43c] = (UINT8)key_char;
                   8190:                        mem[0x43d] = (UINT8)key_scan;
1.1.1.32  root     8191:                }
                   8192:                if(key_buf_char->count() != 0) {
1.1.1.41  root     8193:                        int key_char = key_buf_char->read();
                   8194:                        int key_scan = key_buf_scan->read();
                   8195:                        key_code |= key_char << 16;
                   8196:                        key_code |= key_scan << 24;
1.1.1.33  root     8197:                        key_recv |= 0xffff0000;
1.1.1.41  root     8198:                        // write to bottom of key buffer
                   8199:                        mem[0x43c] = (UINT8)key_char;
                   8200:                        mem[0x43d] = (UINT8)key_scan;
1.1.1.32  root     8201:                }
1.1.1.35  root     8202: #ifdef USE_SERVICE_THREAD
                   8203:                LeaveCriticalSection(&key_buf_crit_sect);
                   8204: #endif
1.1       root     8205:        }
                   8206: }
                   8207: 
1.1.1.35  root     8208: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     8209: {
1.1.1.33  root     8210:        while(key_recv == 0 && !m_halted) {
                   8211:                pcbios_update_key_code(true);
1.1       root     8212:        }
1.1.1.33  root     8213:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8214:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   8215:                        if(REG8(AH) == 0x10) {
                   8216:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   8217:                        } else {
                   8218:                                key_code = ((key_code >> 16) & 0xff00);
                   8219:                        }
                   8220:                        key_recv >>= 16;
1.1       root     8221:                }
                   8222:        }
                   8223:        REG16(AX) = key_code & 0xffff;
                   8224:        key_code >>= 16;
1.1.1.33  root     8225:        key_recv >>= 16;
1.1.1.35  root     8226:        
                   8227: #ifdef USE_SERVICE_THREAD
                   8228:        service_exit = true;
                   8229: #endif
                   8230:        return(0);
                   8231: }
                   8232: 
                   8233: inline void pcbios_int_16h_00h()
                   8234: {
                   8235: #ifdef USE_SERVICE_THREAD
                   8236:        start_service_loop(pcbios_int_16h_00h_thread);
                   8237: #else
                   8238:        pcbios_int_16h_00h_thread(NULL);
                   8239:        REQUEST_HARDWRE_UPDATE();
                   8240: #endif
1.1       root     8241: }
                   8242: 
                   8243: inline void pcbios_int_16h_01h()
                   8244: {
1.1.1.33  root     8245:        if(key_recv == 0) {
                   8246:                pcbios_update_key_code(false);
1.1.1.5   root     8247:        }
1.1.1.33  root     8248:        if(key_recv != 0) {
                   8249:                UINT32 key_code_tmp = key_code;
                   8250:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8251:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   8252:                                if(REG8(AH) == 0x11) {
                   8253:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   8254:                                } else {
                   8255:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   8256:                                }
                   8257:                        }
1.1       root     8258:                }
1.1.1.5   root     8259:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     8260: #if defined(HAS_I386)
1.1.1.33  root     8261:                m_ZF = 0;
                   8262: #else
                   8263:                m_ZeroVal = 1;
                   8264: #endif
                   8265:        } else {
                   8266: #if defined(HAS_I386)
                   8267:                m_ZF = 1;
1.1.1.3   root     8268: #else
1.1.1.33  root     8269:                m_ZeroVal = 0;
1.1.1.3   root     8270: #endif
1.1.1.33  root     8271:        }
1.1       root     8272: }
                   8273: 
                   8274: inline void pcbios_int_16h_02h()
                   8275: {
                   8276:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   8277:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   8278:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   8279:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   8280:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   8281:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   8282:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   8283:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   8284: }
                   8285: 
                   8286: inline void pcbios_int_16h_03h()
                   8287: {
                   8288:        static UINT16 status = 0;
                   8289:        
                   8290:        switch(REG8(AL)) {
                   8291:        case 0x05:
                   8292:                status = REG16(BX);
                   8293:                break;
                   8294:        case 0x06:
                   8295:                REG16(BX) = status;
                   8296:                break;
                   8297:        default:
1.1.1.3   root     8298:                m_CF = 1;
1.1       root     8299:                break;
                   8300:        }
                   8301: }
                   8302: 
                   8303: inline void pcbios_int_16h_05h()
                   8304: {
1.1.1.32  root     8305:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8306: #ifdef USE_SERVICE_THREAD
                   8307:                EnterCriticalSection(&key_buf_crit_sect);
                   8308: #endif
1.1.1.32  root     8309:                key_buf_char->write(REG8(CL));
                   8310:                key_buf_scan->write(REG8(CH));
1.1.1.35  root     8311: #ifdef USE_SERVICE_THREAD
                   8312:                LeaveCriticalSection(&key_buf_crit_sect);
                   8313: #endif
1.1.1.32  root     8314:        }
1.1       root     8315:        REG8(AL) = 0x00;
                   8316: }
                   8317: 
                   8318: inline void pcbios_int_16h_12h()
                   8319: {
                   8320:        pcbios_int_16h_02h();
                   8321:        
                   8322:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   8323:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   8324:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   8325:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   8326:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   8327:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   8328:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   8329:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   8330: }
                   8331: 
                   8332: inline void pcbios_int_16h_13h()
                   8333: {
                   8334:        static UINT16 status = 0;
                   8335:        
                   8336:        switch(REG8(AL)) {
                   8337:        case 0x00:
                   8338:                status = REG16(DX);
                   8339:                break;
                   8340:        case 0x01:
                   8341:                REG16(DX) = status;
                   8342:                break;
                   8343:        default:
1.1.1.22  root     8344:                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     8345:                m_CF = 1;
1.1       root     8346:                break;
                   8347:        }
                   8348: }
                   8349: 
                   8350: inline void pcbios_int_16h_14h()
                   8351: {
                   8352:        static UINT8 status = 0;
                   8353:        
                   8354:        switch(REG8(AL)) {
                   8355:        case 0x00:
                   8356:        case 0x01:
                   8357:                status = REG8(AL);
                   8358:                break;
                   8359:        case 0x02:
                   8360:                REG8(AL) = status;
                   8361:                break;
                   8362:        default:
1.1.1.22  root     8363:                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     8364:                m_CF = 1;
1.1       root     8365:                break;
                   8366:        }
                   8367: }
                   8368: 
1.1.1.24  root     8369: inline void pcbios_int_16h_55h()
                   8370: {
                   8371:        switch(REG8(AL)) {
                   8372:        case 0x00:
                   8373:                // keyboard tsr is not present
                   8374:                break;
                   8375:        case 0xfe:
                   8376:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   8377:                break;
                   8378:        case 0xff:
                   8379:                break;
                   8380:        default:
                   8381:                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));
                   8382:                m_CF = 1;
                   8383:                break;
                   8384:        }
                   8385: }
                   8386: 
1.1.1.30  root     8387: inline void pcbios_int_16h_6fh()
                   8388: {
                   8389:        switch(REG8(AL)) {
                   8390:        case 0x00:
                   8391:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   8392:                break;
                   8393:        default:
                   8394:                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));
                   8395:                m_CF = 1;
                   8396:                break;
                   8397:        }
                   8398: }
                   8399: 
1.1.1.37  root     8400: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   8401: {
                   8402:        UINT8 hi = jis >> 8;
                   8403:        UINT8 lo = jis & 0xff;
                   8404:        
                   8405:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   8406:        hi = (hi - 0x21) / 2 + 0x81;
                   8407:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   8408:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   8409:        
                   8410:        return((hi << 8) + lo);
                   8411: }
                   8412: 
                   8413: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   8414: {
                   8415:        UINT8 hi = sjis >> 8;
                   8416:        UINT8 lo = sjis & 0xff;
                   8417:        
                   8418:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   8419:                return(0x2121);
                   8420:        }
                   8421:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   8422:                return(0x2121);
                   8423:        }
                   8424:        if(hi >= 0xf0 && hi <= 0xf3) {
                   8425:                // gaiji
                   8426:                if(lo >= 0x40 && lo <= 0x7e) {
                   8427:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   8428:                }
                   8429:                if(lo >= 0x80 && lo <= 0x9e) {
                   8430:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   8431:                }
                   8432:                if(lo >= 0x9f && lo <= 0xfc) {
                   8433:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   8434:                }
                   8435:        }
                   8436:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   8437:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   8438:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   8439:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   8440:        
                   8441:        return((hi << 8) + lo);
                   8442: }
                   8443: 
1.1.1.38  root     8444: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   8445: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   8446: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     8447: 
                   8448: void pcbios_printer_out(int c, UINT8 data)
                   8449: {
                   8450:        if(pio[c].conv_mode) {
                   8451:                if(pio[c].sjis_hi != 0) {
                   8452:                        if(!pio[c].jis_mode) {
                   8453:                                printer_out(c, 0x1c);
                   8454:                                printer_out(c, 0x26);
                   8455:                                pio[c].jis_mode = true;
                   8456:                        }
                   8457:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   8458:                        printer_out(c, jis >> 8);
                   8459:                        printer_out(c, jis & 0xff);
                   8460:                        pio[c].sjis_hi = 0;
                   8461:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   8462:                        printer_out(c, data);
                   8463:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8464:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8465:                        }
                   8466:                        pio[c].esc_len++;
                   8467:                        
                   8468:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8469:                        case 0x33: // 1Bh 33h XX
                   8470:                        case 0x4a: // 1Bh 4Ah XX
                   8471:                        case 0x4e: // 1Bh 4Eh XX
                   8472:                        case 0x51: // 1Bh 51h XX
                   8473:                        case 0x55: // 1Bh 55h XX
                   8474:                        case 0x6c: // 1Bh 6Ch XX
                   8475:                        case 0x71: // 1Bh 71h XX
                   8476:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     8477:                                if(pio[c].esc_len == 3) {
                   8478:                                        pio[c].esc_buf[0] = 0x00;
                   8479:                                }
                   8480:                                break;
1.1.1.38  root     8481:                        case 0x24: // 1Bh 24h XX XX
                   8482:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     8483:                                if(pio[c].esc_len == 4) {
                   8484:                                        pio[c].esc_buf[0] = 0x00;
                   8485:                                }
                   8486:                                break;
1.1.1.38  root     8487:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     8488:                                if(pio[c].esc_len >= 3) {
                   8489:                                        switch(pio[c].esc_buf[2]) {
                   8490:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   8491:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   8492:                                                        pio[c].esc_buf[0] = 0x00;
                   8493:                                                }
                   8494:                                                break;
                   8495:                                        case 32: case 33: case 38: case 39: case 40:
                   8496:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   8497:                                                        pio[c].esc_buf[0] = 0x00;
                   8498:                                                }
                   8499:                                                break;
1.1.1.38  root     8500:                                        case 71: case 72: case 73:
                   8501:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   8502:                                                        pio[c].esc_buf[0] = 0x00;
                   8503:                                                }
                   8504:                                                break;
1.1.1.37  root     8505:                                        default:
                   8506:                                                pio[c].esc_buf[0] = 0x00;
                   8507:                                                break;
                   8508:                                        }
                   8509:                                }
                   8510:                                break;
1.1.1.38  root     8511:                        case 0x40: // 1Bh 40h
1.1.1.37  root     8512:                                if(pio[c].jis_mode) {
                   8513:                                        printer_out(c, 0x1c);
                   8514:                                        printer_out(c, 0x2e);
                   8515:                                        pio[c].jis_mode = false;
                   8516:                                }
                   8517:                                pio[c].esc_buf[0] = 0x00;
                   8518:                                break;
1.1.1.38  root     8519:                        case 0x42: // 1Bh 42h data 00h
                   8520:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8521:                                if(pio[c].esc_len >= 3 && data == 0) {
                   8522:                                        pio[c].esc_buf[0] = 0x00;
                   8523:                                }
                   8524:                                break;
1.1.1.38  root     8525:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     8526:                                if(pio[c].esc_len >= 3 && data != 0) {
                   8527:                                        pio[c].esc_buf[0] = 0x00;
                   8528:                                }
                   8529:                                break;
1.1.1.38  root     8530:                        default: // 1Bh XX
1.1.1.37  root     8531:                                pio[c].esc_buf[0] = 0x00;
                   8532:                                break;
                   8533:                        }
                   8534:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   8535:                        printer_out(c, data);
                   8536:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8537:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8538:                        }
                   8539:                        pio[c].esc_len++;
                   8540:                        
                   8541:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8542:                        case 0x21: // 1Ch 21h XX
                   8543:                        case 0x2d: // 1Ch 2Dh XX
                   8544:                        case 0x57: // 1Ch 57h XX
                   8545:                        case 0x6b: // 1Ch 6Bh XX
                   8546:                        case 0x72: // 1Ch 72h XX
                   8547:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     8548:                                if(pio[c].esc_len == 3) {
                   8549:                                        pio[c].esc_buf[0] = 0x00;
                   8550:                                }
                   8551:                                break;
1.1.1.38  root     8552:                        case 0x26: // 1Ch 26h
1.1.1.37  root     8553:                                pio[c].jis_mode = true;
                   8554:                                pio[c].esc_buf[0] = 0x00;
                   8555:                                break;
1.1.1.38  root     8556:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     8557:                                pio[c].jis_mode = false;
                   8558:                                pio[c].esc_buf[0] = 0x00;
                   8559:                                break;
1.1.1.38  root     8560:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     8561:                                if(pio[c].esc_len == 76) {
                   8562:                                        pio[c].esc_buf[0] = 0x00;
                   8563:                                }
                   8564:                                break;
1.1.1.38  root     8565:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8566:                                if(pio[c].esc_len == 6) {
                   8567:                                        pio[c].esc_buf[0] = 0x00;
                   8568:                                }
                   8569:                                break;
1.1.1.38  root     8570:                        case 0x53: // 1Ch 53h XX XX
                   8571:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     8572:                                if(pio[c].esc_len == 4) {
                   8573:                                        pio[c].esc_buf[0] = 0x00;
                   8574:                                }
                   8575:                                break;
1.1.1.38  root     8576:                        default: // 1Ch XX
1.1.1.37  root     8577:                                pio[c].esc_buf[0] = 0x00;
                   8578:                                break;
                   8579:                        }
                   8580:                } else if(data == 0x1b || data == 0x1c) {
                   8581:                        printer_out(c, data);
                   8582:                        pio[c].esc_buf[0] = data;
                   8583:                        pio[c].esc_len = 1;
                   8584:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   8585:                        pio[c].sjis_hi = data;
                   8586:                } else {
                   8587:                        if(pio[c].jis_mode) {
                   8588:                                printer_out(c, 0x1c);
                   8589:                                printer_out(c, 0x2e);
                   8590:                                pio[c].jis_mode = false;
                   8591:                        }
                   8592:                        printer_out(c, data);
                   8593:                }
                   8594:        } else {
                   8595:                if(pio[c].jis_mode) {
                   8596:                        printer_out(c, 0x1c);
                   8597:                        printer_out(c, 0x2e);
                   8598:                        pio[c].jis_mode = false;
                   8599:                }
                   8600:                printer_out(c, data);
                   8601:        }
                   8602: }
                   8603: 
                   8604: inline void pcbios_int_17h_00h()
                   8605: {
                   8606:        if(REG16(DX) < 3) {
                   8607:                pcbios_printer_out(REG16(DX), REG8(AL));
                   8608:                REG8(AH) = 0xd0;
                   8609:        }
                   8610: }
                   8611: 
                   8612: inline void pcbios_int_17h_01h()
                   8613: {
                   8614:        if(REG16(DX) < 3) {
                   8615:                REG8(AH) = 0xd0;
                   8616:        }
                   8617: }
                   8618: 
                   8619: inline void pcbios_int_17h_02h()
                   8620: {
                   8621:        if(REG16(DX) < 3) {
                   8622:                REG8(AH) = 0xd0;
                   8623:        }
                   8624: }
                   8625: 
                   8626: inline void pcbios_int_17h_03h()
                   8627: {
                   8628:        switch(REG8(AL)) {
                   8629:        case 0x00:
                   8630:                if(REG16(DX) < 3) {
                   8631:                        if(pio[REG16(DX)].jis_mode) {
                   8632:                                printer_out(REG16(DX), 0x1c);
                   8633:                                printer_out(REG16(DX), 0x2e);
                   8634:                                pio[REG16(DX)].jis_mode = false;
                   8635:                        }
                   8636:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   8637:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   8638:                        }
                   8639:                        REG16(CX) = 0x0000;
                   8640:                        REG8(AH) = 0xd0;
                   8641:                }
                   8642:                break;
                   8643:        default:
                   8644:                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));
                   8645:                break;
                   8646:        }
                   8647: }
                   8648: 
                   8649: inline void pcbios_int_17h_50h()
                   8650: {
                   8651:        switch(REG8(AL)) {
                   8652:        case 0x00:
                   8653:                if(REG16(DX) < 3) {
                   8654:                        if(REG16(BX) = 0x0001) {
                   8655:                                pio[REG16(DX)].conv_mode = false;
                   8656:                                REG8(AL) = 0x00;
                   8657:                        } else if(REG16(BX) = 0x0051) {
                   8658:                                pio[REG16(DX)].conv_mode = true;
                   8659:                                REG8(AL) = 0x00;
                   8660:                        } else {
                   8661:                                REG8(AL) = 0x01;
                   8662:                        }
                   8663:                } else {
                   8664:                        REG8(AL) = 0x02;
                   8665:                }
                   8666:                break;
                   8667:        case 0x01:
                   8668:                if(REG16(DX) < 3) {
                   8669:                        if(pio[REG16(DX)].conv_mode) {
                   8670:                                REG16(BX) = 0x0051;
                   8671:                        } else {
                   8672:                                REG16(BX) = 0x0001;
                   8673:                        }
                   8674:                        REG8(AL) = 0x00;
                   8675:                } else {
                   8676:                        REG8(AL) = 0x02;
                   8677:                }
                   8678:                break;
                   8679:        default:
                   8680:                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));
                   8681:                break;
                   8682:        }
                   8683: }
                   8684: 
                   8685: inline void pcbios_int_17h_51h()
                   8686: {
                   8687:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   8688:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   8689:        } else {
                   8690:                REG16(DX) = 0x0000;
                   8691:        }
                   8692: }
                   8693: 
                   8694: inline void pcbios_int_17h_52h()
                   8695: {
                   8696:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   8697:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   8698:        } else {
                   8699:                REG16(DX) = 0x0000;
                   8700:        }
                   8701: }
                   8702: 
                   8703: inline void pcbios_int_17h_84h()
                   8704: {
                   8705:        if(REG16(DX) < 3) {
                   8706:                if(pio[REG16(DX)].jis_mode) {
                   8707:                        printer_out(REG16(DX), 0x1c);
                   8708:                        printer_out(REG16(DX), 0x2e);
                   8709:                        pio[REG16(DX)].jis_mode = false;
                   8710:                }
                   8711:                printer_out(REG16(DX), REG8(AL));
                   8712:                REG8(AH) = 0xd0;
                   8713:        }
                   8714: }
                   8715: 
                   8716: inline void pcbios_int_17h_85h()
                   8717: {
                   8718:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   8719: }
                   8720: 
1.1       root     8721: inline void pcbios_int_1ah_00h()
                   8722: {
1.1.1.19  root     8723:        pcbios_update_daily_timer_counter(timeGetTime());
                   8724:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   8725:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   8726:        REG8(AL) = mem[0x470];
                   8727:        mem[0x470] = 0;
1.1       root     8728: }
                   8729: 
                   8730: inline int to_bcd(int t)
                   8731: {
                   8732:        int u = (t % 100) / 10;
                   8733:        return (u << 4) | (t % 10);
                   8734: }
                   8735: 
                   8736: inline void pcbios_int_1ah_02h()
                   8737: {
                   8738:        SYSTEMTIME time;
                   8739:        
                   8740:        GetLocalTime(&time);
                   8741:        REG8(CH) = to_bcd(time.wHour);
                   8742:        REG8(CL) = to_bcd(time.wMinute);
                   8743:        REG8(DH) = to_bcd(time.wSecond);
                   8744:        REG8(DL) = 0x00;
                   8745: }
                   8746: 
                   8747: inline void pcbios_int_1ah_04h()
                   8748: {
                   8749:        SYSTEMTIME time;
                   8750:        
                   8751:        GetLocalTime(&time);
                   8752:        REG8(CH) = to_bcd(time.wYear / 100);
                   8753:        REG8(CL) = to_bcd(time.wYear);
                   8754:        REG8(DH) = to_bcd(time.wMonth);
                   8755:        REG8(DL) = to_bcd(time.wDay);
                   8756: }
                   8757: 
                   8758: inline void pcbios_int_1ah_0ah()
                   8759: {
                   8760:        SYSTEMTIME time;
                   8761:        FILETIME file_time;
                   8762:        WORD dos_date, dos_time;
                   8763:        
                   8764:        GetLocalTime(&time);
                   8765:        SystemTimeToFileTime(&time, &file_time);
                   8766:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   8767:        REG16(CX) = dos_date;
                   8768: }
                   8769: 
                   8770: // msdos system call
                   8771: 
1.1.1.43  root     8772: inline void msdos_int_21h_56h(int lfn);
                   8773: 
1.1       root     8774: inline void msdos_int_21h_00h()
                   8775: {
1.1.1.3   root     8776:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     8777: }
                   8778: 
1.1.1.35  root     8779: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     8780: {
                   8781:        REG8(AL) = msdos_getche();
1.1.1.33  root     8782:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8783:        
1.1.1.35  root     8784: #ifdef USE_SERVICE_THREAD
                   8785:        service_exit = true;
                   8786: #endif
                   8787:        return(0);
                   8788: }
                   8789: 
                   8790: inline void msdos_int_21h_01h()
                   8791: {
                   8792: #ifdef USE_SERVICE_THREAD
                   8793:        start_service_loop(msdos_int_21h_01h_thread);
                   8794: #else
                   8795:        msdos_int_21h_01h_thread(NULL);
                   8796:        REQUEST_HARDWRE_UPDATE();
                   8797: #endif
1.1       root     8798: }
                   8799: 
                   8800: inline void msdos_int_21h_02h()
                   8801: {
1.1.1.33  root     8802:        UINT8 data = REG8(DL);
                   8803:        msdos_putch(data);
                   8804:        REG8(AL) = data;
                   8805:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8806: }
                   8807: 
                   8808: inline void msdos_int_21h_03h()
                   8809: {
                   8810:        REG8(AL) = msdos_aux_in();
                   8811: }
                   8812: 
                   8813: inline void msdos_int_21h_04h()
                   8814: {
                   8815:        msdos_aux_out(REG8(DL));
                   8816: }
                   8817: 
                   8818: inline void msdos_int_21h_05h()
                   8819: {
                   8820:        msdos_prn_out(REG8(DL));
                   8821: }
                   8822: 
                   8823: inline void msdos_int_21h_06h()
                   8824: {
                   8825:        if(REG8(DL) == 0xff) {
                   8826:                if(msdos_kbhit()) {
                   8827:                        REG8(AL) = msdos_getch();
1.1.1.3   root     8828: #if defined(HAS_I386)
                   8829:                        m_ZF = 0;
                   8830: #else
                   8831:                        m_ZeroVal = 1;
                   8832: #endif
1.1       root     8833:                } else {
                   8834:                        REG8(AL) = 0;
1.1.1.3   root     8835: #if defined(HAS_I386)
                   8836:                        m_ZF = 1;
                   8837: #else
                   8838:                        m_ZeroVal = 0;
                   8839: #endif
1.1.1.14  root     8840:                        maybe_idle();
1.1       root     8841:                }
                   8842:        } else {
1.1.1.33  root     8843:                UINT8 data = REG8(DL);
                   8844:                msdos_putch(data);
                   8845:                REG8(AL) = data;
1.1       root     8846:        }
                   8847: }
                   8848: 
1.1.1.35  root     8849: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     8850: {
                   8851:        REG8(AL) = msdos_getch();
1.1.1.26  root     8852:        
1.1.1.35  root     8853: #ifdef USE_SERVICE_THREAD
                   8854:        service_exit = true;
                   8855: #endif
                   8856:        return(0);
1.1       root     8857: }
                   8858: 
1.1.1.35  root     8859: inline void msdos_int_21h_07h()
                   8860: {
                   8861: #ifdef USE_SERVICE_THREAD
                   8862:        start_service_loop(msdos_int_21h_07h_thread);
                   8863: #else
                   8864:        msdos_int_21h_07h_thread(NULL);
                   8865:        REQUEST_HARDWRE_UPDATE();
                   8866: #endif
                   8867: }
                   8868: 
                   8869: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     8870: {
                   8871:        REG8(AL) = msdos_getch();
1.1.1.33  root     8872:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8873:        
1.1.1.35  root     8874: #ifdef USE_SERVICE_THREAD
                   8875:        service_exit = true;
                   8876: #endif
                   8877:        return(0);
                   8878: }
                   8879: 
                   8880: inline void msdos_int_21h_08h()
                   8881: {
                   8882: #ifdef USE_SERVICE_THREAD
                   8883:        start_service_loop(msdos_int_21h_08h_thread);
                   8884: #else
                   8885:        msdos_int_21h_08h_thread(NULL);
                   8886:        REQUEST_HARDWRE_UPDATE();
                   8887: #endif
1.1       root     8888: }
                   8889: 
                   8890: inline void msdos_int_21h_09h()
                   8891: {
1.1.1.21  root     8892:        msdos_stdio_reopen();
                   8893:        
1.1.1.20  root     8894:        process_t *process = msdos_process_info_get(current_psp);
                   8895:        int fd = msdos_psp_get_file_table(1, current_psp);
                   8896:        
1.1.1.14  root     8897:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   8898:        int len = 0;
1.1       root     8899:        
1.1.1.14  root     8900:        while(str[len] != '$' && len < 0x10000) {
                   8901:                len++;
                   8902:        }
1.1.1.20  root     8903:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     8904:                // stdout is redirected to file
1.1.1.20  root     8905:                msdos_write(fd, str, len);
1.1       root     8906:        } else {
                   8907:                for(int i = 0; i < len; i++) {
1.1.1.14  root     8908:                        msdos_putch(str[i]);
1.1       root     8909:                }
                   8910:        }
1.1.1.33  root     8911:        REG8(AL) = '$';
                   8912:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8913: }
                   8914: 
1.1.1.35  root     8915: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     8916: {
1.1.1.3   root     8917:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     8918:        int max = mem[ofs] - 1;
                   8919:        UINT8 *buf = mem + ofs + 2;
                   8920:        int chr, p = 0;
                   8921:        
                   8922:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     8923:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     8924:                        p = 0;
1.1.1.33  root     8925:                        msdos_putch(0x03);
                   8926:                        msdos_putch(0x0d);
                   8927:                        msdos_putch(0x0a);
1.1.1.26  root     8928:                        break;
1.1.1.33  root     8929:                } else if(ctrl_break_pressed) {
                   8930:                        // skip this byte
1.1.1.26  root     8931:                } else if(chr == 0x00) {
1.1       root     8932:                        // skip 2nd byte
                   8933:                        msdos_getch();
                   8934:                } else if(chr == 0x08) {
                   8935:                        // back space
                   8936:                        if(p > 0) {
                   8937:                                p--;
1.1.1.20  root     8938:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     8939:                                        msdos_putch(0x08);
                   8940:                                        msdos_putch(0x08);
                   8941:                                        msdos_putch(0x20);
                   8942:                                        msdos_putch(0x20);
                   8943:                                        msdos_putch(0x08);
                   8944:                                        msdos_putch(0x08);
1.1.1.36  root     8945:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   8946:                                        p--;
                   8947:                                        msdos_putch(0x08);
                   8948:                                        msdos_putch(0x08);
                   8949:                                        msdos_putch(0x20);
                   8950:                                        msdos_putch(0x20);
                   8951:                                        msdos_putch(0x08);
                   8952:                                        msdos_putch(0x08);
1.1.1.34  root     8953:                                } else {
                   8954:                                        msdos_putch(0x08);
                   8955:                                        msdos_putch(0x20);
                   8956:                                        msdos_putch(0x08);
                   8957:                                }
                   8958:                        }
                   8959:                } else if(chr == 0x1b) {
                   8960:                        // escape
                   8961:                        while(p > 0) {
                   8962:                                p--;
                   8963:                                if(msdos_ctrl_code_check(buf[p])) {
                   8964:                                        msdos_putch(0x08);
                   8965:                                        msdos_putch(0x08);
                   8966:                                        msdos_putch(0x20);
                   8967:                                        msdos_putch(0x20);
                   8968:                                        msdos_putch(0x08);
                   8969:                                        msdos_putch(0x08);
1.1.1.20  root     8970:                                } else {
1.1.1.34  root     8971:                                        msdos_putch(0x08);
                   8972:                                        msdos_putch(0x20);
                   8973:                                        msdos_putch(0x08);
1.1.1.20  root     8974:                                }
1.1       root     8975:                        }
                   8976:                } else if(p < max) {
                   8977:                        buf[p++] = chr;
                   8978:                        msdos_putch(chr);
                   8979:                }
                   8980:        }
                   8981:        buf[p] = 0x0d;
                   8982:        mem[ofs + 1] = p;
1.1.1.33  root     8983:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8984:        
1.1.1.35  root     8985: #ifdef USE_SERVICE_THREAD
                   8986:        service_exit = true;
                   8987: #endif
                   8988:        return(0);
                   8989: }
                   8990: 
                   8991: inline void msdos_int_21h_0ah()
                   8992: {
                   8993:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   8994: #ifdef USE_SERVICE_THREAD
                   8995:                start_service_loop(msdos_int_21h_0ah_thread);
                   8996: #else
                   8997:                msdos_int_21h_0ah_thread(NULL);
                   8998:                REQUEST_HARDWRE_UPDATE();
                   8999: #endif
                   9000:        }
1.1       root     9001: }
                   9002: 
                   9003: inline void msdos_int_21h_0bh()
                   9004: {
                   9005:        if(msdos_kbhit()) {
                   9006:                REG8(AL) = 0xff;
                   9007:        } else {
                   9008:                REG8(AL) = 0x00;
1.1.1.14  root     9009:                maybe_idle();
1.1       root     9010:        }
1.1.1.33  root     9011:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9012: }
                   9013: 
                   9014: inline void msdos_int_21h_0ch()
                   9015: {
                   9016:        // clear key buffer
1.1.1.21  root     9017:        msdos_stdio_reopen();
                   9018:        
1.1.1.20  root     9019:        process_t *process = msdos_process_info_get(current_psp);
                   9020:        int fd = msdos_psp_get_file_table(0, current_psp);
                   9021:        
                   9022:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9023:                // stdin is redirected to file
                   9024:        } else {
                   9025:                while(msdos_kbhit()) {
                   9026:                        msdos_getch();
                   9027:                }
                   9028:        }
                   9029:        
                   9030:        switch(REG8(AL)) {
                   9031:        case 0x01:
                   9032:                msdos_int_21h_01h();
                   9033:                break;
                   9034:        case 0x06:
                   9035:                msdos_int_21h_06h();
                   9036:                break;
                   9037:        case 0x07:
                   9038:                msdos_int_21h_07h();
                   9039:                break;
                   9040:        case 0x08:
                   9041:                msdos_int_21h_08h();
                   9042:                break;
                   9043:        case 0x0a:
                   9044:                msdos_int_21h_0ah();
                   9045:                break;
                   9046:        default:
1.1.1.22  root     9047: //             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));
                   9048: //             REG16(AX) = 0x01;
                   9049: //             m_CF = 1;
1.1       root     9050:                break;
                   9051:        }
                   9052: }
                   9053: 
                   9054: inline void msdos_int_21h_0dh()
                   9055: {
                   9056: }
                   9057: 
                   9058: inline void msdos_int_21h_0eh()
                   9059: {
                   9060:        if(REG8(DL) < 26) {
                   9061:                _chdrive(REG8(DL) + 1);
                   9062:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9063:                msdos_sda_update(current_psp);
1.1       root     9064:        }
                   9065:        REG8(AL) = 26; // zdrive
                   9066: }
                   9067: 
1.1.1.14  root     9068: inline void msdos_int_21h_0fh()
                   9069: {
                   9070:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9071:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45! root     9072:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9073:        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     9074:        
1.1.1.14  root     9075:        if(hFile == INVALID_HANDLE_VALUE) {
                   9076:                REG8(AL) = 0xff;
                   9077:        } else {
                   9078:                REG8(AL) = 0;
                   9079:                fcb->current_block = 0;
                   9080:                fcb->record_size = 128;
                   9081:                fcb->file_size = GetFileSize(hFile, NULL);
                   9082:                fcb->handle = hFile;
                   9083:                fcb->cur_record = 0;
                   9084:        }
                   9085: }
                   9086: 
                   9087: inline void msdos_int_21h_10h()
                   9088: {
                   9089:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9090:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9091:        
                   9092:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9093: }
                   9094: 
1.1       root     9095: inline void msdos_int_21h_11h()
                   9096: {
1.1.1.3   root     9097:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9098:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9099:        
                   9100:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9101:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9102:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9103:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45! root     9104:        const char *path = msdos_fcb_path(fcb);
1.1       root     9105:        WIN32_FIND_DATA fd;
                   9106:        
1.1.1.13  root     9107:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9108:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9109:                FindClose(dtainfo->find_handle);
                   9110:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9111:        }
                   9112:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9113:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9114:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9115:        
1.1.1.14  root     9116:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9117:                dtainfo->allowable_mask &= ~8;
1.1       root     9118:        }
1.1.1.14  root     9119:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   9120:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9121:                      !msdos_find_file_has_8dot3name(&fd)) {
                   9122:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9123:                                FindClose(dtainfo->find_handle);
                   9124:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9125:                                break;
                   9126:                        }
                   9127:                }
                   9128:        }
1.1.1.13  root     9129:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9130:                if(ext_fcb->flag == 0xff) {
                   9131:                        ext_find->flag = 0xff;
                   9132:                        memset(ext_find->reserved, 0, 5);
                   9133:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9134:                }
                   9135:                find->drive = _getdrive();
1.1.1.13  root     9136:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9137:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9138:                find->nt_res = 0;
                   9139:                msdos_find_file_conv_local_time(&fd);
                   9140:                find->create_time_ms = 0;
                   9141:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9142:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9143:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9144:                find->cluster_hi = find->cluster_lo = 0;
                   9145:                find->file_size = fd.nFileSizeLow;
                   9146:                REG8(AL) = 0x00;
1.1.1.14  root     9147:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9148:                if(ext_fcb->flag == 0xff) {
                   9149:                        ext_find->flag = 0xff;
                   9150:                        memset(ext_find->reserved, 0, 5);
                   9151:                        ext_find->attribute = 8;
                   9152:                }
                   9153:                find->drive = _getdrive();
                   9154:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9155:                find->attribute = 8;
                   9156:                find->nt_res = 0;
                   9157:                msdos_find_file_conv_local_time(&fd);
                   9158:                find->create_time_ms = 0;
                   9159:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9160:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9161:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9162:                find->cluster_hi = find->cluster_lo = 0;
                   9163:                find->file_size = 0;
1.1.1.14  root     9164:                dtainfo->allowable_mask &= ~8;
1.1       root     9165:                REG8(AL) = 0x00;
                   9166:        } else {
                   9167:                REG8(AL) = 0xff;
                   9168:        }
                   9169: }
                   9170: 
                   9171: inline void msdos_int_21h_12h()
                   9172: {
1.1.1.3   root     9173:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     9174: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9175:        
                   9176:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9177:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9178:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9179:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9180:        WIN32_FIND_DATA fd;
                   9181:        
1.1.1.13  root     9182:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9183:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9184:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     9185:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9186:                              !msdos_find_file_has_8dot3name(&fd)) {
                   9187:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9188:                                        FindClose(dtainfo->find_handle);
                   9189:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9190:                                        break;
                   9191:                                }
                   9192:                        }
                   9193:                } else {
1.1.1.13  root     9194:                        FindClose(dtainfo->find_handle);
                   9195:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9196:                }
                   9197:        }
1.1.1.13  root     9198:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9199:                if(ext_fcb->flag == 0xff) {
                   9200:                        ext_find->flag = 0xff;
                   9201:                        memset(ext_find->reserved, 0, 5);
                   9202:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9203:                }
                   9204:                find->drive = _getdrive();
1.1.1.13  root     9205:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9206:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9207:                find->nt_res = 0;
                   9208:                msdos_find_file_conv_local_time(&fd);
                   9209:                find->create_time_ms = 0;
                   9210:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9211:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9212:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9213:                find->cluster_hi = find->cluster_lo = 0;
                   9214:                find->file_size = fd.nFileSizeLow;
                   9215:                REG8(AL) = 0x00;
1.1.1.14  root     9216:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9217:                if(ext_fcb->flag == 0xff) {
                   9218:                        ext_find->flag = 0xff;
                   9219:                        memset(ext_find->reserved, 0, 5);
                   9220:                        ext_find->attribute = 8;
                   9221:                }
                   9222:                find->drive = _getdrive();
                   9223:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9224:                find->attribute = 8;
                   9225:                find->nt_res = 0;
                   9226:                msdos_find_file_conv_local_time(&fd);
                   9227:                find->create_time_ms = 0;
                   9228:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9229:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9230:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9231:                find->cluster_hi = find->cluster_lo = 0;
                   9232:                find->file_size = 0;
1.1.1.14  root     9233:                dtainfo->allowable_mask &= ~8;
1.1       root     9234:                REG8(AL) = 0x00;
                   9235:        } else {
                   9236:                REG8(AL) = 0xff;
                   9237:        }
                   9238: }
                   9239: 
                   9240: inline void msdos_int_21h_13h()
                   9241: {
1.1.1.3   root     9242:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     9243:                REG8(AL) = 0xff;
                   9244:        } else {
                   9245:                REG8(AL) = 0x00;
                   9246:        }
                   9247: }
                   9248: 
1.1.1.16  root     9249: inline void msdos_int_21h_14h()
                   9250: {
                   9251:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9252:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9253:        process_t *process = msdos_process_info_get(current_psp);
                   9254:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9255:        DWORD num = 0;
                   9256:        
                   9257:        memset(mem + dta_laddr, 0, fcb->record_size);
                   9258:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9259:                REG8(AL) = 1;
                   9260:        } else {
                   9261:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9262:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9263:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9264:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9265:        }
                   9266: }
                   9267: 
                   9268: inline void msdos_int_21h_15h()
1.1.1.14  root     9269: {
                   9270:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9271:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     9272:        process_t *process = msdos_process_info_get(current_psp);
                   9273:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9274:        DWORD num = 0;
1.1.1.14  root     9275:        
1.1.1.16  root     9276:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9277:                REG8(AL) = 1;
                   9278:        } else {
                   9279:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9280:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9281:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9282:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9283:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9284:        }
                   9285: }
                   9286: 
                   9287: inline void msdos_int_21h_16h()
                   9288: {
                   9289:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9290:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45! root     9291:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9292:        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     9293:        
1.1.1.14  root     9294:        if(hFile == INVALID_HANDLE_VALUE) {
                   9295:                REG8(AL) = 0xff;
                   9296:        } else {
                   9297:                REG8(AL) = 0;
                   9298:                fcb->current_block = 0;
                   9299:                fcb->record_size = 128;
                   9300:                fcb->file_size = 0;
                   9301:                fcb->handle = hFile;
                   9302:                fcb->cur_record = 0;
                   9303:        }
                   9304: }
                   9305: 
1.1.1.16  root     9306: inline void msdos_int_21h_17h()
                   9307: {
                   9308:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9309:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45! root     9310: //     const char *path_src = msdos_fcb_path(fcb_src);
        !          9311:        char path_src[MAX_PATH];
        !          9312:        strcpy(path_src, msdos_fcb_path(fcb_src));
        !          9313:        
1.1.1.16  root     9314:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   9315:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45! root     9316: //     const char *path_dst = msdos_fcb_path(fcb_dst);
        !          9317:        char path_dst[MAX_PATH];
        !          9318:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     9319:        
                   9320:        if(rename(path_src, path_dst)) {
                   9321:                REG8(AL) = 0xff;
                   9322:        } else {
                   9323:                REG8(AL) = 0;
                   9324:        }
                   9325: }
                   9326: 
1.1       root     9327: inline void msdos_int_21h_18h()
                   9328: {
                   9329:        REG8(AL) = 0x00;
                   9330: }
                   9331: 
                   9332: inline void msdos_int_21h_19h()
                   9333: {
                   9334:        REG8(AL) = _getdrive() - 1;
                   9335: }
                   9336: 
                   9337: inline void msdos_int_21h_1ah()
                   9338: {
                   9339:        process_t *process = msdos_process_info_get(current_psp);
                   9340:        
                   9341:        process->dta.w.l = REG16(DX);
1.1.1.3   root     9342:        process->dta.w.h = SREG(DS);
1.1.1.23  root     9343:        msdos_sda_update(current_psp);
1.1       root     9344: }
                   9345: 
                   9346: inline void msdos_int_21h_1bh()
                   9347: {
                   9348:        int drive_num = _getdrive() - 1;
                   9349:        UINT16 seg, ofs;
                   9350:        
                   9351:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9352:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9353:                REG8(AL) = dpb->highest_sector_num + 1;
                   9354:                REG16(CX) = dpb->bytes_per_sector;
                   9355:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9356:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9357:        } else {
                   9358:                REG8(AL) = 0xff;
1.1.1.3   root     9359:                m_CF = 1;
1.1       root     9360:        }
                   9361: 
                   9362: }
                   9363: 
                   9364: inline void msdos_int_21h_1ch()
                   9365: {
1.1.1.41  root     9366:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     9367:        UINT16 seg, ofs;
                   9368:        
                   9369:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9370:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9371:                REG8(AL) = dpb->highest_sector_num + 1;
                   9372:                REG16(CX) = dpb->bytes_per_sector;
                   9373:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9374:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9375:        } else {
                   9376:                REG8(AL) = 0xff;
1.1.1.3   root     9377:                m_CF = 1;
1.1       root     9378:        }
                   9379: 
                   9380: }
                   9381: 
                   9382: inline void msdos_int_21h_1dh()
                   9383: {
                   9384:        REG8(AL) = 0;
                   9385: }
                   9386: 
                   9387: inline void msdos_int_21h_1eh()
                   9388: {
                   9389:        REG8(AL) = 0;
                   9390: }
                   9391: 
                   9392: inline void msdos_int_21h_1fh()
                   9393: {
                   9394:        int drive_num = _getdrive() - 1;
                   9395:        UINT16 seg, ofs;
                   9396:        
                   9397:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9398:                REG8(AL) = 0;
1.1.1.3   root     9399:                SREG(DS) = seg;
                   9400:                i386_load_segment_descriptor(DS);
1.1       root     9401:                REG16(BX) = ofs;
                   9402:        } else {
                   9403:                REG8(AL) = 0xff;
1.1.1.3   root     9404:                m_CF = 1;
1.1       root     9405:        }
                   9406: }
                   9407: 
                   9408: inline void msdos_int_21h_20h()
                   9409: {
                   9410:        REG8(AL) = 0;
                   9411: }
                   9412: 
1.1.1.14  root     9413: inline void msdos_int_21h_21h()
                   9414: {
                   9415:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9416:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9417:        
                   9418:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9419:                REG8(AL) = 1;
                   9420:        } else {
                   9421:                process_t *process = msdos_process_info_get(current_psp);
                   9422:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9423:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     9424:                DWORD num = 0;
1.1.1.14  root     9425:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9426:                        REG8(AL) = 1;
                   9427:                } else {
                   9428:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9429:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9430:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     9431:                }
                   9432:        }
                   9433: }
                   9434: 
                   9435: inline void msdos_int_21h_22h()
                   9436: {
                   9437:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9438:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9439:        
                   9440:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9441:                REG8(AL) = 0xff;
                   9442:        } else {
                   9443:                process_t *process = msdos_process_info_get(current_psp);
                   9444:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     9445:                DWORD num = 0;
1.1.1.14  root     9446:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   9447:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9448:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9449:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9450:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     9451:        }
                   9452: }
                   9453: 
1.1.1.16  root     9454: inline void msdos_int_21h_23h()
                   9455: {
                   9456:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9457:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45! root     9458:        const char *path = msdos_fcb_path(fcb);
1.1.1.16  root     9459:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9460:        
                   9461:        if(hFile == INVALID_HANDLE_VALUE) {
                   9462:                REG8(AL) = 0xff;
                   9463:        } else {
                   9464:                UINT32 size = GetFileSize(hFile, NULL);
                   9465:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   9466:                REG8(AL) = 0;
                   9467:        }
                   9468: }
                   9469: 
                   9470: inline void msdos_int_21h_24h()
                   9471: {
                   9472:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9473:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9474:        
                   9475:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   9476: }
                   9477: 
1.1       root     9478: inline void msdos_int_21h_25h()
                   9479: {
                   9480:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     9481:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     9482: }
                   9483: 
                   9484: inline void msdos_int_21h_26h()
                   9485: {
                   9486:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   9487:        
                   9488:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   9489:        psp->first_mcb = REG16(DX) + 16;
                   9490:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   9491:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   9492:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   9493:        psp->parent_psp = 0;
                   9494: }
                   9495: 
1.1.1.16  root     9496: inline void msdos_int_21h_27h()
                   9497: {
                   9498:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9499:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9500:        
                   9501:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9502:                REG8(AL) = 1;
                   9503:        } else {
                   9504:                process_t *process = msdos_process_info_get(current_psp);
                   9505:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9506:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   9507:                DWORD num = 0;
                   9508:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   9509:                        REG8(AL) = 1;
                   9510:                } else {
                   9511:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9512:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9513:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9514:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9515:                }
                   9516:        }
                   9517: }
                   9518: 
                   9519: inline void msdos_int_21h_28h()
                   9520: {
                   9521:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9522:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9523:        
                   9524:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9525:                REG8(AL) = 0xff;
                   9526:        } else {
                   9527:                process_t *process = msdos_process_info_get(current_psp);
                   9528:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9529:                DWORD num = 0;
                   9530:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   9531:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9532:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9533:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9534:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9535:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9536:        }
                   9537: }
                   9538: 
1.1       root     9539: inline void msdos_int_21h_29h()
                   9540: {
1.1.1.20  root     9541:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   9542:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     9543:        UINT8 drv = 0;
                   9544:        char sep_chars[] = ":.;,=+";
                   9545:        char end_chars[] = "\\<>|/\"[]";
                   9546:        char spc_chars[] = " \t";
                   9547:        
1.1.1.20  root     9548:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   9549:        buffer[1023] = 0;
                   9550:        memset(name, 0x20, sizeof(name));
                   9551:        memset(ext, 0x20, sizeof(ext));
                   9552:        
1.1       root     9553:        if(REG8(AL) & 1) {
1.1.1.20  root     9554:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   9555:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     9556:                        ofs++;
                   9557:                }
                   9558:        }
1.1.1.20  root     9559:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     9560:        
1.1.1.24  root     9561:        if(buffer[ofs + 1] == ':') {
                   9562:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   9563:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     9564:                        ofs += 2;
1.1.1.24  root     9565:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9566:                                ofs++;
                   9567:                        }
                   9568:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   9569:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     9570:                        ofs += 2;
1.1.1.24  root     9571:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9572:                                ofs++;
                   9573:                        }
1.1       root     9574:                }
                   9575:        }
1.1.1.20  root     9576:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9577:                UINT8 c = buffer[ofs];
                   9578:                if(is_kanji) {
                   9579:                        is_kanji = 0;
                   9580:                } else if(msdos_lead_byte_check(c)) {
                   9581:                        is_kanji = 1;
                   9582:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9583:                        break;
                   9584:                } else if(c >= 'a' && c <= 'z') {
                   9585:                        c -= 0x20;
                   9586:                }
                   9587:                ofs++;
                   9588:                name[i] = c;
                   9589:        }
1.1.1.20  root     9590:        if(buffer[ofs] == '.') {
1.1       root     9591:                ofs++;
1.1.1.20  root     9592:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9593:                        UINT8 c = buffer[ofs];
                   9594:                        if(is_kanji) {
                   9595:                                is_kanji = 0;
                   9596:                        } else if(msdos_lead_byte_check(c)) {
                   9597:                                is_kanji = 1;
                   9598:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9599:                                break;
                   9600:                        } else if(c >= 'a' && c <= 'z') {
                   9601:                                c -= 0x20;
                   9602:                        }
                   9603:                        ofs++;
                   9604:                        ext[i] = c;
                   9605:                }
                   9606:        }
1.1.1.20  root     9607:        int si = REG16(SI) + ofs;
1.1.1.3   root     9608:        int ds = SREG(DS);
1.1       root     9609:        while(si > 0xffff) {
                   9610:                si -= 0x10;
                   9611:                ds++;
                   9612:        }
                   9613:        REG16(SI) = si;
1.1.1.3   root     9614:        SREG(DS) = ds;
                   9615:        i386_load_segment_descriptor(DS);
1.1       root     9616:        
1.1.1.3   root     9617:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     9618:        if(!(REG8(AL) & 2) || drv != 0) {
                   9619:                fcb[0] = drv;
                   9620:        }
                   9621:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   9622:                memcpy(fcb + 1, name, 8);
                   9623:        }
                   9624:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   9625:                memcpy(fcb + 9, ext, 3);
                   9626:        }
                   9627:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     9628:                if(fcb[i] == '*') {
                   9629:                        found_star = 1;
                   9630:                }
                   9631:                if(found_star) {
                   9632:                        fcb[i] = '?';
                   9633:                }
                   9634:        }
1.1.1.20  root     9635:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     9636:                if(fcb[i] == '*') {
                   9637:                        found_star = 1;
                   9638:                }
                   9639:                if(found_star) {
                   9640:                        fcb[i] = '?';
                   9641:                }
                   9642:        }
                   9643:        
1.1.1.44  root     9644:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     9645:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   9646:                        REG8(AL) = 0x01;
1.1.1.20  root     9647:                } else {
                   9648:                        REG8(AL) = 0x00;
1.1       root     9649:                }
                   9650:        } else {
                   9651:                REG8(AL) = 0xff;
                   9652:        }
                   9653: }
                   9654: 
                   9655: inline void msdos_int_21h_2ah()
                   9656: {
                   9657:        SYSTEMTIME sTime;
                   9658:        
                   9659:        GetLocalTime(&sTime);
                   9660:        REG16(CX) = sTime.wYear;
                   9661:        REG8(DH) = (UINT8)sTime.wMonth;
                   9662:        REG8(DL) = (UINT8)sTime.wDay;
                   9663:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   9664: }
                   9665: 
                   9666: inline void msdos_int_21h_2bh()
                   9667: {
1.1.1.14  root     9668:        REG8(AL) = 0xff;
1.1       root     9669: }
                   9670: 
                   9671: inline void msdos_int_21h_2ch()
                   9672: {
                   9673:        SYSTEMTIME sTime;
                   9674:        
                   9675:        GetLocalTime(&sTime);
                   9676:        REG8(CH) = (UINT8)sTime.wHour;
                   9677:        REG8(CL) = (UINT8)sTime.wMinute;
                   9678:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     9679:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     9680: }
                   9681: 
                   9682: inline void msdos_int_21h_2dh()
                   9683: {
                   9684:        REG8(AL) = 0x00;
                   9685: }
                   9686: 
                   9687: inline void msdos_int_21h_2eh()
                   9688: {
                   9689:        process_t *process = msdos_process_info_get(current_psp);
                   9690:        
                   9691:        process->verify = REG8(AL);
                   9692: }
                   9693: 
                   9694: inline void msdos_int_21h_2fh()
                   9695: {
                   9696:        process_t *process = msdos_process_info_get(current_psp);
                   9697:        
                   9698:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     9699:        SREG(ES) = process->dta.w.h;
                   9700:        i386_load_segment_descriptor(ES);
1.1       root     9701: }
                   9702: 
                   9703: inline void msdos_int_21h_30h()
                   9704: {
                   9705:        // Version Flag / OEM
1.1.1.27  root     9706:        if(REG8(AL) == 0x01) {
1.1.1.29  root     9707: #ifdef SUPPORT_HMA
                   9708:                REG16(BX) = 0x0000;
                   9709: #else
                   9710:                REG16(BX) = 0x1000; // DOS is in HMA
                   9711: #endif
1.1       root     9712:        } else {
1.1.1.27  root     9713:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   9714:                // but this is not correct on Windows 98 SE
                   9715: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   9716:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     9717:        }
1.1.1.27  root     9718:        REG16(CX) = 0x0000;
1.1.1.30  root     9719:        REG8(AL) = dos_major_version;   // 7
                   9720:        REG8(AH) = dos_minor_version;   // 10
1.1       root     9721: }
                   9722: 
                   9723: inline void msdos_int_21h_31h()
                   9724: {
1.1.1.29  root     9725:        try {
                   9726:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9727:        } catch(...) {
                   9728:                // recover the broken mcb
                   9729:                int mcb_seg = current_psp - 1;
                   9730:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     9731:                
1.1.1.29  root     9732:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     9733:                        mcb->mz = 'M';
                   9734:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   9735:                        
1.1.1.29  root     9736:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     9737:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     9738:                        } else {
1.1.1.39  root     9739:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     9740:                        }
                   9741:                } else {
                   9742:                        mcb->mz = 'Z';
1.1.1.30  root     9743:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     9744:                }
                   9745:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9746:        }
1.1       root     9747:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   9748: }
                   9749: 
                   9750: inline void msdos_int_21h_32h()
                   9751: {
                   9752:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   9753:        UINT16 seg, ofs;
                   9754:        
                   9755:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9756:                REG8(AL) = 0;
1.1.1.3   root     9757:                SREG(DS) = seg;
                   9758:                i386_load_segment_descriptor(DS);
1.1       root     9759:                REG16(BX) = ofs;
                   9760:        } else {
                   9761:                REG8(AL) = 0xff;
1.1.1.3   root     9762:                m_CF = 1;
1.1       root     9763:        }
                   9764: }
                   9765: 
                   9766: inline void msdos_int_21h_33h()
                   9767: {
                   9768:        char path[MAX_PATH];
                   9769:        
                   9770:        switch(REG8(AL)) {
                   9771:        case 0x00:
1.1.1.33  root     9772:                REG8(DL) = ctrl_break_checking;
1.1       root     9773:                break;
                   9774:        case 0x01:
1.1.1.33  root     9775:                ctrl_break_checking = REG8(DL);
                   9776:                break;
                   9777:        case 0x02:
                   9778:                {
                   9779:                        UINT8 old = ctrl_break_checking;
                   9780:                        ctrl_break_checking = REG8(DL);
                   9781:                        REG8(DL) = old;
                   9782:                }
                   9783:                break;
                   9784:        case 0x03:
                   9785:        case 0x04:
                   9786:                // DOS 4.0+ - Unused
1.1       root     9787:                break;
                   9788:        case 0x05:
                   9789:                GetSystemDirectory(path, MAX_PATH);
                   9790:                if(path[0] >= 'a' && path[0] <= 'z') {
                   9791:                        REG8(DL) = path[0] - 'a' + 1;
                   9792:                } else {
                   9793:                        REG8(DL) = path[0] - 'A' + 1;
                   9794:                }
                   9795:                break;
                   9796:        case 0x06:
1.1.1.2   root     9797:                // MS-DOS version (7.10)
1.1       root     9798:                REG8(BL) = 7;
1.1.1.2   root     9799:                REG8(BH) = 10;
1.1       root     9800:                REG8(DL) = 0;
1.1.1.29  root     9801: #ifdef SUPPORT_HMA
                   9802:                REG8(DH) = 0x00;
                   9803: #else
                   9804:                REG8(DH) = 0x10; // DOS is in HMA
                   9805: #endif
1.1       root     9806:                break;
1.1.1.6   root     9807:        case 0x07:
                   9808:                if(REG8(DL) == 0) {
                   9809:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   9810:                } else if(REG8(DL) == 1) {
                   9811:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   9812:                }
                   9813:                break;
1.1       root     9814:        default:
1.1.1.22  root     9815:                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     9816:                REG16(AX) = 0x01;
1.1.1.3   root     9817:                m_CF = 1;
1.1       root     9818:                break;
                   9819:        }
                   9820: }
                   9821: 
1.1.1.23  root     9822: inline void msdos_int_21h_34h()
                   9823: {
                   9824:        SREG(ES) = SDA_TOP >> 4;
                   9825:        i386_load_segment_descriptor(ES);
                   9826:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   9827: }
                   9828: 
1.1       root     9829: inline void msdos_int_21h_35h()
                   9830: {
                   9831:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     9832:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   9833:        i386_load_segment_descriptor(ES);
1.1       root     9834: }
                   9835: 
                   9836: inline void msdos_int_21h_36h()
                   9837: {
                   9838:        struct _diskfree_t df = {0};
                   9839:        
                   9840:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   9841:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   9842:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     9843:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   9844:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     9845:        } else {
                   9846:                REG16(AX) = 0xffff;
                   9847:        }
                   9848: }
                   9849: 
                   9850: inline void msdos_int_21h_37h()
                   9851: {
1.1.1.22  root     9852:        static UINT8 dev_flag = 0xff;
1.1       root     9853:        
                   9854:        switch(REG8(AL)) {
                   9855:        case 0x00:
1.1.1.22  root     9856:                {
                   9857:                        process_t *process = msdos_process_info_get(current_psp);
                   9858:                        REG8(AL) = 0x00;
                   9859:                        REG8(DL) = process->switchar;
                   9860:                }
1.1       root     9861:                break;
                   9862:        case 0x01:
1.1.1.22  root     9863:                {
                   9864:                        process_t *process = msdos_process_info_get(current_psp);
                   9865:                        REG8(AL) = 0x00;
                   9866:                        process->switchar = REG8(DL);
1.1.1.23  root     9867:                        msdos_sda_update(current_psp);
1.1.1.22  root     9868:                }
                   9869:                break;
                   9870:        case 0x02:
                   9871:                REG8(DL) = dev_flag;
                   9872:                break;
                   9873:        case 0x03:
                   9874:                dev_flag = REG8(DL);
                   9875:                break;
                   9876:        case 0xd0:
                   9877:        case 0xd1:
                   9878:        case 0xd2:
                   9879:        case 0xd3:
                   9880:        case 0xd4:
                   9881:        case 0xd5:
                   9882:        case 0xd6:
                   9883:        case 0xd7:
                   9884:        case 0xdc:
                   9885:        case 0xdd:
                   9886:        case 0xde:
                   9887:        case 0xdf:
                   9888:                // diet ???
                   9889:                REG16(AX) = 1;
1.1       root     9890:                break;
                   9891:        default:
1.1.1.22  root     9892:                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     9893:                REG16(AX) = 1;
                   9894:                break;
                   9895:        }
                   9896: }
                   9897: 
1.1.1.42  root     9898: int get_country_info(country_info_t *ci, LCID locale)
1.1.1.17  root     9899: {
                   9900:        char LCdata[80];
                   9901:        
1.1.1.19  root     9902:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.42  root     9903:        GetLocaleInfo(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     9904:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.42  root     9905:        GetLocaleInfo(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     9906:        ci->currency_format = *LCdata - '0';
1.1.1.42  root     9907:        GetLocaleInfo(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     9908:        ci->date_format = *LCdata - '0';
1.1.1.42  root     9909:        GetLocaleInfo(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     9910:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.42  root     9911:        GetLocaleInfo(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     9912:        *ci->date_sep = *LCdata;
1.1.1.42  root     9913:        GetLocaleInfo(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     9914:        *ci->dec_sep = *LCdata;
1.1.1.42  root     9915:        GetLocaleInfo(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     9916:        *ci->list_sep = *LCdata;
1.1.1.42  root     9917:        GetLocaleInfo(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     9918:        *ci->thou_sep = *LCdata;
1.1.1.42  root     9919:        GetLocaleInfo(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     9920:        *ci->time_sep = *LCdata;
1.1.1.42  root     9921:        GetLocaleInfo(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     9922:        if(strchr(LCdata, 'H') != NULL) {
                   9923:                ci->time_format = 1;
                   9924:        }
1.1.1.27  root     9925:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffd:000a
1.1.1.24  root     9926:        ci->case_map.w.h = 0xfffd;
1.1.1.42  root     9927:        GetLocaleInfo(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     9928:        return atoi(LCdata);
                   9929: }
                   9930: 
1.1.1.42  root     9931: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   9932: {
                   9933:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   9934: }
                   9935: 
                   9936: int get_country_info(country_info_t *ci)
                   9937: {
                   9938:        return get_country_info(ci, LOCALE_USER_DEFAULT);
                   9939: }
                   9940: 
1.1.1.43  root     9941: void set_country_info(country_info_t *ci, int size)
                   9942: {
                   9943:        char LCdata[80];
                   9944:        
                   9945:        if(size >= 0x00 + 2) {
                   9946:                memset(LCdata, 0, sizeof(LCdata));
                   9947:                *LCdata = '0' + ci->date_format;
                   9948:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
                   9949:        }
                   9950:        if(size >= 0x02 + 5) {
                   9951:                memset(LCdata, 0, sizeof(LCdata));
                   9952:                memcpy(LCdata, &ci->currency_symbol, 4);
                   9953:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
                   9954:        }
                   9955:        if(size >= 0x07 + 2) {
                   9956:                memset(LCdata, 0, sizeof(LCdata));
                   9957:                *LCdata = *ci->thou_sep;
                   9958:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
                   9959:        }
                   9960:        if(size >= 0x09 + 2) {
                   9961:                memset(LCdata, 0, sizeof(LCdata));
                   9962:                *LCdata = *ci->dec_sep;
                   9963:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
                   9964:        }
                   9965:        if(size >= 0x0b + 2) {
                   9966:                memset(LCdata, 0, sizeof(LCdata));
                   9967:                *LCdata = *ci->date_sep;
                   9968:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
                   9969:        }
                   9970:        if(size >= 0x0d + 2) {
                   9971:                memset(LCdata, 0, sizeof(LCdata));
                   9972:                *LCdata = *ci->time_sep;
                   9973:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
                   9974:        }
                   9975:        if(size >= 0x0f + 1) {
                   9976:                memset(LCdata, 0, sizeof(LCdata));
                   9977:                *LCdata = '0' + ci->currency_format;
                   9978:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
                   9979:        }
                   9980:        if(size >= 0x10 + 1) {
                   9981:                sprintf(LCdata, "%d", ci->currency_dec_digits);
                   9982:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
                   9983:        }
                   9984:        if(size >= 0x11 + 1) {
                   9985:                // FIXME: is time format always H/h:mm:ss ???
                   9986:                if(ci->time_format & 1) {
                   9987:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   9988:                } else {
                   9989:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   9990:                }
                   9991:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
                   9992:        }
                   9993:        if(size >= 0x12 + 4) {
                   9994:                // 12h  DWORD   address of case map routine
                   9995:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   9996:        }
                   9997:        if(size >= 0x16 + 2) {
                   9998:                memset(LCdata, 0, sizeof(LCdata));
                   9999:                *LCdata = *ci->list_sep;
                   10000:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
                   10001:        }
                   10002: }
                   10003: 
1.1.1.42  root     10004: #ifndef SUBLANG_SWAHILI
                   10005:        #define SUBLANG_SWAHILI 0x01
                   10006: #endif
                   10007: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10008:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10009: #endif
                   10010: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10011:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10012: #endif
                   10013: #ifndef LANG_BANGLA
                   10014:        #define LANG_BANGLA 0x45
                   10015: #endif
                   10016: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10017:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10018: #endif
                   10019: 
                   10020: static const struct {
                   10021:        int code;
                   10022:        USHORT usPrimaryLanguage;
                   10023:        USHORT usSubLanguage;
                   10024: } country_table[] = {
                   10025:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10026:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10027:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10028:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10029:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10030:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10031: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10032: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10033: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10034: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10035: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10036:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10037:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10038: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10039:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10040: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10041:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10042:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10043:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10044:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10045:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10046:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10047:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10048: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10049: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10050:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10051:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10052:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10053:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10054:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10055: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10056: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10057: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10058:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10059: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10060: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10061: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10062: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10063:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10064:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10065:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10066: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10067:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10068:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10069:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10070:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10071:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10072:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10073:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10074:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10075:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10076:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10077:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10078:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10079:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10080: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10081:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10082:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10083:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10084:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10085:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10086:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10087:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10088:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10089:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10090:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10091: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10092:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10093: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10094:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10095:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10096:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10097:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10098:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10099:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10100:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10101:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10102: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10103:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10104: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10105: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10106:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   10107: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   10108:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   10109:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   10110:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   10111:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   10112:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   10113:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   10114:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   10115: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   10116: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   10117:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   10118: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   10119:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   10120:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   10121:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   10122:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   10123: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   10124: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   10125: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   10126:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   10127:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   10128:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   10129:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   10130:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   10131:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   10132:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   10133:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   10134:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   10135:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   10136:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   10137:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   10138:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   10139:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   10140:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   10141:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   10142:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   10143:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   10144:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   10145:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   10146:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     10147: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     10148:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   10149: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   10150:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   10151:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   10152:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   10153:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   10154:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   10155:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   10156:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   10157:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   10158:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   10159:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   10160:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   10161:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   10162:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   10163:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   10164:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   10165:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   10166:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   10167:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   10168:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   10169:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   10170:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   10171:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   10172:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   10173:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   10174:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   10175: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   10176:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   10177:        {-1, 0, 0},
                   10178: };
                   10179: 
1.1.1.14  root     10180: inline void msdos_int_21h_38h()
                   10181: {
                   10182:        switch(REG8(AL)) {
                   10183:        case 0x00:
1.1.1.19  root     10184:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     10185:                break;
                   10186:        default:
1.1.1.42  root     10187:                for(int i = 0;; i++) {
                   10188:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   10189:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   10190:                                break;
                   10191:                        } else if(country_table[i].code == -1) {
                   10192: //                             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));
                   10193: //                             REG16(AX) = 2;
                   10194: //                             m_CF = 1;
                   10195:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   10196:                                break;
                   10197:                        }
                   10198:                }
1.1.1.14  root     10199:                break;
                   10200:        }
                   10201: }
                   10202: 
1.1       root     10203: inline void msdos_int_21h_39h(int lfn)
                   10204: {
1.1.1.3   root     10205:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10206:                REG16(AX) = errno;
1.1.1.3   root     10207:                m_CF = 1;
1.1       root     10208:        }
                   10209: }
                   10210: 
                   10211: inline void msdos_int_21h_3ah(int lfn)
                   10212: {
1.1.1.3   root     10213:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10214:                REG16(AX) = errno;
1.1.1.3   root     10215:                m_CF = 1;
1.1       root     10216:        }
                   10217: }
                   10218: 
                   10219: inline void msdos_int_21h_3bh(int lfn)
                   10220: {
1.1.1.45! root     10221:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     10222:        
                   10223:        if(_chdir(path)) {
1.1.1.17  root     10224:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     10225:                m_CF = 1;
1.1.1.44  root     10226:        } else {
                   10227:                int drv = _getdrive() - 1;
                   10228:                if(path[1] == ':') {
                   10229:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   10230:                                drv = path[0] - 'A';
                   10231:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   10232:                                drv = path[0] - 'a';
                   10233:                        }
                   10234:                }
                   10235:                msdos_cds_update(drv, path);
1.1       root     10236:        }
                   10237: }
                   10238: 
                   10239: inline void msdos_int_21h_3ch()
                   10240: {
1.1.1.45! root     10241:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10242:        int attr = GetFileAttributes(path);
1.1.1.37  root     10243:        int fd = -1;
                   10244:        int sio_port = 0;
                   10245:        int lpt_port = 0;
1.1       root     10246:        
1.1.1.45! root     10247:        if(msdos_is_device_path(path)) {
        !          10248:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     10249:        } else {
                   10250:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10251:        }
                   10252:        if(fd != -1) {
                   10253:                if(attr == -1) {
                   10254:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   10255:                }
                   10256:                SetFileAttributes(path, attr);
                   10257:                REG16(AX) = fd;
1.1.1.45! root     10258:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10259:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10260:        } else {
                   10261:                REG16(AX) = errno;
1.1.1.3   root     10262:                m_CF = 1;
1.1       root     10263:        }
                   10264: }
                   10265: 
                   10266: inline void msdos_int_21h_3dh()
                   10267: {
1.1.1.45! root     10268:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10269:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     10270:        int fd = -1;
                   10271:        int sio_port = 0;
                   10272:        int lpt_port = 0;
1.1       root     10273:        
                   10274:        if(mode < 0x03) {
1.1.1.45! root     10275:                if(msdos_is_device_path(path)) {
        !          10276:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     10277:                } else {
1.1.1.13  root     10278:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     10279:                }
1.1       root     10280:                if(fd != -1) {
                   10281:                        REG16(AX) = fd;
1.1.1.45! root     10282:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10283:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10284:                } else {
                   10285:                        REG16(AX) = errno;
1.1.1.3   root     10286:                        m_CF = 1;
1.1       root     10287:                }
                   10288:        } else {
                   10289:                REG16(AX) = 0x0c;
1.1.1.3   root     10290:                m_CF = 1;
1.1       root     10291:        }
                   10292: }
                   10293: 
                   10294: inline void msdos_int_21h_3eh()
                   10295: {
                   10296:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10297:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10298:        
1.1.1.20  root     10299:        if(fd < process->max_files && file_handler[fd].valid) {
                   10300:                _close(fd);
                   10301:                msdos_file_handler_close(fd);
                   10302:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     10303:        } else {
                   10304:                REG16(AX) = 0x06;
1.1.1.3   root     10305:                m_CF = 1;
1.1       root     10306:        }
                   10307: }
                   10308: 
1.1.1.35  root     10309: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   10310: {
                   10311:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   10312:        int max = REG16(CX);
                   10313:        int p = 0;
                   10314:        
                   10315:        while(max > p) {
                   10316:                int chr = msdos_getch();
                   10317:                
                   10318:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   10319:                        p = 0;
                   10320:                        buf[p++] = 0x0d;
                   10321:                        if(max > p) {
                   10322:                                buf[p++] = 0x0a;
                   10323:                        }
                   10324:                        msdos_putch(0x03);
                   10325:                        msdos_putch(0x0d);
                   10326:                        msdos_putch(0x0a);
                   10327:                        break;
                   10328:                } else if(ctrl_break_pressed) {
                   10329:                        // skip this byte
                   10330:                } else if(chr == 0x00) {
                   10331:                        // skip 2nd byte
                   10332:                        msdos_getch();
                   10333:                } else if(chr == 0x0d) {
                   10334:                        // carriage return
                   10335:                        buf[p++] = 0x0d;
                   10336:                        if(max > p) {
                   10337:                                buf[p++] = 0x0a;
                   10338:                        }
                   10339:                        msdos_putch('\n');
                   10340:                        break;
                   10341:                } else if(chr == 0x08) {
                   10342:                        // back space
                   10343:                        if(p > 0) {
                   10344:                                p--;
                   10345:                                if(msdos_ctrl_code_check(buf[p])) {
                   10346:                                        msdos_putch(0x08);
                   10347:                                        msdos_putch(0x08);
                   10348:                                        msdos_putch(0x20);
                   10349:                                        msdos_putch(0x20);
                   10350:                                        msdos_putch(0x08);
                   10351:                                        msdos_putch(0x08);
1.1.1.36  root     10352:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   10353:                                        p--;
                   10354:                                        msdos_putch(0x08);
                   10355:                                        msdos_putch(0x08);
                   10356:                                        msdos_putch(0x20);
                   10357:                                        msdos_putch(0x20);
                   10358:                                        msdos_putch(0x08);
                   10359:                                        msdos_putch(0x08);
1.1.1.35  root     10360:                                } else {
                   10361:                                        msdos_putch(0x08);
                   10362:                                        msdos_putch(0x20);
                   10363:                                        msdos_putch(0x08);
                   10364:                                }
                   10365:                        }
                   10366:                } else if(chr == 0x1b) {
                   10367:                        // escape
                   10368:                        while(p > 0) {
                   10369:                                p--;
                   10370:                                if(msdos_ctrl_code_check(buf[p])) {
                   10371:                                        msdos_putch(0x08);
                   10372:                                        msdos_putch(0x08);
                   10373:                                        msdos_putch(0x20);
                   10374:                                        msdos_putch(0x20);
                   10375:                                        msdos_putch(0x08);
                   10376:                                        msdos_putch(0x08);
                   10377:                                } else {
                   10378:                                        msdos_putch(0x08);
                   10379:                                        msdos_putch(0x20);
                   10380:                                        msdos_putch(0x08);
                   10381:                                }
                   10382:                        }
                   10383:                } else {
                   10384:                        buf[p++] = chr;
                   10385:                        msdos_putch(chr);
                   10386:                }
                   10387:        }
                   10388:        REG16(AX) = p;
                   10389:        
                   10390: #ifdef USE_SERVICE_THREAD
                   10391:        service_exit = true;
                   10392: #endif
                   10393:        return(0);
                   10394: }
                   10395: 
1.1       root     10396: inline void msdos_int_21h_3fh()
                   10397: {
                   10398:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10399:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10400:        
1.1.1.20  root     10401:        if(fd < process->max_files && file_handler[fd].valid) {
                   10402:                if(file_mode[file_handler[fd].mode].in) {
                   10403:                        if(file_handler[fd].atty) {
1.1       root     10404:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     10405:                                if(REG16(CX) != 0) {
                   10406: #ifdef USE_SERVICE_THREAD
                   10407:                                        start_service_loop(msdos_int_21h_3fh_thread);
                   10408: #else
                   10409:                                        msdos_int_21h_3fh_thread(NULL);
                   10410:                                        REQUEST_HARDWRE_UPDATE();
                   10411: #endif
                   10412:                                } else {
                   10413:                                        REG16(AX) = 0;
1.1       root     10414:                                }
                   10415:                        } else {
1.1.1.37  root     10416:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10417:                        }
                   10418:                } else {
                   10419:                        REG16(AX) = 0x05;
1.1.1.3   root     10420:                        m_CF = 1;
1.1       root     10421:                }
                   10422:        } else {
                   10423:                REG16(AX) = 0x06;
1.1.1.3   root     10424:                m_CF = 1;
1.1       root     10425:        }
                   10426: }
                   10427: 
                   10428: inline void msdos_int_21h_40h()
                   10429: {
                   10430:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10431:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10432:        
1.1.1.20  root     10433:        if(fd < process->max_files && file_handler[fd].valid) {
                   10434:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     10435:                        if(REG16(CX)) {
1.1.1.20  root     10436:                                if(file_handler[fd].atty) {
1.1       root     10437:                                        // BX is stdout/stderr or is redirected to stdout
                   10438:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     10439:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     10440:                                        }
                   10441:                                        REG16(AX) = REG16(CX);
                   10442:                                } else {
1.1.1.20  root     10443:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10444:                                }
                   10445:                        } else {
1.1.1.20  root     10446:                                UINT32 pos = _tell(fd);
                   10447:                                _lseek(fd, 0, SEEK_END);
                   10448:                                UINT32 size = _tell(fd);
1.1.1.12  root     10449:                                if(pos < size) {
1.1.1.20  root     10450:                                        _lseek(fd, pos, SEEK_SET);
                   10451:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     10452:                                } else {
                   10453:                                        for(UINT32 i = size; i < pos; i++) {
                   10454:                                                UINT8 tmp = 0;
1.1.1.23  root     10455:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     10456:                                        }
1.1.1.20  root     10457:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     10458:                                }
1.1.1.23  root     10459:                                REG16(AX) = 0;
1.1       root     10460:                        }
                   10461:                } else {
                   10462:                        REG16(AX) = 0x05;
1.1.1.3   root     10463:                        m_CF = 1;
1.1       root     10464:                }
                   10465:        } else {
                   10466:                REG16(AX) = 0x06;
1.1.1.3   root     10467:                m_CF = 1;
1.1       root     10468:        }
                   10469: }
                   10470: 
                   10471: inline void msdos_int_21h_41h(int lfn)
                   10472: {
1.1.1.3   root     10473:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10474:                REG16(AX) = errno;
1.1.1.3   root     10475:                m_CF = 1;
1.1       root     10476:        }
                   10477: }
                   10478: 
                   10479: inline void msdos_int_21h_42h()
                   10480: {
                   10481:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10482:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10483:        
1.1.1.20  root     10484:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     10485:                if(REG8(AL) < 0x03) {
1.1.1.35  root     10486:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     10487:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   10488:                        UINT32 pos = _tell(fd);
1.1       root     10489:                        REG16(AX) = pos & 0xffff;
                   10490:                        REG16(DX) = (pos >> 16);
                   10491:                } else {
                   10492:                        REG16(AX) = 0x01;
1.1.1.3   root     10493:                        m_CF = 1;
1.1       root     10494:                }
                   10495:        } else {
                   10496:                REG16(AX) = 0x06;
1.1.1.3   root     10497:                m_CF = 1;
1.1       root     10498:        }
                   10499: }
                   10500: 
                   10501: inline void msdos_int_21h_43h(int lfn)
                   10502: {
1.1.1.45! root     10503:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     10504:        int attr;
                   10505:        
1.1.1.14  root     10506:        if(!lfn && REG8(AL) > 2) {
                   10507:                REG16(AX) = 0x01;
                   10508:                m_CF = 1;
                   10509:                return;
                   10510:        }
                   10511:        switch(REG8(lfn ? BL : AL)) {
1.1       root     10512:        case 0x00:
                   10513:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     10514:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   10515:                } else {
                   10516:                        REG16(AX) = (UINT16)GetLastError();
                   10517:                        m_CF = 1;
                   10518:                }
                   10519:                break;
                   10520:        case 0x01:
                   10521:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   10522:                        REG16(AX) = (UINT16)GetLastError();
                   10523:                        m_CF = 1;
                   10524:                }
                   10525:                break;
                   10526:        case 0x02:
                   10527:                {
1.1.1.45! root     10528:                        DWORD compressed_size = GetCompressedFileSize(path, NULL), file_size = 0;
        !          10529:                        if(compressed_size != INVALID_FILE_SIZE) {
        !          10530:                                if(compressed_size != 0) {
        !          10531:                                        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        !          10532:                                        if(hFile != INVALID_HANDLE_VALUE) {
        !          10533:                                                file_size = GetFileSize(hFile, NULL);
        !          10534:                                                CloseHandle(hFile);
        !          10535:                                        }
        !          10536:                                        if(compressed_size == file_size) {
        !          10537:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
        !          10538:                                                // this isn't correct if the file is in the NTFS MFT
        !          10539:                                                if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
        !          10540:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
        !          10541:                                                }
1.1.1.14  root     10542:                                        }
                   10543:                                }
1.1.1.45! root     10544:                                REG16(AX) = LOWORD(compressed_size);
        !          10545:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     10546:                        } else {
                   10547:                                REG16(AX) = (UINT16)GetLastError();
                   10548:                                m_CF = 1;
1.1       root     10549:                        }
1.1.1.14  root     10550:                }
                   10551:                break;
                   10552:        case 0x03:
                   10553:        case 0x05:
                   10554:        case 0x07:
                   10555:                {
                   10556:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10557:                        if(hFile != INVALID_HANDLE_VALUE) {
                   10558:                                FILETIME local, time;
                   10559:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   10560:                                if(REG8(BL) == 7) {
                   10561:                                        ULARGE_INTEGER hund;
                   10562:                                        hund.LowPart = local.dwLowDateTime;
                   10563:                                        hund.HighPart = local.dwHighDateTime;
                   10564:                                        hund.QuadPart += REG16(SI) * 100000;
                   10565:                                        local.dwLowDateTime = hund.LowPart;
                   10566:                                        local.dwHighDateTime = hund.HighPart;
                   10567:                                }
                   10568:                                LocalFileTimeToFileTime(&local, &time);
                   10569:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   10570:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   10571:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   10572:                                        REG16(AX) = (UINT16)GetLastError();
                   10573:                                        m_CF = 1;
                   10574:                                }
                   10575:                                CloseHandle(hFile);
                   10576:                        } else {
                   10577:                                REG16(AX) = (UINT16)GetLastError();
                   10578:                                m_CF = 1;
1.1       root     10579:                        }
1.1.1.14  root     10580:                }
                   10581:                break;
                   10582:        case 0x04:
                   10583:        case 0x06:
                   10584:        case 0x08:
                   10585:                {
                   10586:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   10587:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   10588:                                FILETIME *time, local;
                   10589:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   10590:                                                   0x06 ? &fad.ftLastAccessTime :
                   10591:                                                          &fad.ftCreationTime;
                   10592:                                FileTimeToLocalFileTime(time, &local);
                   10593:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   10594:                                if(REG8(BL) == 0x08) {
                   10595:                                        ULARGE_INTEGER hund;
                   10596:                                        hund.LowPart = local.dwLowDateTime;
                   10597:                                        hund.HighPart = local.dwHighDateTime;
                   10598:                                        hund.QuadPart /= 100000;
                   10599:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   10600:                                }
                   10601:                        } else {
                   10602:                                REG16(AX) = (UINT16)GetLastError();
                   10603:                                m_CF = 1;
1.1       root     10604:                        }
1.1.1.14  root     10605:                }
                   10606:                break;
1.1.1.43  root     10607:        case 0xff:
                   10608:                if(REG16(BP) == 0x5053) {
                   10609:                        if(REG8(CL) == 0x39) {
                   10610:                                msdos_int_21h_39h(1);
                   10611:                                break;
                   10612:                        } else if(REG8(CL) == 0x56) {
                   10613:                                msdos_int_21h_56h(1);
                   10614:                                break;
                   10615:                        }
                   10616:                }
1.1.1.14  root     10617:        default:
1.1.1.22  root     10618:                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     10619:                REG16(AX) = 0x01;
                   10620:                m_CF = 1;
                   10621:                break;
                   10622:        }
                   10623: }
                   10624: 
                   10625: inline void msdos_int_21h_44h()
                   10626: {
1.1.1.22  root     10627:        static UINT16 iteration_count = 0;
                   10628:        
1.1.1.44  root     10629:        process_t *process;
                   10630:        int fd, drv;
1.1.1.14  root     10631:        
                   10632:        switch(REG8(AL)) {
                   10633:        case 0x00:
                   10634:        case 0x01:
                   10635:        case 0x02:
                   10636:        case 0x03:
                   10637:        case 0x04:
                   10638:        case 0x05:
                   10639:        case 0x06:
                   10640:        case 0x07:
1.1.1.44  root     10641:                process = msdos_process_info_get(current_psp);
                   10642:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     10643:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   10644:                        REG16(AX) = 0x06;
                   10645:                        m_CF = 1;
                   10646:                        return;
1.1.1.14  root     10647:                }
                   10648:                break;
                   10649:        case 0x08:
                   10650:        case 0x09:
1.1.1.44  root     10651:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   10652:                if(!msdos_is_valid_drive(drv)) {
                   10653:                        // invalid drive
1.1.1.14  root     10654:                        REG16(AX) = 0x0f;
                   10655:                        m_CF = 1;
                   10656:                        return;
1.1       root     10657:                }
                   10658:                break;
                   10659:        }
                   10660:        switch(REG8(AL)) {
                   10661:        case 0x00: // get ioctrl data
1.1.1.20  root     10662:                REG16(DX) = file_handler[fd].info;
1.1       root     10663:                break;
                   10664:        case 0x01: // set ioctrl data
1.1.1.45! root     10665:                if(REG8(DH) != 0) {
        !          10666: //                     REG16(AX) = 0x0d; // data invalid
        !          10667: //                     m_CF = 1;
        !          10668:                        file_handler[fd].info = REG16(DX);
        !          10669:                } else {
        !          10670:                        file_handler[fd].info &= 0xff00;
        !          10671:                        file_handler[fd].info |= REG8(DL);
        !          10672:                }
1.1       root     10673:                break;
                   10674:        case 0x02: // recv from character device
1.1.1.45! root     10675:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
        !          10676:                        // from DOSBox
        !          10677:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
        !          10678:                        case 0x00:
        !          10679:                                if(REG16(CX) >= 6) {
        !          10680:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
        !          10681:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
        !          10682:                                        REG16(AX) = 6; // number of bytes actually read
        !          10683:                                } else {
        !          10684:                                        REG16(AX) = 0x0d; // data invalid
        !          10685:                                        m_CF = 1;
        !          10686:                                }
        !          10687:                                break;
        !          10688:                        case 0x01:
        !          10689:                                if(REG16(CX) >= 6) {
        !          10690:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
        !          10691:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
        !          10692:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
        !          10693:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
        !          10694:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
        !          10695:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
        !          10696:                                                        int page = (addr - EMS_TOP) / 0x4000;
        !          10697:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
        !          10698:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
        !          10699:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
        !          10700:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
        !          10701:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
        !          10702:                                                } else {
        !          10703:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
        !          10704:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
        !          10705:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
        !          10706:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
        !          10707:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
        !          10708:                                                }
        !          10709:                                        }
        !          10710:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
        !          10711:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
        !          10712:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
        !          10713:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
        !          10714:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
        !          10715:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
        !          10716:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
        !          10717:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
        !          10718:                                        
        !          10719:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
        !          10720:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
        !          10721:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
        !          10722:                                        REG16(AX) = 6; // number of bytes actually read
        !          10723:                                } else {
        !          10724:                                        REG16(AX) = 0x0d; // data invalid
        !          10725:                                        m_CF = 1;
        !          10726:                                }
        !          10727:                                break;
        !          10728:                        case 0x02:
        !          10729:                                if(REG16(CX) >= 2) {
        !          10730:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
        !          10731:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
        !          10732:                                        REG16(AX) = 2; // number of bytes actually read
        !          10733:                                } else {
        !          10734:                                        REG16(AX) = 0x0d; // data invalid
        !          10735:                                        m_CF = 1;
        !          10736:                                }
        !          10737:                                break;
        !          10738:                        case 0x03:
        !          10739:                                if(REG16(CX) >= 4) {
        !          10740:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
        !          10741:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
        !          10742:                                        REG16(AX) = 4; // number of bytes actually read
        !          10743:                                } else {
        !          10744:                                        REG16(AX) = 0x0d; // data invalid
        !          10745:                                        m_CF = 1;
        !          10746:                                }
        !          10747:                                break;
        !          10748:                        default:
        !          10749:                                REG16(AX) = 0x01; // function number invalid
        !          10750:                                m_CF = 1;
        !          10751:                        }
        !          10752:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
        !          10753:                        if(REG16(CX) >= 5) {
        !          10754:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
        !          10755:                                REG16(AX) = 5; // number of bytes actually read
        !          10756:                        } else {
        !          10757:                                REG16(AX) = 0x0d; // data invalid
        !          10758:                                m_CF = 1;
        !          10759:                        }
        !          10760:                } else {
        !          10761: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
        !          10762: //                     REG16(AX) = REG16(CX);
        !          10763:                        REG16(AX) = 0x05; // access denied
        !          10764:                        m_CF = 1;
        !          10765:                }
        !          10766:                break;
1.1       root     10767:        case 0x03: // send to character device
1.1.1.45! root     10768: //             REG16(AX) = 0x05;
        !          10769: //             m_CF = 1;
        !          10770:                REG16(AX) = 0x00; // success
        !          10771:                break;
1.1       root     10772:        case 0x04: // recv from block device
                   10773:        case 0x05: // send to block device
                   10774:                REG16(AX) = 0x05;
1.1.1.3   root     10775:                m_CF = 1;
1.1       root     10776:                break;
                   10777:        case 0x06: // get read status
1.1.1.20  root     10778:                if(file_mode[file_handler[fd].mode].in) {
                   10779:                        if(file_handler[fd].atty) {
1.1.1.14  root     10780:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     10781:                        } else {
1.1.1.20  root     10782:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     10783:                        }
1.1.1.14  root     10784:                } else {
                   10785:                        REG8(AL) = 0x00;
1.1       root     10786:                }
                   10787:                break;
                   10788:        case 0x07: // get write status
1.1.1.20  root     10789:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     10790:                        REG8(AL) = 0xff;
                   10791:                } else {
                   10792:                        REG8(AL) = 0x00;
1.1       root     10793:                }
                   10794:                break;
                   10795:        case 0x08: // check removable drive
1.1.1.44  root     10796:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     10797:                        // removable drive
                   10798:                        REG16(AX) = 0x00;
1.1       root     10799:                } else {
1.1.1.14  root     10800:                        // fixed drive
                   10801:                        REG16(AX) = 0x01;
1.1       root     10802:                }
                   10803:                break;
                   10804:        case 0x09: // check remote drive
1.1.1.44  root     10805:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     10806:                        // remote drive
                   10807:                        REG16(DX) = 0x1000;
1.1.1.44  root     10808:                } else if(msdos_is_subst_drive(drv)) {
                   10809:                        // subst drive
                   10810:                        REG16(DX) = 0x8000;
1.1       root     10811:                } else {
1.1.1.14  root     10812:                        // local drive
1.1.1.44  root     10813:                        REG16(DX) = 0x0000;
1.1       root     10814:                }
                   10815:                break;
1.1.1.21  root     10816:        case 0x0a: // check remote handle
1.1.1.45! root     10817:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
        !          10818:                        REG16(DX) = 0x8000;
        !          10819:                } else {
        !          10820:                        REG16(DX) = 0x0000;
        !          10821:                }
1.1.1.21  root     10822:                break;
1.1       root     10823:        case 0x0b: // set retry count
                   10824:                break;
1.1.1.22  root     10825:        case 0x0c: // generic character device request
                   10826:                if(REG8(CL) == 0x45) {
                   10827:                        // set iteration (retry) count
                   10828:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   10829:                } else if(REG8(CL) == 0x4a) {
                   10830:                        // select code page
                   10831:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   10832:                        msdos_nls_tables_update();
1.1.1.44  root     10833:                } else if(REG8(CL) == 0x4c) {
                   10834:                        // start code-page preparation
                   10835:                        int ids[3] = {437, 0, 0}; // 437: US English
                   10836:                        int count = 1, offset = 0;
                   10837:                        if(active_code_page != 437) {
                   10838:                                ids[count++] = active_code_page;
                   10839:                        }
                   10840:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   10841:                                ids[count++] = system_code_page;
                   10842:                        }
                   10843:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   10844:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   10845:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   10846:                        for(int i = 0; i < count; i++) {
                   10847:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   10848:                        }
                   10849:                } else if(REG8(CL) == 0x4d) {
                   10850:                        // end code-page preparation
                   10851:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   10852:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.22  root     10853:                } else if(REG8(CL) == 0x65) {
                   10854:                        // get iteration (retry) count
                   10855:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   10856:                } else if(REG8(CL) == 0x6a) {
                   10857:                        // query selected code page
                   10858:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   10859:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   10860:                        
                   10861:                        CPINFO info;
                   10862:                        GetCPInfo(active_code_page, &info);
                   10863:                        
                   10864:                        if(info.MaxCharSize != 1) {
                   10865:                                for(int i = 0;; i++) {
                   10866:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   10867:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   10868:                                        
                   10869:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   10870:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   10871:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   10872:                                        
                   10873:                                        if(lo == 0 && hi == 0) {
                   10874:                                                break;
                   10875:                                        }
                   10876:                                }
                   10877:                        }
1.1.1.44  root     10878:                } else if(REG8(CL) == 0x6b) {
                   10879:                        // query prepare list
                   10880:                        int ids[3] = {437, 0, 0}; // 437: US English
                   10881:                        int count = 1, offset = 0;
                   10882:                        if(active_code_page != 437) {
                   10883:                                ids[count++] = active_code_page;
                   10884:                        }
                   10885:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   10886:                                ids[count++] = system_code_page;
                   10887:                        }
                   10888:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   10889:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   10890:                        for(int i = 0; i < count; i++) {
                   10891:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   10892:                        }
                   10893:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   10894:                        for(int i = 0; i < count; i++) {
                   10895:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   10896:                        }
1.1.1.22  root     10897:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     10898:                        // get display information
1.1.1.22  root     10899:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;
                   10900:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;
                   10901:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;
                   10902:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;
                   10903:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;
                   10904:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;
                   10905:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;
                   10906:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a));
                   10907:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);
                   10908:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a);
                   10909:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;
                   10910:                } else {
                   10911:                        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));
                   10912:                        REG16(AX) = 0x01; // invalid function
                   10913:                        m_CF = 1;
                   10914:                }
                   10915:                break;
                   10916:        case 0x0d: // generic block device request
                   10917:                if(REG8(CL) == 0x40) {
                   10918:                        // set device parameters
                   10919:                } else if(REG8(CL) == 0x46) {
                   10920:                        // set volume serial number
                   10921:                } else if(REG8(CL) == 0x4a) {
                   10922:                        // lock logical volume
                   10923:                } else if(REG8(CL) == 0x4b) {
                   10924:                        // lock physical volume
                   10925:                } else if(REG8(CL) == 0x60) {
                   10926:                        // get device parameters
1.1.1.42  root     10927:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     10928:                        
1.1.1.42  root     10929:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   10930:                                drive_param_t *drive_param = &drive_params[drive_num];
                   10931:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   10932:                                
                   10933:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   10934:                                switch(geo->MediaType) {
                   10935:                                case F5_360_512:
                   10936:                                case F5_320_512:
                   10937:                                case F5_320_1024:
                   10938:                                case F5_180_512:
                   10939:                                case F5_160_512:
                   10940:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   10941:                                        break;
                   10942:                                case F5_1Pt2_512:
                   10943:                                case F3_1Pt2_512:
                   10944:                                case F3_1Pt23_1024:
                   10945:                                case F5_1Pt23_1024:
                   10946:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   10947:                                        break;
                   10948:                                case F3_720_512:
                   10949:                                case F3_640_512:
                   10950:                                case F5_640_512:
                   10951:                                case F5_720_512:
                   10952:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   10953:                                        break;
                   10954:                                case F8_256_128:
                   10955:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   10956:                                        break;
                   10957:                                case FixedMedia:
                   10958:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   10959:                                        break;
                   10960:                                case F3_1Pt44_512:
                   10961:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   10962:                                        break;
                   10963:                                case F3_2Pt88_512:
                   10964:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   10965:                                        break;
                   10966:                                default:
                   10967:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   10968: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   10969:                                        break;
1.1.1.22  root     10970:                                }
1.1.1.42  root     10971:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   10972:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   10973:                                switch(geo->MediaType) {
                   10974:                                case F5_360_512:
                   10975:                                case F5_320_512:
                   10976:                                case F5_320_1024:
                   10977:                                case F5_180_512:
                   10978:                                case F5_160_512:
                   10979:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   10980:                                        break;
                   10981:                                default:
                   10982:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   10983:                                        break;
                   10984:                                }
                   10985:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   10986:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   10987:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   10988:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   10989:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   10990:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   10991:                                switch(geo->MediaType) {
                   10992:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   10993:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   10994:                                        break;
                   10995:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   10996:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   10997:                                        break;
                   10998:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   10999:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11000:                                        break;
                   11001:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11002:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11003:                                        break;
                   11004:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11005:                                case F3_1Pt2_512:
                   11006:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11007:                                case F5_720_512:
                   11008:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11009:                                        break;
                   11010:                                case FixedMedia:        // hard disk
                   11011:                                case RemovableMedia:
                   11012:                                case Unknown:
                   11013:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11014:                                        break;
                   11015:                                default:
                   11016:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11017:                                        break;
                   11018:                                }
                   11019:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11020:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11021:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11022:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11023:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11024:                                // 21h  BYTE    device type
                   11025:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11026:                        } else {
                   11027:                                REG16(AX) = 0x0f; // invalid drive
                   11028:                                m_CF = 1;
                   11029:                        }
                   11030:                } else if(REG8(CL) == 0x66) {
                   11031:                        // get volume serial number
                   11032:                        char path[] = "A:\\";
                   11033:                        char volume_label[MAX_PATH];
                   11034:                        DWORD serial_number = 0;
                   11035:                        char file_system[MAX_PATH];
                   11036:                        
                   11037:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11038:                        
                   11039:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   11040:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11041:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11042:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11043:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11044:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11045:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11046:                        } else {
                   11047:                                REG16(AX) = 0x0f; // invalid drive
                   11048:                                m_CF = 1;
                   11049:                        }
                   11050:                } else if(REG8(CL) == 0x67) {
                   11051:                        // get access flag
                   11052:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11053:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11054:                } else if(REG8(CL) == 0x68) {
                   11055:                        // sense media type
1.1.1.42  root     11056:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11057:                        
1.1.1.42  root     11058:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11059:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11060:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11061:                                
                   11062:                                switch(geo->MediaType) {
                   11063:                                case F3_720_512:
                   11064:                                case F5_720_512:
                   11065:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11066:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   11067:                                        break;
                   11068:                                case F3_1Pt44_512:
                   11069:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11070:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   11071:                                        break;
                   11072:                                case F3_2Pt88_512:
                   11073:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11074:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   11075:                                        break;
                   11076:                                default:
                   11077:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   11078:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   11079:                                        break;
1.1.1.22  root     11080:                                }
                   11081:                        } else {
                   11082:                                REG16(AX) = 0x0f; // invalid drive
                   11083:                                m_CF = 1;
                   11084:                        }
                   11085:                } else if(REG8(CL) == 0x6a) {
                   11086:                        // unlock logical volume
                   11087:                } else if(REG8(CL) == 0x6b) {
                   11088:                        // unlock physical volume
                   11089:                } else {
                   11090:                        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));
                   11091:                        REG16(AX) = 0x01; // invalid function
                   11092:                        m_CF = 1;
                   11093:                }
                   11094:                break;
                   11095:        case 0x0e: // get logical drive map
1.1.1.44  root     11096:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11097:                        REG16(AX) = 0x0f; // invalid drive
                   11098:                        m_CF = 1;
                   11099:                } else {
                   11100:                        REG8(AL) = 0;
1.1.1.22  root     11101:                }
                   11102:                break;
                   11103:        case 0x0f: // set logical drive map
1.1.1.44  root     11104:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11105:                        REG16(AX) = 0x0f; // invalid drive
                   11106:                        m_CF = 1;
1.1.1.22  root     11107:                }
                   11108:                break;
                   11109:        case 0x10: // query generic ioctrl capability (handle)
                   11110:                switch(REG8(CL)) {
                   11111:                case 0x45:
                   11112:                case 0x4a:
                   11113:                case 0x65:
                   11114:                case 0x6a:
                   11115:                case 0x7f:
                   11116:                        REG16(AX) = 0x0000; // supported
                   11117:                        break;
                   11118:                default:
                   11119:                        REG8(AL) = 0x01; // ioctl capability not available
                   11120:                        m_CF = 1;
                   11121:                        break;
                   11122:                }
                   11123:                break;
                   11124:        case 0x11: // query generic ioctrl capability (drive)
                   11125:                switch(REG8(CL)) {
                   11126:                case 0x40:
                   11127:                case 0x46:
                   11128:                case 0x4a:
                   11129:                case 0x4b:
                   11130:                case 0x60:
                   11131:                case 0x66:
                   11132:                case 0x67:
                   11133:                case 0x68:
                   11134:                case 0x6a:
                   11135:                case 0x6b:
                   11136:                        REG16(AX) = 0x0000; // supported
                   11137:                        break;
                   11138:                default:
                   11139:                        REG8(AL) = 0x01; // ioctl capability not available
                   11140:                        m_CF = 1;
                   11141:                        break;
                   11142:                }
                   11143:                break;
                   11144:        case 0x12: // determine dos type
                   11145:        case 0x51: // concurrent dos v3.2+ - installation check
                   11146:        case 0x52: // determine dos type/get dr dos versuin
                   11147:                REG16(AX) = 0x01; // this  is not DR-DOS
                   11148:                m_CF = 1;
                   11149:                break;
1.1       root     11150:        default:
1.1.1.22  root     11151:                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     11152:                REG16(AX) = 0x01;
1.1.1.3   root     11153:                m_CF = 1;
1.1       root     11154:                break;
                   11155:        }
                   11156: }
                   11157: 
                   11158: inline void msdos_int_21h_45h()
                   11159: {
                   11160:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11161:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11162:        
1.1.1.20  root     11163:        if(fd < process->max_files && file_handler[fd].valid) {
                   11164:                int dup_fd = _dup(fd);
                   11165:                if(dup_fd != -1) {
                   11166:                        REG16(AX) = dup_fd;
                   11167:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11168: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11169:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11170:                } else {
                   11171:                        REG16(AX) = errno;
1.1.1.3   root     11172:                        m_CF = 1;
1.1       root     11173:                }
                   11174:        } else {
                   11175:                REG16(AX) = 0x06;
1.1.1.3   root     11176:                m_CF = 1;
1.1       root     11177:        }
                   11178: }
                   11179: 
                   11180: inline void msdos_int_21h_46h()
                   11181: {
                   11182:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11183:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11184:        int dup_fd = REG16(CX);
                   11185:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     11186:        
1.1.1.20  root     11187:        if(REG16(BX) == REG16(CX)) {
                   11188:                REG16(AX) = 0x06;
                   11189:                m_CF = 1;
                   11190:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   11191:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   11192:                        _close(tmp_fd);
                   11193:                        msdos_file_handler_close(tmp_fd);
                   11194:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   11195:                }
                   11196:                if(_dup2(fd, dup_fd) != -1) {
                   11197:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11198: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11199:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11200:                } else {
                   11201:                        REG16(AX) = errno;
1.1.1.3   root     11202:                        m_CF = 1;
1.1       root     11203:                }
                   11204:        } else {
                   11205:                REG16(AX) = 0x06;
1.1.1.3   root     11206:                m_CF = 1;
1.1       root     11207:        }
                   11208: }
                   11209: 
                   11210: inline void msdos_int_21h_47h(int lfn)
                   11211: {
                   11212:        char path[MAX_PATH];
                   11213:        
                   11214:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45! root     11215:                if(!lfn) {
        !          11216:                        strcpy(path, msdos_short_path(path));
        !          11217:                }
1.1       root     11218:                if(path[1] == ':') {
                   11219:                        // the returned path does not include a drive or the initial backslash
1.1.1.45! root     11220:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     11221:                } else {
1.1.1.45! root     11222:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     11223:                }
                   11224:        } else {
                   11225:                REG16(AX) = errno;
1.1.1.3   root     11226:                m_CF = 1;
1.1       root     11227:        }
                   11228: }
                   11229: 
                   11230: inline void msdos_int_21h_48h()
                   11231: {
1.1.1.19  root     11232:        int seg, umb_linked;
1.1       root     11233:        
1.1.1.8   root     11234:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     11235:                // unlink umb not to allocate memory in umb
                   11236:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   11237:                        msdos_mem_unlink_umb();
                   11238:                }
1.1.1.8   root     11239:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11240:                        REG16(AX) = seg;
                   11241:                } else {
                   11242:                        REG16(AX) = 0x08;
                   11243:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   11244:                        m_CF = 1;
                   11245:                }
1.1.1.19  root     11246:                if(umb_linked != 0) {
                   11247:                        msdos_mem_link_umb();
                   11248:                }
1.1.1.8   root     11249:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   11250:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11251:                        REG16(AX) = seg;
                   11252:                } else {
                   11253:                        REG16(AX) = 0x08;
                   11254:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   11255:                        m_CF = 1;
                   11256:                }
                   11257:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   11258:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11259:                        REG16(AX) = seg;
                   11260:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11261:                        REG16(AX) = seg;
                   11262:                } else {
                   11263:                        REG16(AX) = 0x08;
                   11264:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   11265:                        m_CF = 1;
                   11266:                }
1.1       root     11267:        }
                   11268: }
                   11269: 
                   11270: inline void msdos_int_21h_49h()
                   11271: {
1.1.1.14  root     11272:        int mcb_seg = SREG(ES) - 1;
                   11273:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   11274:        
                   11275:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11276:                msdos_mem_free(SREG(ES));
                   11277:        } else {
1.1.1.33  root     11278:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     11279:                m_CF = 1;
                   11280:        }
1.1       root     11281: }
                   11282: 
                   11283: inline void msdos_int_21h_4ah()
                   11284: {
1.1.1.14  root     11285:        int mcb_seg = SREG(ES) - 1;
                   11286:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     11287:        int max_paragraphs;
                   11288:        
1.1.1.14  root     11289:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11290:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   11291:                        REG16(AX) = 0x08;
                   11292:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   11293:                        m_CF = 1;
                   11294:                }
                   11295:        } else {
1.1.1.33  root     11296:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     11297:                m_CF = 1;
1.1       root     11298:        }
                   11299: }
                   11300: 
                   11301: inline void msdos_int_21h_4bh()
                   11302: {
1.1.1.3   root     11303:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11304:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     11305:        
                   11306:        switch(REG8(AL)) {
                   11307:        case 0x00:
                   11308:        case 0x01:
                   11309:                if(msdos_process_exec(command, param, REG8(AL))) {
                   11310:                        REG16(AX) = 0x02;
1.1.1.3   root     11311:                        m_CF = 1;
1.1       root     11312:                }
                   11313:                break;
1.1.1.14  root     11314:        case 0x03:
                   11315:                {
                   11316:                        int fd;
                   11317:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   11318:                                REG16(AX) = 0x02;
                   11319:                                m_CF = 1;
                   11320:                                break;
                   11321:                        }
                   11322:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   11323:                        _close(fd);
                   11324:                        
                   11325:                        UINT16 *overlay = (UINT16 *)param;
                   11326:                        
                   11327:                        // check exe header
                   11328:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   11329:                        int header_size = 0;
                   11330:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   11331:                                header_size = header->header_size * 16;
                   11332:                                // relocation
                   11333:                                int start_seg = overlay[1];
                   11334:                                for(int i = 0; i < header->relocations; i++) {
                   11335:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   11336:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   11337:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   11338:                                }
                   11339:                        }
                   11340:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   11341:                }
                   11342:                break;
1.1.1.43  root     11343:        case 0xfd:
                   11344:        case 0xfe:
                   11345:                // unknown function called in FreeCOM
                   11346:                REG16(AX) = 0x01;
                   11347:                m_CF = 1;
                   11348:                break;
1.1       root     11349:        default:
1.1.1.22  root     11350:                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     11351:                REG16(AX) = 0x01;
1.1.1.3   root     11352:                m_CF = 1;
1.1       root     11353:                break;
                   11354:        }
                   11355: }
                   11356: 
                   11357: inline void msdos_int_21h_4ch()
                   11358: {
                   11359:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   11360: }
                   11361: 
                   11362: inline void msdos_int_21h_4dh()
                   11363: {
                   11364:        REG16(AX) = retval;
                   11365: }
                   11366: 
                   11367: inline void msdos_int_21h_4eh()
                   11368: {
                   11369:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11370:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11371:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45! root     11372:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11373:        WIN32_FIND_DATA fd;
                   11374:        
1.1.1.14  root     11375:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   11376:        find->find_magic = FIND_MAGIC;
                   11377:        find->dta_index = dtainfo - dtalist;
1.1       root     11378:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     11379:        dtainfo->allowable_mask = REG8(CL);
                   11380:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     11381:        
1.1.1.14  root     11382:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   11383:                dtainfo->allowable_mask &= ~8;
1.1       root     11384:        }
1.1.1.14  root     11385:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   11386:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11387:                      !msdos_find_file_has_8dot3name(&fd)) {
                   11388:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11389:                                FindClose(dtainfo->find_handle);
                   11390:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11391:                                break;
                   11392:                        }
                   11393:                }
                   11394:        }
1.1.1.13  root     11395:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11396:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11397:                msdos_find_file_conv_local_time(&fd);
                   11398:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11399:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11400:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11401:                REG16(AX) = 0;
1.1.1.14  root     11402:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11403:                find->attrib = 8;
                   11404:                find->size = 0;
                   11405:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11406:                dtainfo->allowable_mask &= ~8;
1.1       root     11407:                REG16(AX) = 0;
                   11408:        } else {
                   11409:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     11410:                m_CF = 1;
1.1       root     11411:        }
                   11412: }
                   11413: 
                   11414: inline void msdos_int_21h_4fh()
                   11415: {
                   11416:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11417:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11418:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     11419:        WIN32_FIND_DATA fd;
                   11420:        
1.1.1.14  root     11421:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   11422:                REG16(AX) = 0x12;
                   11423:                m_CF = 1;
                   11424:                return;
                   11425:        }
                   11426:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     11427:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11428:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     11429:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11430:                              !msdos_find_file_has_8dot3name(&fd)) {
                   11431:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11432:                                        FindClose(dtainfo->find_handle);
                   11433:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11434:                                        break;
                   11435:                                }
                   11436:                        }
                   11437:                } else {
1.1.1.13  root     11438:                        FindClose(dtainfo->find_handle);
                   11439:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11440:                }
                   11441:        }
1.1.1.13  root     11442:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11443:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11444:                msdos_find_file_conv_local_time(&fd);
                   11445:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11446:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11447:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11448:                REG16(AX) = 0;
1.1.1.14  root     11449:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11450:                find->attrib = 8;
                   11451:                find->size = 0;
                   11452:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11453:                dtainfo->allowable_mask &= ~8;
1.1       root     11454:                REG16(AX) = 0;
                   11455:        } else {
                   11456:                REG16(AX) = 0x12;
1.1.1.3   root     11457:                m_CF = 1;
1.1       root     11458:        }
                   11459: }
                   11460: 
                   11461: inline void msdos_int_21h_50h()
                   11462: {
1.1.1.8   root     11463:        if(current_psp != REG16(BX)) {
                   11464:                process_t *process = msdos_process_info_get(current_psp);
                   11465:                if(process != NULL) {
                   11466:                        process->psp = REG16(BX);
                   11467:                }
                   11468:                current_psp = REG16(BX);
1.1.1.23  root     11469:                msdos_sda_update(current_psp);
1.1.1.8   root     11470:        }
1.1       root     11471: }
                   11472: 
                   11473: inline void msdos_int_21h_51h()
                   11474: {
                   11475:        REG16(BX) = current_psp;
                   11476: }
                   11477: 
                   11478: inline void msdos_int_21h_52h()
                   11479: {
1.1.1.25  root     11480:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     11481:        i386_load_segment_descriptor(ES);
1.1.1.25  root     11482:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     11483: }
                   11484: 
1.1.1.43  root     11485: inline void msdos_int_21h_53h()
                   11486: {
                   11487:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   11488:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   11489:        
                   11490:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   11491:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   11492:        dpb->shift_count = 0;
                   11493:        dpb->reserved_sectors = 0;
                   11494:        dpb->fat_num = bpb->fat_num;
                   11495:        dpb->root_entries = bpb->root_entries;
                   11496:        dpb->first_data_sector = 0;
                   11497:        if(bpb->sectors_per_cluster != 0) {
                   11498:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   11499:        } else {
                   11500:                dpb->highest_cluster_num = 0;
                   11501:        }
                   11502:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   11503:        dpb->first_dir_sector = 0;
                   11504:        dpb->device_driver_header = 0;
                   11505:        dpb->media_type = bpb->media_type;
                   11506:        dpb->drive_accessed = 0;
                   11507:        dpb->next_dpb_ofs = 0xffff;
                   11508:        dpb->next_dpb_seg = 0xffff;
                   11509:        dpb->first_free_cluster = 0;
                   11510:        dpb->free_clusters = 0xffff;
                   11511: }
                   11512: 
1.1       root     11513: inline void msdos_int_21h_54h()
                   11514: {
                   11515:        process_t *process = msdos_process_info_get(current_psp);
                   11516:        
                   11517:        REG8(AL) = process->verify;
                   11518: }
                   11519: 
                   11520: inline void msdos_int_21h_55h()
                   11521: {
                   11522:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   11523:        
                   11524:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   11525:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   11526:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   11527:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   11528:        psp->parent_psp = current_psp;
                   11529: }
                   11530: 
                   11531: inline void msdos_int_21h_56h(int lfn)
                   11532: {
                   11533:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     11534:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   11535:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     11536:        
                   11537:        if(rename(src, dst)) {
                   11538:                REG16(AX) = errno;
1.1.1.3   root     11539:                m_CF = 1;
1.1       root     11540:        }
                   11541: }
                   11542: 
                   11543: inline void msdos_int_21h_57h()
                   11544: {
                   11545:        FILETIME time, local;
1.1.1.14  root     11546:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     11547:        HANDLE hHandle;
1.1       root     11548:        
1.1.1.21  root     11549:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     11550:                REG16(AX) = (UINT16)GetLastError();
                   11551:                m_CF = 1;
                   11552:                return;
                   11553:        }
                   11554:        ctime = atime = mtime = NULL;
                   11555:        
1.1       root     11556:        switch(REG8(AL)) {
                   11557:        case 0x00:
1.1.1.6   root     11558:        case 0x01:
1.1.1.14  root     11559:                mtime = &time;
1.1.1.6   root     11560:                break;
                   11561:        case 0x04:
                   11562:        case 0x05:
1.1.1.14  root     11563:                atime = &time;
1.1       root     11564:                break;
1.1.1.6   root     11565:        case 0x06:
                   11566:        case 0x07:
1.1.1.14  root     11567:                ctime = &time;
                   11568:                break;
                   11569:        default:
1.1.1.22  root     11570:                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     11571:                REG16(AX) = 0x01;
                   11572:                m_CF = 1;
                   11573:                return;
                   11574:        }
                   11575:        if(REG8(AL) & 1) {
1.1       root     11576:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   11577:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     11578:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     11579:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11580:                        m_CF = 1;
1.1       root     11581:                }
1.1.1.14  root     11582:        } else {
1.1.1.21  root     11583:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     11584:                        // assume a device and use the current time
                   11585:                        GetSystemTimeAsFileTime(&time);
                   11586:                }
                   11587:                FileTimeToLocalFileTime(&time, &local);
                   11588:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     11589:        }
                   11590: }
                   11591: 
                   11592: inline void msdos_int_21h_58h()
                   11593: {
                   11594:        switch(REG8(AL)) {
                   11595:        case 0x00:
1.1.1.7   root     11596:                REG16(AX) = malloc_strategy;
                   11597:                break;
                   11598:        case 0x01:
1.1.1.24  root     11599: //             switch(REG16(BX)) {
                   11600:                switch(REG8(BL)) {
1.1.1.7   root     11601:                case 0x0000:
                   11602:                case 0x0001:
                   11603:                case 0x0002:
                   11604:                case 0x0040:
                   11605:                case 0x0041:
                   11606:                case 0x0042:
                   11607:                case 0x0080:
                   11608:                case 0x0081:
                   11609:                case 0x0082:
                   11610:                        malloc_strategy = REG16(BX);
1.1.1.23  root     11611:                        msdos_sda_update(current_psp);
1.1.1.7   root     11612:                        break;
                   11613:                default:
1.1.1.22  root     11614:                        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     11615:                        REG16(AX) = 0x01;
                   11616:                        m_CF = 1;
                   11617:                        break;
                   11618:                }
                   11619:                break;
                   11620:        case 0x02:
1.1.1.19  root     11621:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     11622:                break;
                   11623:        case 0x03:
1.1.1.24  root     11624: //             switch(REG16(BX)) {
                   11625:                switch(REG8(BL)) {
1.1.1.7   root     11626:                case 0x0000:
1.1.1.19  root     11627:                        msdos_mem_unlink_umb();
                   11628:                        break;
1.1.1.7   root     11629:                case 0x0001:
1.1.1.19  root     11630:                        msdos_mem_link_umb();
1.1.1.7   root     11631:                        break;
                   11632:                default:
1.1.1.22  root     11633:                        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     11634:                        REG16(AX) = 0x01;
                   11635:                        m_CF = 1;
                   11636:                        break;
                   11637:                }
1.1       root     11638:                break;
                   11639:        default:
1.1.1.22  root     11640:                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     11641:                REG16(AX) = 0x01;
1.1.1.3   root     11642:                m_CF = 1;
1.1       root     11643:                break;
                   11644:        }
                   11645: }
                   11646: 
                   11647: inline void msdos_int_21h_59h()
                   11648: {
1.1.1.23  root     11649:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   11650:        
                   11651:        REG16(AX) = sda->extended_error_code;
1.1.1.43  root     11652:        REG8(BL)  = sda->suggested_action;
                   11653:        REG8(BH)  = sda->error_class;
                   11654:        REG8(CL)  = sda->byte_reserved_1;
                   11655:        REG8(CH)  = sda->locus_of_last_error;
                   11656:        REG16(DX) = sda->word_reserved_1;
                   11657:        REG16(SI) = sda->word_reserved_2;
                   11658:        REG16(DI) = sda->last_error_pointer.w.l;
                   11659:        SREG(DS)  = sda->word_reserved_3;
                   11660:        i386_load_segment_descriptor(DS);
                   11661:        SREG(ES)  = sda->last_error_pointer.w.h;
                   11662:        i386_load_segment_descriptor(ES);
1.1       root     11663: }
                   11664: 
                   11665: inline void msdos_int_21h_5ah()
                   11666: {
1.1.1.3   root     11667:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11668:        int len = strlen(path);
                   11669:        char tmp[MAX_PATH];
                   11670:        
                   11671:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   11672:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11673:                
                   11674:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11675:                REG16(AX) = fd;
                   11676:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11677:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11678:                
                   11679:                strcpy(path, tmp);
                   11680:                int dx = REG16(DX) + len;
1.1.1.3   root     11681:                int ds = SREG(DS);
1.1       root     11682:                while(dx > 0xffff) {
                   11683:                        dx -= 0x10;
                   11684:                        ds++;
                   11685:                }
                   11686:                REG16(DX) = dx;
1.1.1.3   root     11687:                SREG(DS) = ds;
                   11688:                i386_load_segment_descriptor(DS);
1.1       root     11689:        } else {
                   11690:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11691:                m_CF = 1;
1.1       root     11692:        }
                   11693: }
                   11694: 
                   11695: inline void msdos_int_21h_5bh()
                   11696: {
1.1.1.45! root     11697:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11698:        
1.1.1.45! root     11699: //     if(msdos_is_existing_file(path)) {
        !          11700:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     11701:                // already exists
                   11702:                REG16(AX) = 0x50;
1.1.1.3   root     11703:                m_CF = 1;
1.1       root     11704:        } else {
                   11705:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11706:                
                   11707:                if(fd != -1) {
                   11708:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11709:                        REG16(AX) = fd;
                   11710:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11711:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11712:                } else {
                   11713:                        REG16(AX) = errno;
1.1.1.3   root     11714:                        m_CF = 1;
1.1       root     11715:                }
                   11716:        }
                   11717: }
                   11718: 
                   11719: inline void msdos_int_21h_5ch()
                   11720: {
                   11721:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11722:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11723:        
1.1.1.20  root     11724:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     11725:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     11726:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     11727:                        UINT32 pos = _tell(fd);
                   11728:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   11729:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     11730:                                REG16(AX) = errno;
1.1.1.3   root     11731:                                m_CF = 1;
1.1       root     11732:                        }
1.1.1.20  root     11733:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     11734:                        
1.1       root     11735:                        // some seconds may be passed in _locking()
1.1.1.35  root     11736:                        REQUEST_HARDWRE_UPDATE();
1.1       root     11737:                } else {
                   11738:                        REG16(AX) = 0x01;
1.1.1.3   root     11739:                        m_CF = 1;
1.1       root     11740:                }
                   11741:        } else {
                   11742:                REG16(AX) = 0x06;
1.1.1.3   root     11743:                m_CF = 1;
1.1       root     11744:        }
                   11745: }
                   11746: 
1.1.1.22  root     11747: inline void msdos_int_21h_5dh()
                   11748: {
                   11749:        switch(REG8(AL)) {
1.1.1.45! root     11750:        case 0x00:
        !          11751:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
        !          11752:                        // current system
        !          11753:                        static bool reenter = false;
        !          11754:                        if(!reenter) {
        !          11755:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
        !          11756:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
        !          11757:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
        !          11758:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
        !          11759:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
        !          11760:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
        !          11761:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
        !          11762:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
        !          11763:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
        !          11764:                                i386_load_segment_descriptor(DS);
        !          11765:                                i386_load_segment_descriptor(ES);
        !          11766:                                reenter = true;
        !          11767:                                try {
        !          11768:                                        msdos_syscall(0x21);
        !          11769:                                } catch(...) {
        !          11770:                                }
        !          11771:                                reenter = false;
        !          11772:                        }
        !          11773:                } else {
        !          11774:                        REG16(AX) = 0x49; //  network software not installed
        !          11775:                        m_CF = 1;
        !          11776:                }
        !          11777:                break;
1.1.1.22  root     11778:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     11779:                SREG(DS) = (SDA_TOP >> 4);
                   11780:                i386_load_segment_descriptor(DS);
                   11781:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   11782:                REG16(CX) = 0x80;
                   11783:                REG16(DX) = 0x1a;
                   11784:                break;
1.1.1.45! root     11785:        case 0x07: // get redirected printer mode
        !          11786:        case 0x08: // set redirected printer mode
        !          11787:        case 0x09: // flush redirected printer output
        !          11788:                REG16(AX) = 0x49; //  network software not installed
        !          11789:                m_CF = 1;
        !          11790:                break;
1.1.1.43  root     11791:        case 0x0a: // set extended error information
                   11792:                {
                   11793:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   11794:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
                   11795:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   11796:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
                   11797:                        sda->byte_reserved_1        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
                   11798:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
                   11799:                        sda->word_reserved_1        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   11800:                        sda->word_reserved_2        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
                   11801:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
                   11802:                        sda->word_reserved_3        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
                   11803:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   11804:                }
                   11805:                break;
1.1.1.23  root     11806:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     11807:                REG16(AX) = 0x01;
                   11808:                m_CF = 1;
                   11809:                break;
                   11810:        default:
                   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));
                   11812:                REG16(AX) = 0x01;
                   11813:                m_CF = 1;
                   11814:                break;
                   11815:        }
                   11816: }
                   11817: 
1.1.1.42  root     11818: inline void msdos_int_21h_5eh()
                   11819: {
                   11820:        switch(REG8(AL)) {
                   11821:        case 0x00:
                   11822:                {
                   11823:                        char name[256] = {0};
                   11824:                        DWORD dwSize = 256;
                   11825:                        
                   11826:                        if(GetComputerName(name, &dwSize)) {
                   11827:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11828:                                for(int i = 0; i < 15; i++) {
                   11829:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   11830:                                }
                   11831:                                dest[15] = '\0';
                   11832:                                REG8(CH) = 0x01; // nonzero valid
                   11833:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   11834:                        } else {
                   11835:                                REG16(AX) = 0x01;
                   11836:                                m_CF = 1;
                   11837:                        }
                   11838:                }
                   11839:                break;
                   11840:        default:
1.1.1.45! root     11841: //             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));
        !          11842: //             REG16(AX) = 0x01;
        !          11843:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     11844:                m_CF = 1;
                   11845:                break;
                   11846:        }
                   11847: }
                   11848: 
1.1.1.30  root     11849: inline void msdos_int_21h_5fh()
                   11850: {
                   11851:        switch(REG8(AL)) {
1.1.1.42  root     11852:        case 0x05:
1.1.1.44  root     11853:                REG16(BP) = 0;
                   11854:                for(int i = 0; i < 26; i++) {
                   11855:                        if(msdos_is_remote_drive(i)) {
                   11856:                                REG16(BP)++;
1.1.1.42  root     11857:                        }
                   11858:                }
1.1.1.30  root     11859:        case 0x02:
1.1.1.44  root     11860:                for(int i = 0, index = 0; i < 26; i++) {
                   11861:                        if(msdos_is_remote_drive(i)) {
                   11862:                                if(index == REG16(BX)) {
                   11863:                                        char volume[] = "A:";
1.1.1.30  root     11864:                                        volume[0] = 'A' + i;
1.1.1.44  root     11865:                                        DWORD dwSize = 128;
                   11866:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   11867:                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   11868:                                        REG8(BH) = 0x00; // valid
                   11869:                                        REG8(BL) = 0x04; // disk drive
                   11870:                                        REG16(CX) = 0x00; // LANtastic
                   11871:                                        return;
1.1.1.30  root     11872:                                }
1.1.1.44  root     11873:                                index++;
1.1.1.30  root     11874:                        }
                   11875:                }
                   11876:                REG16(AX) = 0x12; // no more files
                   11877:                m_CF = 1;
                   11878:                break;
1.1.1.44  root     11879:        case 0x07:
                   11880:                if(msdos_is_valid_drive(REG8(DL))) {
                   11881:                        msdos_cds_update(REG8(DL));
                   11882:                } else {
                   11883:                        REG16(AX) = 0x0f; // invalid drive
                   11884:                        m_CF = 1;
                   11885:                }
                   11886:                break;
                   11887:        case 0x08:
                   11888:                if(msdos_is_valid_drive(REG8(DL))) {
                   11889:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   11890:                        cds->drive_attrib = 0x0000;
                   11891:                } else {
                   11892:                        REG16(AX) = 0x0f; // invalid drive
                   11893:                        m_CF = 1;
                   11894:                }
                   11895:                break;
1.1.1.30  root     11896:        default:
1.1.1.45! root     11897: //             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));
        !          11898: //             REG16(AX) = 0x01;
        !          11899:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     11900:                m_CF = 1;
                   11901:                break;
                   11902:        }
                   11903: }
                   11904: 
1.1       root     11905: inline void msdos_int_21h_60h(int lfn)
                   11906: {
1.1.1.45! root     11907:        char full[MAX_PATH];
        !          11908:        const char *path = NULL;
1.1.1.14  root     11909:        
1.1       root     11910:        if(lfn) {
1.1.1.14  root     11911:                char *name;
                   11912:                *full = '\0';
1.1.1.3   root     11913:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     11914:                switch(REG8(CL)) {
                   11915:                case 1:
                   11916:                        GetShortPathName(full, full, MAX_PATH);
                   11917:                        my_strupr(full);
                   11918:                        break;
                   11919:                case 2:
                   11920:                        GetLongPathName(full, full, MAX_PATH);
                   11921:                        break;
                   11922:                }
                   11923:                path = full;
                   11924:        } else {
                   11925:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   11926:        }
                   11927:        if(*path != '\0') {
                   11928:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     11929:        } else {
1.1.1.14  root     11930:                REG16(AX) = (UINT16)GetLastError();
                   11931:                m_CF = 1;
1.1       root     11932:        }
                   11933: }
                   11934: 
                   11935: inline void msdos_int_21h_61h()
                   11936: {
                   11937:        REG8(AL) = 0;
                   11938: }
                   11939: 
                   11940: inline void msdos_int_21h_62h()
                   11941: {
                   11942:        REG16(BX) = current_psp;
                   11943: }
                   11944: 
                   11945: inline void msdos_int_21h_63h()
                   11946: {
                   11947:        switch(REG8(AL)) {
                   11948:        case 0x00:
1.1.1.3   root     11949:                SREG(DS) = (DBCS_TABLE >> 4);
                   11950:                i386_load_segment_descriptor(DS);
1.1       root     11951:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   11952:                REG8(AL) = 0x00;
                   11953:                break;
1.1.1.22  root     11954:        case 0x01: // set korean input mode
                   11955:        case 0x02: // get korean input mode
                   11956:                REG8(AL) = 0xff; // not supported
                   11957:                break;
1.1       root     11958:        default:
1.1.1.22  root     11959:                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     11960:                REG16(AX) = 0x01;
1.1.1.3   root     11961:                m_CF = 1;
1.1       root     11962:                break;
                   11963:        }
                   11964: }
                   11965: 
1.1.1.25  root     11966: UINT16 get_extended_country_info(UINT8 func)
1.1       root     11967: {
1.1.1.25  root     11968:        switch(func) {
1.1.1.17  root     11969:        case 0x01:
                   11970:                if(REG16(CX) >= 5) {
1.1.1.19  root     11971:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     11972:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   11973:                                REG16(CX) = sizeof(data);
                   11974:                        ZeroMemory(data, sizeof(data));
                   11975:                        data[0] = 0x01;
                   11976:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     11977:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     11978:                        *(UINT16 *)(data + 5) = active_code_page;
                   11979:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     11980: //                     REG16(AX) = active_code_page;
1.1.1.17  root     11981:                } else {
1.1.1.25  root     11982:                        return(0x08); // insufficient memory
1.1.1.17  root     11983:                }
                   11984:                break;
                   11985:        case 0x02:
                   11986:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   11987:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   11988:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     11989: //             REG16(AX) = active_code_page;
1.1.1.17  root     11990:                REG16(CX) = 0x05;
                   11991:                break;
1.1.1.23  root     11992:        case 0x03:
                   11993:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   11994:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   11995:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     11996: //             REG16(AX) = active_code_page;
1.1.1.23  root     11997:                REG16(CX) = 0x05;
                   11998:                break;
1.1.1.17  root     11999:        case 0x04:
                   12000:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   12001:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   12002:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     12003: //             REG16(AX) = active_code_page;
1.1.1.17  root     12004:                REG16(CX) = 0x05;
                   12005:                break;
                   12006:        case 0x05:
                   12007:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   12008:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   12009:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     12010: //             REG16(AX) = active_code_page;
1.1.1.17  root     12011:                REG16(CX) = 0x05;
                   12012:                break;
                   12013:        case 0x06:
                   12014:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   12015:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   12016:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     12017: //             REG16(AX) = active_code_page;
1.1.1.17  root     12018:                REG16(CX) = 0x05;
                   12019:                break;
1.1       root     12020:        case 0x07:
1.1.1.3   root     12021:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   12022:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   12023:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     12024: //             REG16(AX) = active_code_page;
1.1       root     12025:                REG16(CX) = 0x05;
                   12026:                break;
1.1.1.25  root     12027:        default:
                   12028:                return(0x01); // function number invalid
                   12029:        }
                   12030:        return(0x00);
                   12031: }
                   12032: 
                   12033: inline void msdos_int_21h_65h()
                   12034: {
                   12035:        char tmp[0x10000];
                   12036:        
                   12037:        switch(REG8(AL)) {
1.1.1.43  root     12038:        case 0x00:
                   12039:                if(REG16(CX) >= 7) {
                   12040:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   12041:                        REG16(AX) = system_code_page;
                   12042:                } else {
                   12043:                        REG16(AX) = 0x0c;
                   12044:                        m_CF = 1;
                   12045:                }
                   12046:                break;
1.1.1.25  root     12047:        case 0x01:
                   12048:        case 0x02:
                   12049:        case 0x03:
                   12050:        case 0x04:
                   12051:        case 0x05:
                   12052:        case 0x06:
                   12053:        case 0x07:
                   12054:                {
                   12055:                        UINT16 result = get_extended_country_info(REG8(AL));
                   12056:                        if(result) {
                   12057:                                REG16(AX) = result;
                   12058:                                m_CF = 1;
                   12059:                        } else {
                   12060:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   12061:                        }
                   12062:                }
                   12063:                break;
1.1       root     12064:        case 0x20:
1.1.1.25  root     12065:        case 0xa0:
1.1.1.19  root     12066:                memset(tmp, 0, sizeof(tmp));
                   12067:                tmp[0] = REG8(DL);
1.1       root     12068:                my_strupr(tmp);
                   12069:                REG8(DL) = tmp[0];
                   12070:                break;
                   12071:        case 0x21:
1.1.1.25  root     12072:        case 0xa1:
1.1       root     12073:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12074:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     12075:                my_strupr(tmp);
1.1.1.3   root     12076:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     12077:                break;
                   12078:        case 0x22:
1.1.1.25  root     12079:        case 0xa2:
1.1.1.3   root     12080:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     12081:                break;
1.1.1.25  root     12082:        case 0x23:
                   12083:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45! root     12084:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     12085:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45! root     12086:                        REG16(AX) = 0x00;
        !          12087:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
        !          12088:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     12089:                        REG16(AX) = 0x01;
                   12090:                } else {
                   12091:                        REG16(AX) = 0x02;
                   12092:                }
                   12093:                break;
1.1       root     12094:        default:
1.1.1.22  root     12095:                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     12096:                REG16(AX) = 0x01;
1.1.1.3   root     12097:                m_CF = 1;
1.1       root     12098:                break;
                   12099:        }
                   12100: }
                   12101: 
                   12102: inline void msdos_int_21h_66h()
                   12103: {
                   12104:        switch(REG8(AL)) {
                   12105:        case 0x01:
                   12106:                REG16(BX) = active_code_page;
                   12107:                REG16(DX) = system_code_page;
                   12108:                break;
                   12109:        case 0x02:
                   12110:                if(active_code_page == REG16(BX)) {
                   12111:                        REG16(AX) = 0xeb41;
                   12112:                } else if(_setmbcp(REG16(BX)) == 0) {
                   12113:                        active_code_page = REG16(BX);
1.1.1.17  root     12114:                        msdos_nls_tables_update();
1.1       root     12115:                        REG16(AX) = 0xeb41;
1.1.1.32  root     12116:                        SetConsoleCP(active_code_page);
                   12117:                        SetConsoleOutputCP(active_code_page);
1.1       root     12118:                } else {
                   12119:                        REG16(AX) = 0x25;
1.1.1.3   root     12120:                        m_CF = 1;
1.1       root     12121:                }
                   12122:                break;
                   12123:        default:
1.1.1.22  root     12124:                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     12125:                REG16(AX) = 0x01;
1.1.1.3   root     12126:                m_CF = 1;
1.1       root     12127:                break;
                   12128:        }
                   12129: }
                   12130: 
                   12131: inline void msdos_int_21h_67h()
                   12132: {
                   12133:        process_t *process = msdos_process_info_get(current_psp);
                   12134:        
                   12135:        if(REG16(BX) <= MAX_FILES) {
                   12136:                process->max_files = max(REG16(BX), 20);
                   12137:        } else {
                   12138:                REG16(AX) = 0x08;
1.1.1.3   root     12139:                m_CF = 1;
1.1       root     12140:        }
                   12141: }
                   12142: 
                   12143: inline void msdos_int_21h_68h()
                   12144: {
                   12145:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12146:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12147:        
1.1.1.20  root     12148:        if(fd < process->max_files && file_handler[fd].valid) {
                   12149:                // fflush(_fdopen(fd, ""));
1.1       root     12150:        } else {
                   12151:                REG16(AX) = 0x06;
1.1.1.3   root     12152:                m_CF = 1;
1.1       root     12153:        }
                   12154: }
                   12155: 
                   12156: inline void msdos_int_21h_69h()
                   12157: {
1.1.1.3   root     12158:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12159:        char path[] = "A:\\";
                   12160:        char volume_label[MAX_PATH];
                   12161:        DWORD serial_number = 0;
                   12162:        char file_system[MAX_PATH];
                   12163:        
                   12164:        if(REG8(BL) == 0) {
                   12165:                path[0] = 'A' + _getdrive() - 1;
                   12166:        } else {
                   12167:                path[0] = 'A' + REG8(BL) - 1;
                   12168:        }
                   12169:        
                   12170:        switch(REG8(AL)) {
                   12171:        case 0x00:
                   12172:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   12173:                        info->info_level = 0;
                   12174:                        info->serial_number = serial_number;
                   12175:                        memset(info->volume_label, 0x20, 11);
                   12176:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   12177:                        memset(info->file_system, 0x20, 8);
                   12178:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   12179:                } else {
                   12180:                        REG16(AX) = errno;
1.1.1.3   root     12181:                        m_CF = 1;
1.1       root     12182:                }
                   12183:                break;
                   12184:        case 0x01:
                   12185:                REG16(AX) = 0x03;
1.1.1.3   root     12186:                m_CF = 1;
1.1.1.45! root     12187:                break;
        !          12188:        default:
        !          12189:                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));
        !          12190:                REG16(AX) = 0x01;
        !          12191:                m_CF = 1;
        !          12192:                break;
1.1       root     12193:        }
                   12194: }
                   12195: 
                   12196: inline void msdos_int_21h_6ah()
                   12197: {
                   12198:        REG8(AH) = 0x68;
                   12199:        msdos_int_21h_68h();
                   12200: }
                   12201: 
                   12202: inline void msdos_int_21h_6bh()
                   12203: {
1.1.1.45! root     12204:        REG8(AL) = 0x00;
1.1       root     12205: }
                   12206: 
                   12207: inline void msdos_int_21h_6ch(int lfn)
                   12208: {
1.1.1.45! root     12209:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     12210:        int mode = REG8(BL) & 0x03;
                   12211:        
                   12212:        if(mode < 0x03) {
1.1.1.29  root     12213:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12214:                        // file exists
                   12215:                        if(REG8(DL) & 1) {
1.1.1.37  root     12216:                                int fd = -1;
                   12217:                                int sio_port = 0;
                   12218:                                int lpt_port = 0;
1.1       root     12219:                                
1.1.1.45! root     12220:                                if(msdos_is_device_path(path)) {
        !          12221:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     12222:                                } else {
1.1.1.13  root     12223:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     12224:                                }
1.1       root     12225:                                if(fd != -1) {
                   12226:                                        REG16(AX) = fd;
                   12227:                                        REG16(CX) = 1;
1.1.1.45! root     12228:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12229:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12230:                                } else {
                   12231:                                        REG16(AX) = errno;
1.1.1.3   root     12232:                                        m_CF = 1;
1.1       root     12233:                                }
                   12234:                        } else if(REG8(DL) & 2) {
                   12235:                                int attr = GetFileAttributes(path);
1.1.1.37  root     12236:                                int fd = -1;
                   12237:                                int sio_port = 0;
                   12238:                                int lpt_port = 0;
1.1       root     12239:                                
1.1.1.45! root     12240:                                if(msdos_is_device_path(path)) {
        !          12241:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     12242:                                } else {
                   12243:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12244:                                }
                   12245:                                if(fd != -1) {
                   12246:                                        if(attr == -1) {
                   12247:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   12248:                                        }
                   12249:                                        SetFileAttributes(path, attr);
                   12250:                                        REG16(AX) = fd;
                   12251:                                        REG16(CX) = 3;
1.1.1.45! root     12252:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12253:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12254:                                } else {
                   12255:                                        REG16(AX) = errno;
1.1.1.3   root     12256:                                        m_CF = 1;
1.1       root     12257:                                }
                   12258:                        } else {
                   12259:                                REG16(AX) = 0x50;
1.1.1.3   root     12260:                                m_CF = 1;
1.1       root     12261:                        }
                   12262:                } else {
                   12263:                        // file not exists
                   12264:                        if(REG8(DL) & 0x10) {
                   12265:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12266:                                
                   12267:                                if(fd != -1) {
                   12268:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12269:                                        REG16(AX) = fd;
                   12270:                                        REG16(CX) = 2;
                   12271:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12272:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12273:                                } else {
                   12274:                                        REG16(AX) = errno;
1.1.1.3   root     12275:                                        m_CF = 1;
1.1       root     12276:                                }
                   12277:                        } else {
                   12278:                                REG16(AX) = 0x02;
1.1.1.3   root     12279:                                m_CF = 1;
1.1       root     12280:                        }
                   12281:                }
                   12282:        } else {
                   12283:                REG16(AX) = 0x0c;
1.1.1.3   root     12284:                m_CF = 1;
1.1       root     12285:        }
                   12286: }
                   12287: 
1.1.1.43  root     12288: inline void msdos_int_21h_70h()
                   12289: {
                   12290:        switch(REG8(AL)) {
                   12291:        case 0x02:
                   12292:                if(REG16(CX) >= 7) {
                   12293:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   12294:                        msdos_nls_tables_update();
                   12295:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   12296:                        REG16(AX) = system_code_page;
                   12297:                } else {
                   12298:                        REG16(AX) = 0x0c;
                   12299:                        m_CF = 1;
                   12300:                }
                   12301:                break;
                   12302:        default:
                   12303:                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));
                   12304:                REG16(AX) = 0x01;
                   12305:                m_CF = 1;
                   12306:                break;
                   12307:        }
                   12308: }
                   12309: 
1.1       root     12310: inline void msdos_int_21h_710dh()
                   12311: {
                   12312:        // reset drive
                   12313: }
                   12314: 
1.1.1.17  root     12315: inline void msdos_int_21h_7141h(int lfn)
                   12316: {
                   12317:        if(REG16(SI) == 0) {
                   12318:                msdos_int_21h_41h(lfn);
                   12319:                return;
                   12320:        }
                   12321:        if(REG16(SI) != 1) {
                   12322:                REG16(AX) = 5;
                   12323:                m_CF = 1;
                   12324:        }
                   12325:        /* wild card and matching attributes... */
                   12326:        char tmp[MAX_PATH * 2];
                   12327:        // copy search pathname (and quick check overrun)
                   12328:        ZeroMemory(tmp, sizeof(tmp));
                   12329:        tmp[MAX_PATH - 1] = '\0';
                   12330:        tmp[MAX_PATH] = 1;
                   12331:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   12332:        
                   12333:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   12334:                REG16(AX) = 1;
                   12335:                m_CF = 1;
                   12336:                return;
                   12337:        }
                   12338:        for(char *s = tmp; *s; ++s) {
                   12339:                if(*s == '/') {
                   12340:                        *s = '\\';
                   12341:                }
                   12342:        }
                   12343:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   12344:        if(tmp_name) {
                   12345:                ++tmp_name;
                   12346:        } else {
                   12347:                tmp_name = strchr(tmp, ':');
                   12348:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   12349:        }
                   12350:        
                   12351:        WIN32_FIND_DATAA fd;
                   12352:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   12353:        if(fh == INVALID_HANDLE_VALUE) {
                   12354:                REG16(AX) = 2;
                   12355:                m_CF = 1;
                   12356:                return;
                   12357:        }
                   12358:        do {
                   12359:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   12360:                        strcpy(tmp_name, fd.cFileName);
                   12361:                        if(remove(msdos_trimmed_path(tmp, lfn))) {
                   12362:                                REG16(AX) = 5;
                   12363:                                m_CF = 1;
                   12364:                                break;
                   12365:                        }
                   12366:                }
                   12367:        } while(FindNextFileA(fh, &fd));
                   12368:        if(!m_CF) {
                   12369:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   12370:                        m_CF = 1;
                   12371:                        REG16(AX) = 2;
                   12372:                }
                   12373:        }
                   12374:        FindClose(fh);
                   12375: }
                   12376: 
1.1       root     12377: inline void msdos_int_21h_714eh()
                   12378: {
                   12379:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12380:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   12381:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12382:        WIN32_FIND_DATA fd;
                   12383:        
1.1.1.13  root     12384:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12385:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12386:                FindClose(dtainfo->find_handle);
                   12387:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12388:        }
                   12389:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12390:        dtainfo->allowable_mask = REG8(CL);
                   12391:        dtainfo->required_mask = REG8(CH);
                   12392:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12393:        
1.1.1.14  root     12394:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12395:                dtainfo->allowable_mask &= ~8;
1.1       root     12396:        }
1.1.1.14  root     12397:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   12398:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12399:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12400:                                FindClose(dtainfo->find_handle);
                   12401:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12402:                                break;
                   12403:                        }
                   12404:                }
                   12405:        }
1.1.1.13  root     12406:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12407:                find->attrib = fd.dwFileAttributes;
                   12408:                msdos_find_file_conv_local_time(&fd);
                   12409:                if(REG16(SI) == 0) {
                   12410:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12411:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12412:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12413:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12414:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12415:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12416:                } else {
                   12417:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12418:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12419:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12420:                }
                   12421:                find->size_hi = fd.nFileSizeHigh;
                   12422:                find->size_lo = fd.nFileSizeLow;
                   12423:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12424:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12425:                REG16(AX) = dtainfo - dtalist + 1;
                   12426:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12427:                // volume label
                   12428:                find->attrib = 8;
                   12429:                find->size_hi = find->size_lo = 0;
                   12430:                strcpy(find->full_name, process->volume_label);
                   12431:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12432:                dtainfo->allowable_mask &= ~8;
                   12433:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     12434:        } else {
                   12435:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12436:                m_CF = 1;
1.1       root     12437:        }
                   12438: }
                   12439: 
                   12440: inline void msdos_int_21h_714fh()
                   12441: {
                   12442:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12443:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     12444:        WIN32_FIND_DATA fd;
                   12445:        
1.1.1.14  root     12446:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12447:                REG16(AX) = 6;
1.1.1.13  root     12448:                m_CF = 1;
                   12449:                return;
                   12450:        }
1.1.1.14  root     12451:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12452:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12453:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     12454:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12455:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12456:                                        FindClose(dtainfo->find_handle);
                   12457:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12458:                                        break;
                   12459:                                }
                   12460:                        }
                   12461:                } else {
1.1.1.13  root     12462:                        FindClose(dtainfo->find_handle);
                   12463:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12464:                }
                   12465:        }
1.1.1.13  root     12466:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12467:                find->attrib = fd.dwFileAttributes;
                   12468:                msdos_find_file_conv_local_time(&fd);
                   12469:                if(REG16(SI) == 0) {
                   12470:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12471:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12472:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12473:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12474:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12475:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12476:                } else {
                   12477:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12478:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12479:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12480:                }
                   12481:                find->size_hi = fd.nFileSizeHigh;
                   12482:                find->size_lo = fd.nFileSizeLow;
                   12483:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12484:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12485:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12486:                // volume label
                   12487:                find->attrib = 8;
                   12488:                find->size_hi = find->size_lo = 0;
                   12489:                strcpy(find->full_name, process->volume_label);
                   12490:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12491:                dtainfo->allowable_mask &= ~8;
1.1       root     12492:        } else {
                   12493:                REG16(AX) = 0x12;
1.1.1.3   root     12494:                m_CF = 1;
1.1       root     12495:        }
                   12496: }
                   12497: 
                   12498: inline void msdos_int_21h_71a0h()
                   12499: {
                   12500:        DWORD max_component_len, file_sys_flag;
                   12501:        
1.1.1.14  root     12502:        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))) {
                   12503:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   12504:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     12505:                REG16(CX) = (UINT16)max_component_len;          // 255
                   12506:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   12507:        } else {
                   12508:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12509:                m_CF = 1;
1.1       root     12510:        }
                   12511: }
                   12512: 
                   12513: inline void msdos_int_21h_71a1h()
                   12514: {
1.1.1.14  root     12515:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12516:                REG16(AX) = 6;
1.1.1.13  root     12517:                m_CF = 1;
                   12518:                return;
                   12519:        }
1.1.1.14  root     12520:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12521:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12522:                FindClose(dtainfo->find_handle);
                   12523:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12524:        }
                   12525: }
                   12526: 
                   12527: inline void msdos_int_21h_71a6h()
                   12528: {
                   12529:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12530:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   12531:        
1.1.1.3   root     12532:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12533:        struct _stat64 status;
                   12534:        DWORD serial_number = 0;
                   12535:        
1.1.1.20  root     12536:        if(fd < process->max_files && file_handler[fd].valid) {
                   12537:                if(_fstat64(fd, &status) == 0) {
                   12538:                        if(file_handler[fd].path[1] == ':') {
1.1       root     12539:                                // NOTE: we need to consider the network file path "\\host\share\"
                   12540:                                char volume[] = "A:\\";
1.1.1.20  root     12541:                                volume[0] = file_handler[fd].path[1];
1.1       root     12542:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   12543:                        }
1.1.1.20  root     12544:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     12545:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   12546:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   12547:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   12548:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   12549:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   12550:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   12551:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   12552:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   12553:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   12554:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     12555:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     12556:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     12557:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     12558:                } else {
                   12559:                        REG16(AX) = errno;
1.1.1.3   root     12560:                        m_CF = 1;
1.1       root     12561:                }
                   12562:        } else {
                   12563:                REG16(AX) = 0x06;
1.1.1.3   root     12564:                m_CF = 1;
1.1       root     12565:        }
                   12566: }
                   12567: 
                   12568: inline void msdos_int_21h_71a7h()
                   12569: {
                   12570:        switch(REG8(BL)) {
                   12571:        case 0x00:
1.1.1.3   root     12572:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     12573:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12574:                        m_CF = 1;
1.1       root     12575:                }
                   12576:                break;
                   12577:        case 0x01:
                   12578:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     12579:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     12580:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12581:                        m_CF = 1;
1.1       root     12582:                }
                   12583:                break;
                   12584:        default:
1.1.1.22  root     12585:                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     12586:                REG16(AX) = 0x01;
1.1.1.3   root     12587:                m_CF = 1;
1.1       root     12588:                break;
                   12589:        }
                   12590: }
                   12591: 
                   12592: inline void msdos_int_21h_71a8h()
                   12593: {
                   12594:        if(REG8(DH) == 0) {
                   12595:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     12596:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     12597:                memset(fcb, 0x20, sizeof(fcb));
                   12598:                int len = strlen(tmp);
1.1.1.21  root     12599:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     12600:                        if(tmp[i] == '.') {
                   12601:                                pos = 8;
                   12602:                        } else {
                   12603:                                if(msdos_lead_byte_check(tmp[i])) {
                   12604:                                        fcb[pos++] = tmp[i++];
                   12605:                                }
                   12606:                                fcb[pos++] = tmp[i];
                   12607:                        }
                   12608:                }
1.1.1.3   root     12609:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     12610:        } else {
1.1.1.3   root     12611:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     12612:        }
                   12613: }
                   12614: 
1.1.1.22  root     12615: inline void msdos_int_21h_71aah()
                   12616: {
                   12617:        char drv[] = "A:", path[MAX_PATH];
                   12618:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12619:        
                   12620:        if(REG8(BL) == 0) {
                   12621:                drv[0] = 'A' + _getdrive() - 1;
                   12622:        } else {
                   12623:                drv[0] = 'A' + REG8(BL) - 1;
                   12624:        }
                   12625:        switch(REG8(BH)) {
                   12626:        case 0x00:
1.1.1.44  root     12627:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12628:                        REG16(AX) = 0x0f; // invalid drive
                   12629:                        m_CF = 1;
                   12630:                } else if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   12631:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     12632:                        m_CF = 1;
                   12633:                }
                   12634:                break;
                   12635:        case 0x01:
1.1.1.44  root     12636:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12637:                        REG16(AX) = 0x0f; // invalid drive
                   12638:                        m_CF = 1;
                   12639:                } else if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     12640:                        REG16(AX) = 0x0f; // invalid drive
                   12641:                        m_CF = 1;
                   12642:                }
                   12643:                break;
                   12644:        case 0x02:
1.1.1.44  root     12645:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12646:                        REG16(AX) = 0x0f; // invalid drive
                   12647:                        m_CF = 1;
                   12648:                } else if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     12649:                        REG16(AX) = 0x0f; // invalid drive
                   12650:                        m_CF = 1;
                   12651:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   12652:                        REG16(AX) = 0x0f; // invalid drive
                   12653:                        m_CF = 1;
                   12654:                } else {
                   12655:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   12656:                }
                   12657:                break;
                   12658:        default:
                   12659:                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));
                   12660:                REG16(AX) = 0x01;
                   12661:                m_CF = 1;
                   12662:                break;
                   12663:        }
                   12664: }
                   12665: 
1.1.1.14  root     12666: inline void msdos_int_21h_7300h()
                   12667: {
1.1.1.44  root     12668:        REG8(AL) = REG8(CL);
                   12669:        REG8(AH) = 0;
1.1.1.14  root     12670: }
                   12671: 
                   12672: inline void msdos_int_21h_7302h()
                   12673: {
                   12674:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   12675:        UINT16 seg, ofs;
                   12676:        
                   12677:        if(REG16(CX) < 0x3f) {
                   12678:                REG8(AL) = 0x18;
                   12679:                m_CF = 1;
                   12680:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   12681:                REG8(AL) = 0xff;
                   12682:                m_CF = 1;
                   12683:        } else {
                   12684:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   12685:        }
                   12686: }
                   12687: 
1.1       root     12688: inline void msdos_int_21h_7303h()
                   12689: {
1.1.1.3   root     12690:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12691:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     12692:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   12693:        
                   12694:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   12695:                info->size_of_structure = sizeof(ext_space_info_t);
                   12696:                info->structure_version = 0;
                   12697:                info->sectors_per_cluster = sectors_per_cluster;
                   12698:                info->bytes_per_sector = bytes_per_sector;
                   12699:                info->available_clusters_on_drive = free_clusters;
                   12700:                info->total_clusters_on_drive = total_clusters;
                   12701:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   12702:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   12703:                info->available_allocation_units = free_clusters;       // ???
                   12704:                info->total_allocation_units = total_clusters;          // ???
                   12705:        } else {
                   12706:                REG16(AX) = errno;
1.1.1.3   root     12707:                m_CF = 1;
1.1       root     12708:        }
                   12709: }
                   12710: 
1.1.1.30  root     12711: inline void msdos_int_21h_dbh()
                   12712: {
                   12713:        // Novell NetWare - Workstation - Get Number of Local Drives
                   12714:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   12715:        REG8(AL) = dos_info->last_drive;
                   12716: }
                   12717: 
                   12718: inline void msdos_int_21h_dch()
                   12719: {
                   12720:        // Novell NetWare - Connection Services - Get Connection Number
                   12721:        REG8(AL) = 0x00;
                   12722: }
                   12723: 
1.1.1.32  root     12724: inline void msdos_int_24h()
                   12725: {
                   12726:        const char *message = NULL;
                   12727:        int key = 0;
                   12728:        
                   12729:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   12730:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   12731:                        if(active_code_page == 932) {
                   12732:                                message = critical_error_table[i].message_japanese;
                   12733:                        }
                   12734:                        if(message == NULL) {
                   12735:                                message = critical_error_table[i].message_english;
                   12736:                        }
                   12737:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   12738:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   12739:                        
                   12740:                        SREG(ES) = WORK_TOP >> 4;
                   12741:                        i386_load_segment_descriptor(ES);
                   12742:                        REG16(DI) = 0x0000;
                   12743:                        break;
                   12744:                }
                   12745:        }
                   12746:        fprintf(stderr, "\n%s", message);
                   12747:        if(!(REG8(AH) & 0x80)) {
                   12748:                if(REG8(AH) & 0x01) {
                   12749:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   12750:                } else {
                   12751:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   12752:                }
                   12753:        }
                   12754:        fprintf(stderr, "\n");
                   12755:        
1.1.1.33  root     12756:        {
1.1.1.32  root     12757:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     12758:        }
1.1.1.32  root     12759:        if(REG8(AH) & 0x10) {
                   12760:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   12761:        }
                   12762:        if(REG8(AH) & 0x20) {
                   12763:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   12764:        }
                   12765:        if(REG8(AH) & 0x08) {
                   12766:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   12767:        }
                   12768:        fprintf(stderr, "? ");
                   12769:        
                   12770:        while(1) {
                   12771:                while(!_kbhit()) {
                   12772:                        Sleep(10);
                   12773:                }
                   12774:                key = _getch();
                   12775:                
                   12776:                if(key == 'I' || key == 'i') {
                   12777:                        if(REG8(AH) & 0x20) {
                   12778:                                REG8(AL) = 0;
                   12779:                                break;
                   12780:                        }
                   12781:                } else if(key == 'R' || key == 'r') {
                   12782:                        if(REG8(AH) & 0x10) {
                   12783:                                REG8(AL) = 1;
                   12784:                                break;
                   12785:                        }
                   12786:                } else if(key == 'A' || key == 'a') {
                   12787:                        REG8(AL) = 2;
                   12788:                        break;
                   12789:                } else if(key == 'F' || key == 'f') {
                   12790:                        if(REG8(AH) & 0x08) {
                   12791:                                REG8(AL) = 3;
                   12792:                                break;
                   12793:                        }
                   12794:                }
                   12795:        }
                   12796:        fprintf(stderr, "%c\n", key);
                   12797: }
                   12798: 
1.1       root     12799: inline void msdos_int_25h()
                   12800: {
                   12801:        UINT16 seg, ofs;
                   12802:        DWORD dwSize;
                   12803:        
1.1.1.3   root     12804: #if defined(HAS_I386)
                   12805:        I386OP(pushf)();
                   12806: #else
                   12807:        PREFIX86(_pushf());
                   12808: #endif
1.1       root     12809:        
                   12810:        if(!(REG8(AL) < 26)) {
                   12811:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     12812:                m_CF = 1;
1.1       root     12813:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   12814:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12815:                m_CF = 1;
1.1       root     12816:        } else {
                   12817:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   12818:                char dev[64];
                   12819:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   12820:                
                   12821:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   12822:                if(hFile == INVALID_HANDLE_VALUE) {
                   12823:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12824:                        m_CF = 1;
1.1       root     12825:                } else {
1.1.1.19  root     12826:                        UINT32 top_sector  = REG16(DX);
                   12827:                        UINT16 sector_num  = REG16(CX);
                   12828:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   12829:                        
                   12830:                        if(sector_num == 0xffff) {
                   12831:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   12832:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   12833:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   12834:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   12835:                                buffer_addr = (seg << 4) + ofs;
                   12836:                        }
                   12837: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   12838: //                             REG8(AL) = 0x02; // drive not ready
                   12839: //                             m_CF = 1;
                   12840: //                     } else 
                   12841:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     12842:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     12843:                                m_CF = 1;
1.1.1.19  root     12844:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     12845:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     12846:                                m_CF = 1;
1.1       root     12847:                        }
                   12848:                        CloseHandle(hFile);
                   12849:                }
                   12850:        }
                   12851: }
                   12852: 
                   12853: inline void msdos_int_26h()
                   12854: {
1.1.1.42  root     12855:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     12856:        UINT16 seg, ofs;
                   12857:        DWORD dwSize;
                   12858:        
1.1.1.3   root     12859: #if defined(HAS_I386)
                   12860:        I386OP(pushf)();
                   12861: #else
                   12862:        PREFIX86(_pushf());
                   12863: #endif
1.1       root     12864:        
                   12865:        if(!(REG8(AL) < 26)) {
                   12866:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     12867:                m_CF = 1;
1.1       root     12868:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   12869:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12870:                m_CF = 1;
1.1       root     12871:        } else {
                   12872:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   12873:                char dev[64];
                   12874:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   12875:                
                   12876:                if(dpb->media_type == 0xf8) {
                   12877:                        // this drive is not a floppy
1.1.1.6   root     12878: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   12879: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   12880: //                     }
1.1       root     12881:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12882:                        m_CF = 1;
1.1       root     12883:                } else {
                   12884:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   12885:                        if(hFile == INVALID_HANDLE_VALUE) {
                   12886:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12887:                                m_CF = 1;
1.1       root     12888:                        } else {
1.1.1.19  root     12889:                                UINT32 top_sector  = REG16(DX);
                   12890:                                UINT16 sector_num  = REG16(CX);
                   12891:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   12892:                                
                   12893:                                if(sector_num == 0xffff) {
                   12894:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   12895:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   12896:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   12897:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   12898:                                        buffer_addr = (seg << 4) + ofs;
                   12899:                                }
1.1       root     12900:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   12901:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12902:                                        m_CF = 1;
1.1.1.19  root     12903:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     12904:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     12905:                                        m_CF = 1;
1.1.1.19  root     12906:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     12907:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     12908:                                        m_CF = 1;
1.1       root     12909:                                }
                   12910:                                CloseHandle(hFile);
                   12911:                        }
                   12912:                }
                   12913:        }
                   12914: }
                   12915: 
                   12916: inline void msdos_int_27h()
                   12917: {
1.1.1.29  root     12918:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   12919:        try {
                   12920:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   12921:        } catch(...) {
                   12922:                // recover the broken mcb
                   12923:                int mcb_seg = SREG(CS) - 1;
                   12924:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     12925:                
1.1.1.29  root     12926:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     12927:                        mcb->mz = 'M';
                   12928:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   12929:                        
1.1.1.29  root     12930:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     12931:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     12932:                        } else {
1.1.1.39  root     12933:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     12934:                        }
                   12935:                } else {
                   12936:                        mcb->mz = 'Z';
1.1.1.30  root     12937:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     12938:                }
                   12939:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   12940:        }
1.1.1.3   root     12941:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     12942: }
                   12943: 
                   12944: inline void msdos_int_29h()
                   12945: {
1.1.1.14  root     12946: #if 1
                   12947:        // need to check escape sequences
1.1       root     12948:        msdos_putch(REG8(AL));
1.1.1.14  root     12949: #else
                   12950:        DWORD num;
                   12951:        vram_flush();
1.1.1.23  root     12952:        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     12953:        cursor_moved = true;
                   12954: #endif
1.1       root     12955: }
                   12956: 
                   12957: inline void msdos_int_2eh()
                   12958: {
                   12959:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   12960:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12961:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     12962:        char *token = my_strtok(tmp, " ");
                   12963:        strcpy(command, token);
                   12964:        strcpy(opt, token + strlen(token) + 1);
                   12965:        
                   12966:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   12967:        param->env_seg = 0;
                   12968:        param->cmd_line.w.l = 44;
                   12969:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   12970:        param->fcb1.w.l = 24;
                   12971:        param->fcb1.w.h = (WORK_TOP >> 4);
                   12972:        param->fcb2.w.l = 24;
                   12973:        param->fcb2.w.h = (WORK_TOP >> 4);
                   12974:        
                   12975:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   12976:        
                   12977:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   12978:        cmd_line->len = strlen(opt);
                   12979:        strcpy(cmd_line->cmd, opt);
                   12980:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   12981:        
1.1.1.28  root     12982:        try {
                   12983:                if(msdos_process_exec(command, param, 0)) {
                   12984:                        REG16(AX) = 0xffff; // error before processing command
                   12985:                } else {
                   12986:                        // set flag to set retval to ax when the started process is terminated
                   12987:                        process_t *process = msdos_process_info_get(current_psp);
                   12988:                        process->called_by_int2eh = true;
                   12989:                }
                   12990:        } catch(...) {
                   12991:                REG16(AX) = 0xffff; // error before processing command
                   12992:        }
1.1       root     12993: }
                   12994: 
1.1.1.29  root     12995: inline void msdos_int_2fh_05h()
                   12996: {
                   12997:        switch(REG8(AL)) {
                   12998:        case 0x00:
1.1.1.32  root     12999:                REG8(AL) = 0xff;
                   13000:                break;
                   13001:        case 0x01:
                   13002:        case 0x02:
                   13003:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   13004:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   13005:                                const char *message = NULL;
                   13006:                                if(active_code_page == 932) {
                   13007:                                        message = standard_error_table[i].message_japanese;
                   13008:                                }
                   13009:                                if(message == NULL) {
                   13010:                                        message = standard_error_table[i].message_english;
                   13011:                                }
                   13012:                                strcpy((char *)(mem + WORK_TOP), message);
                   13013:                                
                   13014:                                SREG(ES) = WORK_TOP >> 4;
                   13015:                                i386_load_segment_descriptor(ES);
                   13016:                                REG16(DI) = 0x0000;
                   13017:                                REG8(AL) = 0x01;
                   13018:                                break;
                   13019:                        }
                   13020:                }
1.1.1.29  root     13021:                break;
                   13022:        default:
                   13023:                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));
                   13024:                m_CF = 1;
                   13025:        }
                   13026: }
                   13027: 
1.1.1.44  root     13028: inline void msdos_int_2fh_06h()
                   13029: {
                   13030:        switch(REG8(AL)) {
                   13031:        case 0x00:
                   13032:                // ASSIGN is not installed
                   13033:                break;
                   13034:        case 0x01:
                   13035:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   13036:                REG16(AX) = 0x01;
                   13037:                m_CF = 1;
                   13038:                break;
                   13039:        default:
                   13040:                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));
                   13041:                REG16(AX) = 0x01;
                   13042:                m_CF = 1;
                   13043:                break;
                   13044:        }
                   13045: }
                   13046: 
1.1.1.22  root     13047: inline void msdos_int_2fh_11h()
                   13048: {
                   13049:        switch(REG8(AL)) {
                   13050:        case 0x00:
1.1.1.29  root     13051:                if(i386_read_stack() == 0xdada) {
                   13052:                        // MSCDEX is not installed
                   13053: //                     REG8(AL) = 0x00;
                   13054:                } else {
                   13055:                        // Network Redirector is not installed
                   13056: //                     REG8(AL) = 0x00;
                   13057:                }
1.1.1.22  root     13058:                break;
                   13059:        default:
1.1.1.43  root     13060: //             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     13061:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     13062:                m_CF = 1;
                   13063:                break;
                   13064:        }
                   13065: }
                   13066: 
1.1.1.21  root     13067: inline void msdos_int_2fh_12h()
                   13068: {
                   13069:        switch(REG8(AL)) {
1.1.1.22  root     13070:        case 0x00:
1.1.1.29  root     13071:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     13072:                REG8(AL) = 0xff;
                   13073:                break;
1.1.1.29  root     13074: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   13075:        case 0x02:
                   13076:                {
                   13077:                        UINT16 stack = i386_read_stack();
                   13078:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   13079:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   13080:                        i386_load_segment_descriptor(ES);
                   13081:                }
                   13082:                break;
1.1.1.30  root     13083:        case 0x03:
                   13084:                SREG(DS) = (DEVICE_TOP >> 4);
                   13085:                i386_load_segment_descriptor(DS);
                   13086:                break;
1.1.1.29  root     13087:        case 0x04:
                   13088:                {
                   13089:                        UINT16 stack = i386_read_stack();
                   13090:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   13091: #if defined(HAS_I386)
                   13092:                        m_ZF = (REG8(AL) == '\\');
                   13093: #else
                   13094:                        m_ZeroVal = (REG8(AL) != '\\');
                   13095: #endif
                   13096:                }
                   13097:                break;
                   13098:        case 0x05:
                   13099:                msdos_putch(i386_read_stack());
                   13100:                break;
                   13101: //     case 0x06: // DOS 3.0+ internal - Invole Critical Error
                   13102: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   13103: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
                   13104: //     case 0x09: // DOS 3.0+ internal - Flush and FREE Disk Buffer
                   13105: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   13106: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   13107: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   13108:        case 0x0d:
                   13109:                {
                   13110:                        SYSTEMTIME time;
                   13111:                        FILETIME file_time;
                   13112:                        WORD dos_date, dos_time;
                   13113:                        GetLocalTime(&time);
                   13114:                        SystemTimeToFileTime(&time, &file_time);
                   13115:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   13116:                        REG16(AX) = dos_date;
                   13117:                        REG16(DX) = dos_time;
                   13118:                }
                   13119:                break;
                   13120: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   13121: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   13122: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   13123:        case 0x11:
                   13124:                {
                   13125:                        char path[MAX_PATH], *p;
                   13126:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13127:                        my_strupr(path);
                   13128:                        while((p = my_strchr(path, '/')) != NULL) {
                   13129:                                *p = '\\';
                   13130:                        }
                   13131:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   13132:                }
                   13133:                break;
                   13134:        case 0x12:
                   13135:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13136:                break;
                   13137:        case 0x13:
                   13138:                {
                   13139:                        char tmp[2] = {0};
                   13140:                        tmp[0] = i386_read_stack();
                   13141:                        my_strupr(tmp);
                   13142:                        REG8(AL) = tmp[0];
                   13143:                }
                   13144:                break;
                   13145:        case 0x14:
                   13146: #if defined(HAS_I386)
                   13147:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   13148: #else
                   13149:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13150: #endif
                   13151:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13152:                break;
                   13153: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     13154:        case 0x16:
                   13155:                if(REG16(BX) < 20) {
                   13156:                        SREG(ES) = SFT_TOP >> 4;
                   13157:                        i386_load_segment_descriptor(ES);
                   13158:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   13159:                        
                   13160:                        // update system file table
                   13161:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   13162:                        if(file_handler[REG16(BX)].valid) {
                   13163:                                int count = 0;
                   13164:                                for(int i = 0; i < 20; i++) {
                   13165:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   13166:                                                count++;
                   13167:                                        }
                   13168:                                }
                   13169:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   13170:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   13171:                                _lseek(REG16(BX), 0, SEEK_END);
                   13172:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   13173:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   13174:                        } else {
                   13175:                                memset(sft, 0, 0x3b);
                   13176:                        }
                   13177:                } else {
                   13178:                        REG16(AX) = 0x06;
                   13179:                        m_CF = 1;
                   13180:                }
                   13181:                break;
1.1.1.29  root     13182: //     case 0x17: // DOS 3.0+ internal - Get Current Directory Structure for Drive
                   13183: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   13184: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   13185:        case 0x1a:
                   13186:                {
                   13187:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   13188:                        if(path[1] == ':') {
                   13189:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   13190:                                        REG8(AL) = path[0] - 'a' + 1;
                   13191:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   13192:                                        REG8(AL) = path[0] - 'A' + 1;
                   13193:                                } else {
                   13194:                                        REG8(AL) = 0xff; // invalid
                   13195:                                }
                   13196:                                strcpy(full, path);
                   13197:                                strcpy(path, full + 2);
                   13198:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   13199:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   13200:                                        REG8(AL) = full[0] - 'a' + 1;
                   13201:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   13202:                                        REG8(AL) = full[0] - 'A' + 1;
                   13203:                                } else {
                   13204:                                        REG8(AL) = 0xff; // invalid
                   13205:                                }
                   13206:                        } else {
                   13207:                                REG8(AL) = 0x00; // default
                   13208:                        }
                   13209:                }
                   13210:                break;
                   13211:        case 0x1b:
                   13212:                {
                   13213:                        int year = REG16(CX) + 1980;
                   13214:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   13215:                }
                   13216:                break;
                   13217: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   13218: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   13219:        case 0x1e:
                   13220:                {
                   13221:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   13222:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   13223:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   13224: #if defined(HAS_I386)
                   13225:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   13226: #else
                   13227:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   13228: #endif
                   13229:                        } else {
                   13230: #if defined(HAS_I386)
                   13231:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   13232: #else
                   13233:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   13234: #endif
                   13235:                        }
                   13236:                }
                   13237:                break;
                   13238: //     case 0x1f: // DOS 3.0+ internal - Build Current Directory Structure
1.1.1.21  root     13239:        case 0x20:
                   13240:                {
                   13241:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13242:                        
                   13243:                        if(fd < 20) {
                   13244:                                SREG(ES) = current_psp;
                   13245:                                i386_load_segment_descriptor(ES);
                   13246:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   13247:                        } else {
                   13248:                                REG16(AX) = 0x06;
                   13249:                                m_CF = 1;
                   13250:                        }
                   13251:                }
                   13252:                break;
1.1.1.29  root     13253:        case 0x21:
                   13254:                msdos_int_21h_60h(0);
                   13255:                break;
                   13256: //     case 0x22: // DOS 3.0+ internal - Set Extended Error Info
                   13257: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   13258: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   13259:        case 0x25:
                   13260:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13261:                break;
                   13262:        case 0x26:
                   13263:                REG8(AL) = REG8(CL);
                   13264:                msdos_int_21h_3dh();
                   13265:                break;
                   13266:        case 0x27:
                   13267:                msdos_int_21h_3eh();
                   13268:                break;
                   13269:        case 0x28:
                   13270:                REG16(AX) = REG16(BP);
                   13271:                msdos_int_21h_42h();
                   13272:                break;
                   13273:        case 0x29:
                   13274:                msdos_int_21h_3fh();
                   13275:                break;
                   13276: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   13277:        case 0x2b:
                   13278:                REG16(AX) = REG16(BP);
                   13279:                msdos_int_21h_44h();
                   13280:                break;
                   13281:        case 0x2c:
                   13282:                REG16(BX) = DEVICE_TOP >> 4;
                   13283:                REG16(AX) = 22;
                   13284:                break;
                   13285:        case 0x2d:
                   13286:                {
                   13287:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13288:                        REG16(AX) = sda->extended_error_code;
                   13289:                }
                   13290:                break;
                   13291:        case 0x2e:
                   13292:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     13293:                        SREG(ES) = 0x0001;
                   13294:                        i386_load_segment_descriptor(ES);
                   13295:                        REG16(DI) = 0x00;
                   13296:                } else if(REG8(DL) == 0x08) {
                   13297:                        // dummy parameter error message read routine is at fffd:0010
                   13298:                        SREG(ES) = 0xfffd;
1.1.1.22  root     13299:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     13300:                        REG16(DI) = 0x0010;
1.1.1.22  root     13301:                }
                   13302:                break;
1.1.1.29  root     13303:        case 0x2f:
                   13304:                if(REG16(DX) != 0) {
1.1.1.30  root     13305:                        dos_major_version = REG8(DL);
                   13306:                        dos_minor_version = REG8(DH);
1.1.1.29  root     13307:                } else {
                   13308:                        REG8(DL) = 7;
                   13309:                        REG8(DH) = 10;
                   13310:                }
                   13311:                break;
                   13312: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   13313: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     13314:        default:
                   13315:                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));
                   13316:                REG16(AX) = 0x01;
                   13317:                m_CF = 1;
                   13318:                break;
                   13319:        }
                   13320: }
                   13321: 
1.1.1.30  root     13322: inline void msdos_int_2fh_13h()
                   13323: {
                   13324:        static UINT16 prevDS = 0, prevDX = 0;
                   13325:        static UINT16 prevES = 0, prevBX = 0;
                   13326:        UINT16 tmp;
                   13327:        
                   13328:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   13329:        i386_load_segment_descriptor(DS);
                   13330:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   13331:        
                   13332:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   13333:        i386_load_segment_descriptor(ES);
                   13334:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   13335: }
                   13336: 
1.1.1.22  root     13337: inline void msdos_int_2fh_14h()
                   13338: {
                   13339:        switch(REG8(AL)) {
                   13340:        case 0x00:
1.1.1.29  root     13341:                // NLSFUNC.COM is installed
                   13342:                REG8(AL) = 0xff;
1.1.1.25  root     13343:                break;
                   13344:        case 0x01:
                   13345:        case 0x03:
                   13346:                REG8(AL) = 0x00;
                   13347:                active_code_page = REG16(BX);
                   13348:                msdos_nls_tables_update();
                   13349:                break;
                   13350:        case 0x02:
                   13351:                REG8(AL) = get_extended_country_info(REG16(BP));
                   13352:                break;
                   13353:        case 0x04:
1.1.1.42  root     13354:                for(int i = 0;; i++) {
                   13355:                        if(country_table[i].code == REG16(DX)) {
                   13356:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   13357:                                break;
                   13358:                        } else if(country_table[i].code == -1) {
                   13359:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13360:                                break;
                   13361:                        }
                   13362:                }
1.1.1.25  root     13363:                REG8(AL) = 0x00;
1.1.1.22  root     13364:                break;
                   13365:        default:
                   13366:                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));
                   13367:                REG16(AX) = 0x01;
                   13368:                m_CF = 1;
                   13369:                break;
                   13370:        }
                   13371: }
                   13372: 
                   13373: inline void msdos_int_2fh_15h()
                   13374: {
                   13375:        switch(REG8(AL)) {
1.1.1.29  root     13376:        case 0x00: // CD-ROM - Installation Check
                   13377:                if(REG16(BX) == 0x0000) {
1.1.1.43  root     13378: #if 0
                   13379:                        // MSCDEX is installed
                   13380:                        REG16(BX) = 0;
                   13381:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     13382:                                if(msdos_is_cdrom_drive(i)) {
                   13383:                                        if(REG16(BX) == 0) {
                   13384:                                                REG16(CX) = i;
1.1.1.43  root     13385:                                        }
1.1.1.44  root     13386:                                        REG16(BX)++;
1.1.1.43  root     13387:                                }
                   13388:                        }
                   13389: #else
1.1.1.29  root     13390:                        // MSCDEX is not installed
                   13391: //                     REG8(AL) = 0x00;
1.1.1.43  root     13392: #endif
1.1.1.29  root     13393:                } else {
                   13394:                        // GRAPHICS.COM is not installed
                   13395: //                     REG8(AL) = 0x00;
                   13396:                }
1.1.1.22  root     13397:                break;
1.1.1.43  root     13398:        case 0x0b:
1.1.1.44  root     13399:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   13400:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   13401:                REG16(BX) = 0xadad;
1.1.1.43  root     13402:                break;
                   13403:        case 0x0d:
1.1.1.44  root     13404:                for(int i = 0, n = 0; i < 26; i++) {
                   13405:                        if(msdos_is_cdrom_drive(i)) {
                   13406:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     13407:                        }
                   13408:                }
                   13409:                break;
1.1.1.22  root     13410:        case 0xff:
1.1.1.29  root     13411:                if(REG16(BX) == 0x0000) {
                   13412:                        // CORELCDX is not installed
                   13413:                } else {
                   13414:                        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));
                   13415:                        REG16(AX) = 0x01;
                   13416:                        m_CF = 1;
                   13417:                }
1.1.1.22  root     13418:                break;
1.1.1.21  root     13419:        default:
1.1.1.22  root     13420:                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     13421:                REG16(AX) = 0x01;
                   13422:                m_CF = 1;
                   13423:                break;
                   13424:        }
                   13425: }
                   13426: 
1.1       root     13427: inline void msdos_int_2fh_16h()
                   13428: {
                   13429:        switch(REG8(AL)) {
                   13430:        case 0x00:
1.1.1.14  root     13431:                if(no_windows) {
1.1.1.29  root     13432:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   13433: //                     REG8(AL) = 0x00;
1.1.1.14  root     13434:                } else {
1.1.1.30  root     13435:                        REG8(AL) = win_major_version;
                   13436:                        REG8(AH) = win_minor_version;
1.1       root     13437:                }
                   13438:                break;
1.1.1.43  root     13439:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     13440:                // from DOSBox
                   13441:                i386_set_a20_line(1);
                   13442:                break;
1.1.1.43  root     13443:        case 0x06: // Windows Enhanced Mode & 286 DOSX exit Broadcast
                   13444:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   13445:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   13446:                break;
                   13447:        case 0x07:
                   13448:                // Virtual Device Call API
                   13449:                break;
1.1.1.22  root     13450:        case 0x0a:
                   13451:                if(!no_windows) {
                   13452:                        REG16(AX) = 0x0000;
1.1.1.30  root     13453:                        REG8(BH) = win_major_version;
                   13454:                        REG8(BL) = win_minor_version;
1.1.1.22  root     13455:                        REG16(CX) = 0x0003; // enhanced
                   13456:                }
                   13457:                break;
1.1.1.30  root     13458:        case 0x0b:
                   13459:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     13460:        case 0x0e:
                   13461:        case 0x0f:
1.1.1.30  root     13462:        case 0x10:
1.1.1.22  root     13463:        case 0x11:
                   13464:        case 0x12:
                   13465:        case 0x13:
                   13466:        case 0x14:
1.1.1.30  root     13467:        case 0x15:
1.1.1.43  root     13468:        case 0x81:
                   13469:        case 0x82:
1.1.1.44  root     13470:        case 0x84:
1.1.1.33  root     13471:        case 0x86:
1.1.1.22  root     13472:        case 0x87:
1.1.1.30  root     13473:        case 0x89:
1.1.1.33  root     13474:        case 0x8a:
1.1.1.22  root     13475:                // function not supported, do not clear AX
                   13476:                break;
1.1.1.14  root     13477:        case 0x80:
                   13478:                Sleep(10);
1.1.1.35  root     13479:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     13480:                REG8(AL) = 0x00;
1.1.1.14  root     13481:                break;
1.1.1.33  root     13482:        case 0x83:
                   13483:                REG16(BX) = 0x01; // system vm id
                   13484:                break;
1.1.1.22  root     13485:        case 0x8e:
                   13486:                REG16(AX) = 0x00; // failed
                   13487:                break;
1.1.1.20  root     13488:        case 0x8f:
                   13489:                switch(REG8(DH)) {
                   13490:                case 0x00:
                   13491:                case 0x02:
                   13492:                case 0x03:
                   13493:                        REG16(AX) = 0x00;
                   13494:                        break;
                   13495:                case 0x01:
                   13496:                        REG16(AX) = 0x168f;
                   13497:                        break;
                   13498:                }
                   13499:                break;
1.1       root     13500:        default:
1.1.1.22  root     13501:                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));
                   13502:                REG16(AX) = 0x01;
                   13503:                m_CF = 1;
                   13504:                break;
                   13505:        }
                   13506: }
                   13507: 
                   13508: inline void msdos_int_2fh_19h()
                   13509: {
                   13510:        switch(REG8(AL)) {
                   13511:        case 0x00:
1.1.1.29  root     13512:                // SHELLB.COM is not installed
                   13513: //             REG8(AL) = 0x00;
1.1.1.22  root     13514:                break;
                   13515:        case 0x01:
                   13516:        case 0x02:
                   13517:        case 0x03:
                   13518:        case 0x04:
                   13519:                REG16(AX) = 0x01;
                   13520:                m_CF = 1;
                   13521:                break;
1.1.1.29  root     13522:        case 0x80:
                   13523:                // IBM ROM-DOS v4.0 is not installed
                   13524: //             REG8(AL) = 0x00;
                   13525:                break;
1.1.1.22  root     13526:        default:
                   13527:                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     13528:                REG16(AX) = 0x01;
1.1.1.3   root     13529:                m_CF = 1;
1.1       root     13530:                break;
                   13531:        }
                   13532: }
                   13533: 
                   13534: inline void msdos_int_2fh_1ah()
                   13535: {
                   13536:        switch(REG8(AL)) {
                   13537:        case 0x00:
1.1.1.29  root     13538:                // ANSI.SYS is installed
1.1       root     13539:                REG8(AL) = 0xff;
                   13540:                break;
                   13541:        default:
1.1.1.22  root     13542:                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));
                   13543:                REG16(AX) = 0x01;
                   13544:                m_CF = 1;
                   13545:                break;
                   13546:        }
                   13547: }
                   13548: 
1.1.1.30  root     13549: inline void msdos_int_2fh_40h()
1.1.1.22  root     13550: {
                   13551:        switch(REG8(AL)) {
                   13552:        case 0x00:
1.1.1.30  root     13553:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   13554:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     13555:                break;
1.1.1.43  root     13556:        case 0x10:
                   13557:                // OS/2 v2.0+ - Installation Check
                   13558:                REG16(AX) = 0x01;
                   13559:                m_CF = 1;
                   13560:                break;
1.1.1.22  root     13561:        default:
                   13562:                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     13563:                REG16(AX) = 0x01;
1.1.1.3   root     13564:                m_CF = 1;
1.1       root     13565:                break;
                   13566:        }
                   13567: }
                   13568: 
                   13569: inline void msdos_int_2fh_43h()
                   13570: {
                   13571:        switch(REG8(AL)) {
                   13572:        case 0x00:
1.1.1.29  root     13573:                // XMS is installed ?
1.1.1.19  root     13574: #ifdef SUPPORT_XMS
                   13575:                if(support_xms) {
                   13576:                        REG8(AL) = 0x80;
1.1.1.44  root     13577:                }
                   13578: #endif
                   13579:                break;
                   13580:        case 0x08:
                   13581: #ifdef SUPPORT_XMS
                   13582:                if(support_xms) {
                   13583:                        REG8(AL) = 0x43;
                   13584:                        REG8(BL) = 0x01; // IBM PC/AT
                   13585:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   13586:                }
1.1.1.19  root     13587: #endif
                   13588:                break;
                   13589:        case 0x10:
                   13590:                SREG(ES) = XMS_TOP >> 4;
                   13591:                i386_load_segment_descriptor(ES);
1.1.1.26  root     13592:                REG16(BX) = 0x15;
1.1       root     13593:                break;
1.1.1.44  root     13594:        case 0xe0:
                   13595:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   13596:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   13597:                        break;
                   13598:                }
1.1       root     13599:        default:
1.1.1.22  root     13600:                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));
                   13601:                REG16(AX) = 0x01;
                   13602:                m_CF = 1;
                   13603:                break;
                   13604:        }
                   13605: }
                   13606: 
                   13607: inline void msdos_int_2fh_46h()
                   13608: {
                   13609:        switch(REG8(AL)) {
                   13610:        case 0x80:
1.1.1.29  root     13611:                // Windows v3.0 is not installed
                   13612: //             REG8(AL) = 0x00;
1.1.1.22  root     13613:                break;
                   13614:        default:
                   13615:                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));
                   13616:                REG16(AX) = 0x01;
                   13617:                m_CF = 1;
                   13618:                break;
                   13619:        }
                   13620: }
                   13621: 
                   13622: inline void msdos_int_2fh_48h()
                   13623: {
                   13624:        switch(REG8(AL)) {
                   13625:        case 0x00:
1.1.1.29  root     13626:                // DOSKEY is not installed
                   13627: //             REG8(AL) = 0x00;
1.1.1.22  root     13628:                break;
                   13629:        case 0x10:
                   13630:                msdos_int_21h_0ah();
                   13631:                REG16(AX) = 0x00;
                   13632:                break;
                   13633:        default:
                   13634:                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     13635:                REG16(AX) = 0x01;
1.1.1.3   root     13636:                m_CF = 1;
1.1       root     13637:                break;
                   13638:        }
                   13639: }
                   13640: 
                   13641: inline void msdos_int_2fh_4ah()
                   13642: {
                   13643:        switch(REG8(AL)) {
1.1.1.29  root     13644: #ifdef SUPPORT_HMA
                   13645:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   13646:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   13647:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13648:                                // restore first free mcb in high memory area
                   13649:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13650:                        }
                   13651:                        int offset = 0xffff;
                   13652:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   13653:                                REG16(DI) = offset + 0x10;
                   13654:                        } else {
                   13655:                                REG16(DI) = 0xffff;
                   13656:                        }
                   13657:                } else {
                   13658:                        // HMA is already used
                   13659:                        REG16(BX) = 0;
                   13660:                        REG16(DI) = 0xffff;
                   13661:                }
                   13662:                SREG(ES) = 0xffff;
                   13663:                i386_load_segment_descriptor(ES);
                   13664:                break;
                   13665:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   13666:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   13667:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13668:                                // restore first free mcb in high memory area
                   13669:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13670:                        }
                   13671:                        int size = REG16(BX), offset;
                   13672:                        if((size % 16) != 0) {
                   13673:                                size &= ~15;
                   13674:                                size += 16;
                   13675:                        }
                   13676:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   13677:                                REG16(BX) = size;
                   13678:                                REG16(DI) = offset + 0x10;
                   13679:                                is_hma_used_by_int_2fh = true;
                   13680:                        } else {
                   13681:                                REG16(BX) = 0;
                   13682:                                REG16(DI) = 0xffff;
                   13683:                        }
                   13684:                } else {
                   13685:                        // HMA is already used
                   13686:                        REG16(BX) = 0;
                   13687:                        REG16(DI) = 0xffff;
                   13688:                }
                   13689:                SREG(ES) = 0xffff;
                   13690:                i386_load_segment_descriptor(ES);
                   13691:                break;
                   13692:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   13693:                if(REG8(DL) == 0x00) {
                   13694:                        if(!is_hma_used_by_xms) {
                   13695:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13696:                                        // restore first free mcb in high memory area
                   13697:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13698:                                        is_hma_used_by_int_2fh = false;
                   13699:                                }
                   13700:                                int size = REG16(BX), offset;
                   13701:                                if((size % 16) != 0) {
                   13702:                                        size &= ~15;
                   13703:                                        size += 16;
                   13704:                                }
                   13705:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   13706: //                                     REG16(BX) = size;
                   13707:                                        SREG(ES) = 0xffff;
                   13708:                                        i386_load_segment_descriptor(ES);
                   13709:                                        REG16(DI) = offset + 0x10;
                   13710:                                        is_hma_used_by_int_2fh = true;
                   13711:                                } else {
                   13712:                                        REG16(DI) = 0xffff;
                   13713:                                }
                   13714:                        } else {
                   13715:                                REG16(DI) = 0xffff;
                   13716:                        }
                   13717:                } else if(REG8(DL) == 0x01) {
                   13718:                        if(!is_hma_used_by_xms) {
                   13719:                                int size = REG16(BX);
                   13720:                                if((size % 16) != 0) {
                   13721:                                        size &= ~15;
                   13722:                                        size += 16;
                   13723:                                }
                   13724:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   13725:                                        // memory block address is not changed
                   13726:                                } else {
                   13727:                                        REG16(DI) = 0xffff;
                   13728:                                }
                   13729:                        } else {
                   13730:                                REG16(DI) = 0xffff;
                   13731:                        }
                   13732:                } else if(REG8(DL) == 0x02) {
                   13733:                        if(!is_hma_used_by_xms) {
                   13734:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13735:                                        // restore first free mcb in high memory area
                   13736:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13737:                                        is_hma_used_by_int_2fh = false;
                   13738:                                } else {
                   13739:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   13740:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   13741:                                                is_hma_used_by_int_2fh = false;
                   13742:                                        }
                   13743:                                }
                   13744:                        }
                   13745:                } else {
                   13746:                        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));
                   13747:                        REG16(AX) = 0x01;
                   13748:                        m_CF = 1;
                   13749:                }
                   13750:                break;
                   13751:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   13752:                if(!is_hma_used_by_xms) {
                   13753:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13754:                                // restore first free mcb in high memory area
                   13755:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13756:                                is_hma_used_by_int_2fh = false;
                   13757:                        }
                   13758:                        REG16(AX) = 0x0000;
                   13759:                        SREG(ES) = 0xffff;
                   13760:                        i386_load_segment_descriptor(ES);
                   13761:                        REG16(DI) = 0x10;
                   13762:                }
                   13763:                break;
                   13764: #else
1.1       root     13765:        case 0x01:
                   13766:        case 0x02:
1.1.1.29  root     13767:                // HMA is already used
1.1.1.27  root     13768:                REG16(BX) = 0x0000;
1.1.1.3   root     13769:                SREG(ES) = 0xffff;
                   13770:                i386_load_segment_descriptor(ES);
1.1       root     13771:                REG16(DI) = 0xffff;
                   13772:                break;
1.1.1.19  root     13773:        case 0x03:
                   13774:                // unable to allocate
                   13775:                REG16(DI) = 0xffff;
                   13776:                break;
                   13777:        case 0x04:
                   13778:                // function not supported, do not clear AX
                   13779:                break;
1.1.1.29  root     13780: #endif
                   13781:        case 0x10:
1.1.1.42  root     13782:                switch(REG16(BX)) {
                   13783:                case 0x0000:
                   13784:                case 0x0001:
                   13785:                case 0x0002:
                   13786:                case 0x0003:
                   13787:                case 0x0004:
                   13788:                case 0x0005:
                   13789:                case 0x0006:
                   13790:                case 0x0007:
                   13791:                case 0x0008:
                   13792:                case 0x000a:
                   13793:                case 0x1234:
                   13794:                        // SMARTDRV v4.00+ is not installed
                   13795:                        break;
                   13796:                default:
1.1.1.29  root     13797:                        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));
                   13798:                        REG16(AX) = 0x01;
                   13799:                        m_CF = 1;
1.1.1.42  root     13800:                        break;
1.1.1.29  root     13801:                }
                   13802:                break;
                   13803:        case 0x11:
1.1.1.42  root     13804:                switch(REG16(BX)) {
                   13805:                case 0x0000:
                   13806:                case 0x0001:
                   13807:                case 0x0002:
                   13808:                case 0x0003:
                   13809:                case 0x0004:
                   13810:                case 0x0005:
                   13811:                case 0x0006:
                   13812:                case 0x0007:
                   13813:                case 0x0008:
                   13814:                case 0x0009:
                   13815:                case 0x000a:
                   13816:                case 0x000b:
                   13817:                case 0xfffe:
                   13818:                case 0xffff:
1.1.1.29  root     13819:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     13820:                        break;
                   13821:                default:
                   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:                break;
                   13828:        case 0x12:
                   13829:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   13830:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   13831:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   13832:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     13833:                } else {
                   13834:                        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));
                   13835:                        REG16(AX) = 0x01;
                   13836:                        m_CF = 1;
                   13837:                }
1.1.1.22  root     13838:                break;
1.1.1.42  root     13839:        case 0x13:
                   13840:                // DBLSPACE.BIN is not installed
                   13841:                break;
1.1.1.22  root     13842:        default:
                   13843:                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));
                   13844:                REG16(AX) = 0x01;
                   13845:                m_CF = 1;
                   13846:                break;
                   13847:        }
                   13848: }
                   13849: 
                   13850: inline void msdos_int_2fh_4bh()
                   13851: {
                   13852:        switch(REG8(AL)) {
1.1.1.24  root     13853:        case 0x01:
1.1.1.22  root     13854:        case 0x02:
1.1.1.29  root     13855:                // Task Switcher is not installed
1.1.1.24  root     13856:                break;
                   13857:        case 0x03:
                   13858:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   13859:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     13860:                break;
1.1.1.30  root     13861:        case 0x04:
                   13862:                REG16(BX) = 0x0000; // free switcher id successfully
                   13863:                break;
1.1.1.43  root     13864:        case 0x05:
                   13865:                REG16(BX) = 0x0000; // no instance data chain
                   13866:                SREG(ES) = 0x0000;
                   13867:                i386_load_segment_descriptor(ES);
                   13868:                break;
1.1       root     13869:        default:
1.1.1.22  root     13870:                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     13871:                REG16(AX) = 0x01;
1.1.1.3   root     13872:                m_CF = 1;
1.1       root     13873:                break;
                   13874:        }
                   13875: }
                   13876: 
1.1.1.44  root     13877: inline void msdos_int_2fh_4dh()
                   13878: {
                   13879:        switch(REG8(AL)) {
                   13880:        case 0x00:
                   13881:                // KKCFUNC is not installed ???
                   13882:                break;
                   13883:        default:
                   13884: //             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));
                   13885:                REG16(AX) = 0x01; // invalid function
                   13886:                m_CF = 1;
                   13887:                break;
                   13888:        }
                   13889: }
                   13890: 
1.1       root     13891: inline void msdos_int_2fh_4fh()
                   13892: {
                   13893:        switch(REG8(AL)) {
                   13894:        case 0x00:
1.1.1.29  root     13895:                // BILING is installed
1.1.1.27  root     13896:                REG16(AX) = 0x0000;
                   13897:                REG8(DL) = 0x01;        // major version
                   13898:                REG8(DH) = 0x00;        // minor version
1.1       root     13899:                break;
                   13900:        case 0x01:
1.1.1.27  root     13901:                REG16(AX) = 0x0000;
1.1       root     13902:                REG16(BX) = active_code_page;
                   13903:                break;
                   13904:        default:
1.1.1.22  root     13905:                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));
                   13906:                REG16(AX) = 0x01;
                   13907:                m_CF = 1;
                   13908:                break;
                   13909:        }
                   13910: }
                   13911: 
                   13912: inline void msdos_int_2fh_55h()
                   13913: {
                   13914:        switch(REG8(AL)) {
                   13915:        case 0x00:
                   13916:        case 0x01:
                   13917: //             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));
                   13918:                break;
                   13919:        default:
                   13920:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     13921:                REG16(AX) = 0x01;
1.1.1.3   root     13922:                m_CF = 1;
1.1       root     13923:                break;
                   13924:        }
                   13925: }
                   13926: 
1.1.1.44  root     13927: inline void msdos_int_2fh_56h()
                   13928: {
                   13929:        switch(REG8(AL)) {
                   13930:        case 0x00:
                   13931:                // INTERLNK is not installed
                   13932:                break;
                   13933:        case 0x01:
                   13934:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   13935: //             if(msdos_is_remote_drive(REG8(BH))) {
                   13936: //                     REG8(AL) = 0x00;
                   13937: //             }
                   13938:                break;
                   13939:        default:
                   13940:                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));
                   13941:                REG16(AX) = 0x01;
                   13942:                m_CF = 1;
                   13943:                break;
                   13944:        }
                   13945: }
                   13946: 
1.1.1.24  root     13947: inline void msdos_int_2fh_adh()
                   13948: {
                   13949:        switch(REG8(AL)) {
                   13950:        case 0x00:
1.1.1.29  root     13951:                // DISPLAY.SYS is installed
1.1.1.24  root     13952:                REG8(AL) = 0xff;
                   13953:                REG16(BX) = 0x100; // ???
                   13954:                break;
                   13955:        case 0x01:
                   13956:                active_code_page = REG16(BX);
                   13957:                msdos_nls_tables_update();
                   13958:                REG16(AX) = 0x01;
                   13959:                break;
                   13960:        case 0x02:
                   13961:                REG16(BX) = active_code_page;
                   13962:                break;
                   13963:        case 0x03:
                   13964:                // FIXME
                   13965:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   13966:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   13967:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   13968:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   13969:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   13970:                break;
                   13971:        case 0x80:
                   13972:                break; // keyb.com is not installed
                   13973:        default:
                   13974:                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));
                   13975:                REG16(AX) = 0x01;
                   13976:                m_CF = 1;
                   13977:                break;
                   13978:        }
                   13979: }
                   13980: 
1.1       root     13981: inline void msdos_int_2fh_aeh()
                   13982: {
                   13983:        switch(REG8(AL)) {
                   13984:        case 0x00:
1.1.1.28  root     13985:                // FIXME: we need to check the given command line
                   13986:                REG8(AL) = 0x00; // the command should be executed as usual
                   13987: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     13988:                break;
                   13989:        case 0x01:
                   13990:                {
                   13991:                        char command[MAX_PATH];
                   13992:                        memset(command, 0, sizeof(command));
1.1.1.3   root     13993:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     13994:                        
                   13995:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   13996:                        param->env_seg = 0;
                   13997:                        param->cmd_line.w.l = 44;
                   13998:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13999:                        param->fcb1.w.l = 24;
                   14000:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   14001:                        param->fcb2.w.l = 24;
                   14002:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   14003:                        
                   14004:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   14005:                        
                   14006:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     14007:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   14008:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     14009:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   14010:                        
1.1.1.28  root     14011:                        try {
                   14012:                                msdos_process_exec(command, param, 0);
                   14013:                        } catch(...) {
                   14014:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     14015:                        }
                   14016:                }
                   14017:                break;
                   14018:        default:
1.1.1.22  root     14019:                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     14020:                REG16(AX) = 0x01;
1.1.1.3   root     14021:                m_CF = 1;
1.1       root     14022:                break;
                   14023:        }
                   14024: }
                   14025: 
1.1.1.34  root     14026: inline void msdos_int_2fh_b7h()
                   14027: {
                   14028:        switch(REG8(AL)) {
                   14029:        case 0x00:
                   14030:                // APPEND is not installed
                   14031: //             REG8(AL) = 0x00;
                   14032:                break;
1.1.1.44  root     14033:        case 0x06:
                   14034:                REG16(BX) = 0x0000;
                   14035:                break;
1.1.1.34  root     14036:        case 0x07:
1.1.1.43  root     14037:        case 0x11:
1.1.1.34  root     14038:                // COMMAND.COM calls this service without checking APPEND is installed
                   14039:                break;
                   14040:        default:
                   14041:                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));
                   14042:                REG16(AX) = 0x01;
                   14043:                m_CF = 1;
                   14044:                break;
                   14045:        }
                   14046: }
                   14047: 
1.1.1.24  root     14048: inline void msdos_int_33h_0000h()
                   14049: {
                   14050:        REG16(AX) = 0xffff; // hardware/driver installed
                   14051:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14052: }
                   14053: 
                   14054: inline void msdos_int_33h_0001h()
                   14055: {
1.1.1.34  root     14056:        if(mouse.hidden > 0) {
                   14057:                mouse.hidden--;
                   14058:        }
                   14059:        if(mouse.hidden == 0) {
1.1.1.24  root     14060:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   14061:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   14062:                }
                   14063:                pic[1].imr &= ~0x10;    // enable irq12
                   14064:        }
                   14065: }
                   14066: 
                   14067: inline void msdos_int_33h_0002h()
                   14068: {
1.1.1.34  root     14069:        mouse.hidden++;
                   14070: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
                   14071:        pic[1].imr |= 0x10;     // enable irq12
1.1.1.24  root     14072: }
                   14073: 
                   14074: inline void msdos_int_33h_0003h()
                   14075: {
1.1.1.34  root     14076: //     if(mouse.hidden > 0) {
                   14077:                update_console_input();
                   14078: //     }
1.1.1.24  root     14079:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     14080:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   14081:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   14082: }
                   14083: 
                   14084: inline void msdos_int_33h_0004h()
                   14085: {
                   14086:        mouse.position.x = REG16(CX);
                   14087:        mouse.position.x = REG16(DX);
1.1.1.24  root     14088: }
                   14089: 
                   14090: inline void msdos_int_33h_0005h()
                   14091: {
1.1.1.34  root     14092: //     if(mouse.hidden > 0) {
                   14093:                update_console_input();
                   14094: //     }
1.1.1.24  root     14095:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14096:                int idx = REG16(BX);
1.1.1.34  root     14097:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   14098:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   14099:                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     14100:                mouse.buttons[idx].pressed_times = 0;
                   14101:        } else {
                   14102:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14103:        }
                   14104:        REG16(AX) = mouse.get_buttons();
                   14105: }
                   14106: 
                   14107: inline void msdos_int_33h_0006h()
                   14108: {
1.1.1.34  root     14109: //     if(mouse.hidden > 0) {
                   14110:                update_console_input();
                   14111: //     }
1.1.1.24  root     14112:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14113:                int idx = REG16(BX);
1.1.1.34  root     14114:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   14115:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   14116:                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     14117:                mouse.buttons[idx].released_times = 0;
                   14118:        } else {
                   14119:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14120:        }
                   14121:        REG16(AX) = mouse.get_buttons();
                   14122: }
                   14123: 
                   14124: inline void msdos_int_33h_0007h()
                   14125: {
                   14126:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   14127:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   14128: }
                   14129: 
                   14130: inline void msdos_int_33h_0008h()
                   14131: {
                   14132:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   14133:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   14134: }
                   14135: 
                   14136: inline void msdos_int_33h_0009h()
                   14137: {
                   14138:        mouse.hot_spot[0] = REG16(BX);
                   14139:        mouse.hot_spot[1] = REG16(CX);
                   14140: }
                   14141: 
                   14142: inline void msdos_int_33h_000bh()
                   14143: {
1.1.1.34  root     14144: //     if(mouse.hidden > 0) {
                   14145:                update_console_input();
                   14146: //     }
1.1.1.24  root     14147:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14148:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   14149:        mouse.prev_position.x = mouse.position.x;
                   14150:        mouse.prev_position.y = mouse.position.y;
                   14151:        REG16(CX) = dx;
                   14152:        REG16(DX) = dy;
                   14153: }
                   14154: 
                   14155: inline void msdos_int_33h_000ch()
                   14156: {
                   14157:        mouse.call_mask = REG16(CX);
                   14158:        mouse.call_addr.w.l = REG16(DX);
                   14159:        mouse.call_addr.w.h = SREG(ES);
                   14160: }
                   14161: 
                   14162: inline void msdos_int_33h_000fh()
                   14163: {
                   14164:        mouse.mickey.x = REG16(CX);
                   14165:        mouse.mickey.y = REG16(DX);
                   14166: }
                   14167: 
                   14168: inline void msdos_int_33h_0011h()
                   14169: {
                   14170:        REG16(AX) = 0xffff;
                   14171:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14172: }
                   14173: 
                   14174: inline void msdos_int_33h_0014h()
                   14175: {
                   14176:        UINT16 old_mask = mouse.call_mask;
                   14177:        UINT16 old_ofs = mouse.call_addr.w.l;
                   14178:        UINT16 old_seg = mouse.call_addr.w.h;
                   14179:        
                   14180:        mouse.call_mask = REG16(CX);
                   14181:        mouse.call_addr.w.l = REG16(DX);
                   14182:        mouse.call_addr.w.h = SREG(ES);
                   14183:        
                   14184:        REG16(CX) = old_mask;
                   14185:        REG16(DX) = old_ofs;
                   14186:        SREG(ES) = old_seg;
                   14187:        i386_load_segment_descriptor(ES);
                   14188: }
                   14189: 
                   14190: inline void msdos_int_33h_0015h()
                   14191: {
                   14192:        REG16(BX) = sizeof(mouse);
                   14193: }
                   14194: 
                   14195: inline void msdos_int_33h_0016h()
                   14196: {
                   14197:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   14198: }
                   14199: 
                   14200: inline void msdos_int_33h_0017h()
                   14201: {
                   14202:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   14203: }
                   14204: 
1.1.1.43  root     14205: inline void msdos_int_33h_0018h()
                   14206: {
                   14207:        for(int i = 0; i < 8; i++) {
                   14208:                if(REG16(CX) & (1 << i)) {
                   14209:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   14210:                                // event handler already exists
                   14211:                                REG16(AX) = 0xffff;
                   14212:                                break;
                   14213:                        }
                   14214:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   14215:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   14216:                }
                   14217:        }
                   14218: }
                   14219: 
                   14220: inline void msdos_int_33h_0019h()
                   14221: {
                   14222:        UINT16 call_mask = REG16(CX);
                   14223:        
                   14224:        REG16(CX) = 0;
                   14225:        
                   14226:        for(int i = 0; i < 8; i++) {
                   14227:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   14228:                        for(int j = 0; j < 8; j++) {
                   14229:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   14230:                                        REG16(CX) |= (1 << j);
                   14231:                                }
                   14232:                        }
                   14233:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   14234:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   14235:                        break;
                   14236:                }
                   14237:        }
                   14238: }
                   14239: 
1.1.1.24  root     14240: inline void msdos_int_33h_001ah()
                   14241: {
                   14242:        mouse.sensitivity[0] = REG16(BX);
                   14243:        mouse.sensitivity[1] = REG16(CX);
                   14244:        mouse.sensitivity[2] = REG16(DX);
                   14245: }
                   14246: 
                   14247: inline void msdos_int_33h_001bh()
                   14248: {
                   14249:        REG16(BX) = mouse.sensitivity[0];
                   14250:        REG16(CX) = mouse.sensitivity[1];
                   14251:        REG16(DX) = mouse.sensitivity[2];
                   14252: }
                   14253: 
                   14254: inline void msdos_int_33h_001dh()
                   14255: {
                   14256:        mouse.display_page = REG16(BX);
                   14257: }
                   14258: 
                   14259: inline void msdos_int_33h_001eh()
                   14260: {
                   14261:        REG16(BX) = mouse.display_page;
                   14262: }
                   14263: 
1.1.1.34  root     14264: inline void msdos_int_33h_001fh()
                   14265: {
                   14266:        // from DOSBox
                   14267:        REG16(BX) = 0x0000;
                   14268:        SREG(ES) = 0x0000;
                   14269:        i386_load_segment_descriptor(ES);
                   14270:        mouse.enabled = false;
                   14271:        mouse.old_hidden = mouse.hidden;
                   14272:        mouse.hidden = 1;
                   14273: }
                   14274: 
                   14275: inline void msdos_int_33h_0020h()
                   14276: {
                   14277:        // from DOSBox
                   14278:        mouse.enabled = true;
                   14279:        mouse.hidden = mouse.old_hidden;
                   14280: }
                   14281: 
1.1.1.24  root     14282: inline void msdos_int_33h_0021h()
                   14283: {
                   14284:        REG16(AX) = 0xffff;
                   14285:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14286: }
                   14287: 
                   14288: inline void msdos_int_33h_0022h()
                   14289: {
                   14290:        mouse.language = REG16(BX);
                   14291: }
                   14292: 
                   14293: inline void msdos_int_33h_0023h()
                   14294: {
                   14295:        REG16(BX) = mouse.language;
                   14296: }
                   14297: 
                   14298: inline void msdos_int_33h_0024h()
                   14299: {
                   14300:        REG16(BX) = 0x0805; // V8.05
                   14301:        REG16(CX) = 0x0400; // PS/2
                   14302: }
                   14303: 
                   14304: inline void msdos_int_33h_0026h()
                   14305: {
                   14306:        REG16(BX) = 0x0000;
                   14307:        REG16(CX) = mouse.max_position.x;
                   14308:        REG16(DX) = mouse.max_position.y;
                   14309: }
                   14310: 
                   14311: inline void msdos_int_33h_002ah()
                   14312: {
1.1.1.34  root     14313:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     14314:        REG16(BX) = mouse.hot_spot[0];
                   14315:        REG16(CX) = mouse.hot_spot[1];
                   14316:        REG16(DX) = 4; // PS/2
                   14317: }
                   14318: 
                   14319: inline void msdos_int_33h_0031h()
                   14320: {
                   14321:        REG16(AX) = mouse.min_position.x;
                   14322:        REG16(BX) = mouse.min_position.y;
                   14323:        REG16(CX) = mouse.max_position.x;
                   14324:        REG16(DX) = mouse.max_position.y;
                   14325: }
                   14326: 
                   14327: inline void msdos_int_33h_0032h()
                   14328: {
                   14329:        REG16(AX) = 0;
                   14330: //     REG16(AX) |= 0x8000; // 0025h
                   14331:        REG16(AX) |= 0x4000; // 0026h
                   14332: //     REG16(AX) |= 0x2000; // 0027h
                   14333: //     REG16(AX) |= 0x1000; // 0028h
                   14334: //     REG16(AX) |= 0x0800; // 0029h
                   14335:        REG16(AX) |= 0x0400; // 002ah
                   14336: //     REG16(AX) |= 0x0200; // 002bh
                   14337: //     REG16(AX) |= 0x0100; // 002ch
                   14338: //     REG16(AX) |= 0x0080; // 002dh
                   14339: //     REG16(AX) |= 0x0040; // 002eh
                   14340:        REG16(AX) |= 0x0020; // 002fh
                   14341: //     REG16(AX) |= 0x0010; // 0030h
                   14342:        REG16(AX) |= 0x0008; // 0031h
                   14343:        REG16(AX) |= 0x0004; // 0032h
                   14344: //     REG16(AX) |= 0x0002; // 0033h
                   14345: //     REG16(AX) |= 0x0001; // 0034h
                   14346: }
                   14347: 
1.1.1.19  root     14348: inline void msdos_int_67h_40h()
                   14349: {
                   14350:        if(!support_ems) {
                   14351:                REG8(AH) = 0x84;
                   14352:        } else {
                   14353:                REG8(AH) = 0x00;
                   14354:        }
                   14355: }
                   14356: 
                   14357: inline void msdos_int_67h_41h()
                   14358: {
                   14359:        if(!support_ems) {
                   14360:                REG8(AH) = 0x84;
                   14361:        } else {
                   14362:                REG8(AH) = 0x00;
                   14363:                REG16(BX) = EMS_TOP >> 4;
                   14364:        }
                   14365: }
                   14366: 
                   14367: inline void msdos_int_67h_42h()
                   14368: {
                   14369:        if(!support_ems) {
                   14370:                REG8(AH) = 0x84;
                   14371:        } else {
                   14372:                REG8(AH) = 0x00;
                   14373:                REG16(BX) = free_ems_pages;
                   14374:                REG16(DX) = MAX_EMS_PAGES;
                   14375:        }
                   14376: }
                   14377: 
                   14378: inline void msdos_int_67h_43h()
                   14379: {
                   14380:        if(!support_ems) {
                   14381:                REG8(AH) = 0x84;
                   14382:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   14383:                REG8(AH) = 0x87;
                   14384:        } else if(REG16(BX) > free_ems_pages) {
                   14385:                REG8(AH) = 0x88;
                   14386:        } else if(REG16(BX) == 0) {
                   14387:                REG8(AH) = 0x89;
                   14388:        } else {
1.1.1.31  root     14389:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14390:                        if(!ems_handles[i].allocated) {
                   14391:                                ems_allocate_pages(i, REG16(BX));
                   14392:                                REG8(AH) = 0x00;
                   14393:                                REG16(DX) = i;
                   14394:                                return;
                   14395:                        }
                   14396:                }
                   14397:                REG8(AH) = 0x85;
                   14398:        }
                   14399: }
                   14400: 
                   14401: inline void msdos_int_67h_44h()
                   14402: {
                   14403:        if(!support_ems) {
                   14404:                REG8(AH) = 0x84;
1.1.1.31  root     14405:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14406:                REG8(AH) = 0x83;
                   14407:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   14408:                REG8(AH) = 0x8a;
                   14409: //     } else if(!(REG8(AL) < 4)) {
                   14410: //             REG8(AH) = 0x8b;
                   14411:        } else if(REG16(BX) == 0xffff) {
                   14412:                ems_unmap_page(REG8(AL) & 3);
                   14413:                REG8(AH) = 0x00;
                   14414:        } else {
                   14415:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   14416:                REG8(AH) = 0x00;
                   14417:        }
                   14418: }
                   14419: 
                   14420: inline void msdos_int_67h_45h()
                   14421: {
                   14422:        if(!support_ems) {
                   14423:                REG8(AH) = 0x84;
1.1.1.31  root     14424:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14425:                REG8(AH) = 0x83;
                   14426:        } else {
                   14427:                ems_release_pages(REG16(DX));
                   14428:                REG8(AH) = 0x00;
                   14429:        }
                   14430: }
                   14431: 
                   14432: inline void msdos_int_67h_46h()
                   14433: {
                   14434:        if(!support_ems) {
                   14435:                REG8(AH) = 0x84;
                   14436:        } else {
1.1.1.29  root     14437: //             REG16(AX) = 0x0032; // EMS 3.2
                   14438:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     14439:        }
                   14440: }
                   14441: 
                   14442: inline void msdos_int_67h_47h()
                   14443: {
                   14444:        // NOTE: the map data should be stored in the specified ems page, not process data
                   14445:        process_t *process = msdos_process_info_get(current_psp);
                   14446:        
                   14447:        if(!support_ems) {
                   14448:                REG8(AH) = 0x84;
1.1.1.31  root     14449: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14450: //             REG8(AH) = 0x83;
                   14451:        } else if(process->ems_pages_stored) {
                   14452:                REG8(AH) = 0x8d;
                   14453:        } else {
                   14454:                for(int i = 0; i < 4; i++) {
                   14455:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   14456:                        process->ems_pages[i].page   = ems_pages[i].page;
                   14457:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   14458:                }
                   14459:                process->ems_pages_stored = true;
                   14460:                REG8(AH) = 0x00;
                   14461:        }
                   14462: }
                   14463: 
                   14464: inline void msdos_int_67h_48h()
                   14465: {
                   14466:        // NOTE: the map data should be restored from the specified ems page, not process data
                   14467:        process_t *process = msdos_process_info_get(current_psp);
                   14468:        
                   14469:        if(!support_ems) {
                   14470:                REG8(AH) = 0x84;
1.1.1.31  root     14471: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14472: //             REG8(AH) = 0x83;
                   14473:        } else if(!process->ems_pages_stored) {
                   14474:                REG8(AH) = 0x8e;
                   14475:        } else {
                   14476:                for(int i = 0; i < 4; i++) {
                   14477:                        if(process->ems_pages[i].mapped) {
                   14478:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   14479:                        } else {
                   14480:                                ems_unmap_page(i);
                   14481:                        }
                   14482:                }
                   14483:                process->ems_pages_stored = false;
                   14484:                REG8(AH) = 0x00;
                   14485:        }
                   14486: }
                   14487: 
                   14488: inline void msdos_int_67h_4bh()
                   14489: {
                   14490:        if(!support_ems) {
                   14491:                REG8(AH) = 0x84;
                   14492:        } else {
                   14493:                REG8(AH) = 0x00;
                   14494:                REG16(BX) = 0;
1.1.1.31  root     14495:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14496:                        if(ems_handles[i].allocated) {
                   14497:                                REG16(BX)++;
                   14498:                        }
                   14499:                }
                   14500:        }
                   14501: }
                   14502: 
                   14503: inline void msdos_int_67h_4ch()
                   14504: {
                   14505:        if(!support_ems) {
                   14506:                REG8(AH) = 0x84;
1.1.1.31  root     14507:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14508:                REG8(AH) = 0x83;
                   14509:        } else {
                   14510:                REG8(AH) = 0x00;
                   14511:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   14512:        }
                   14513: }
                   14514: 
                   14515: inline void msdos_int_67h_4dh()
                   14516: {
                   14517:        if(!support_ems) {
                   14518:                REG8(AH) = 0x84;
                   14519:        } else {
                   14520:                REG8(AH) = 0x00;
                   14521:                REG16(BX) = 0;
1.1.1.31  root     14522:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14523:                        if(ems_handles[i].allocated) {
                   14524:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   14525:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   14526:                                REG16(BX)++;
                   14527:                        }
                   14528:                }
                   14529:        }
                   14530: }
                   14531: 
1.1.1.20  root     14532: inline void msdos_int_67h_4eh()
                   14533: {
                   14534:        if(!support_ems) {
                   14535:                REG8(AH) = 0x84;
                   14536:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14537:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   14538:                        // save page map
                   14539:                        for(int i = 0; i < 4; i++) {
                   14540:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   14541:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   14542:                        }
                   14543:                }
                   14544:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14545:                        // restore page map
                   14546:                        for(int i = 0; i < 4; i++) {
                   14547:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   14548:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   14549:                                
1.1.1.31  root     14550:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     14551:                                        ems_map_page(i, handle, page);
                   14552:                                } else {
                   14553:                                        ems_unmap_page(i);
                   14554:                                }
                   14555:                        }
                   14556:                }
                   14557:                REG8(AH) = 0x00;
                   14558:        } else if(REG8(AL) == 0x03) {
                   14559:                REG8(AH) = 0x00;
1.1.1.21  root     14560:                REG8(AL) = 4 * 4;
                   14561:        } else {
1.1.1.22  root     14562:                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     14563:                REG8(AH) = 0x8f;
                   14564:        }
                   14565: }
                   14566: 
                   14567: inline void msdos_int_67h_4fh()
                   14568: {
                   14569:        if(!support_ems) {
                   14570:                REG8(AH) = 0x84;
                   14571:        } else if(REG8(AL) == 0x00) {
                   14572:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   14573:                
                   14574:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   14575:                for(int i = 0; i < count; i++) {
                   14576:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   14577:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   14578:                        
                   14579: //                     if(!(physical < 4)) {
                   14580: //                             REG8(AH) = 0x8b;
                   14581: //                             return;
                   14582: //                     }
                   14583:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     14584:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   14585:                        *(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     14586:                }
                   14587:                REG8(AH) = 0x00;
                   14588:        } else if(REG8(AL) == 0x01) {
                   14589:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   14590:                
                   14591:                for(int i = 0; i < count; i++) {
                   14592:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   14593:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   14594:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   14595:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   14596:                        
                   14597: //                     if(!(physical < 4)) {
                   14598: //                             REG8(AH) = 0x8b;
                   14599: //                             return;
                   14600: //                     } else
1.1.1.41  root     14601:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     14602:                                ems_map_page(physical & 3, handle, logical);
                   14603:                        } else {
1.1.1.41  root     14604:                                ems_unmap_page(physical & 3);
1.1.1.21  root     14605:                        }
                   14606:                }
                   14607:                REG8(AH) = 0x00;
                   14608:        } else if(REG8(AL) == 0x02) {
                   14609:                REG8(AH) = 0x00;
                   14610:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     14611:        } else {
1.1.1.22  root     14612:                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     14613:                REG8(AH) = 0x8f;
                   14614:        }
                   14615: }
                   14616: 
                   14617: inline void msdos_int_67h_50h()
                   14618: {
                   14619:        if(!support_ems) {
                   14620:                REG8(AH) = 0x84;
1.1.1.31  root     14621:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     14622:                REG8(AH) = 0x83;
                   14623:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   14624:                for(int i = 0; i < REG16(CX); i++) {
                   14625:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   14626:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   14627:                        
                   14628:                        if(REG8(AL) == 0x01) {
                   14629:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   14630:                        }
                   14631: //                     if(!(physical < 4)) {
                   14632: //                             REG8(AH) = 0x8b;
                   14633: //                             return;
                   14634: //                     } else
                   14635:                        if(logical == 0xffff) {
                   14636:                                ems_unmap_page(physical & 3);
                   14637:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   14638:                                ems_map_page(physical & 3, REG16(DX), logical);
                   14639:                        } else {
                   14640:                                REG8(AH) = 0x8a;
                   14641:                                return;
                   14642:                        }
                   14643:                }
                   14644:                REG8(AH) = 0x00;
                   14645:        } else {
1.1.1.22  root     14646:                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     14647:                REG8(AH) = 0x8f;
                   14648:        }
                   14649: }
                   14650: 
1.1.1.19  root     14651: inline void msdos_int_67h_51h()
                   14652: {
                   14653:        if(!support_ems) {
                   14654:                REG8(AH) = 0x84;
1.1.1.31  root     14655:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14656:                REG8(AH) = 0x83;
                   14657:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   14658:                REG8(AH) = 0x87;
                   14659:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   14660:                REG8(AH) = 0x88;
                   14661:        } else {
                   14662:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   14663:                REG8(AH) = 0x00;
                   14664:        }
                   14665: }
                   14666: 
1.1.1.20  root     14667: inline void msdos_int_67h_52h()
                   14668: {
                   14669:        if(!support_ems) {
                   14670:                REG8(AH) = 0x84;
1.1.1.31  root     14671: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   14672: //             REG8(AH) = 0x83;
1.1.1.20  root     14673:        } else if(REG8(AL) == 0x00) {
                   14674:                REG8(AL) = 0x00; // handle is volatile
                   14675:                REG8(AH) = 0x00;
                   14676:        } else if(REG8(AL) == 0x01) {
                   14677:                if(REG8(BL) == 0x00) {
                   14678:                        REG8(AH) = 0x00;
                   14679:                } else {
                   14680:                        REG8(AH) = 0x90; // undefined attribute type
                   14681:                }
                   14682:        } else if(REG8(AL) == 0x02) {
                   14683:                REG8(AL) = 0x00; // only volatile handles supported
                   14684:                REG8(AH) = 0x00;
                   14685:        } else {
1.1.1.22  root     14686:                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     14687:                REG8(AH) = 0x8f;
                   14688:        }
                   14689: }
                   14690: 
1.1.1.19  root     14691: inline void msdos_int_67h_53h()
                   14692: {
                   14693:        if(!support_ems) {
                   14694:                REG8(AH) = 0x84;
1.1.1.31  root     14695:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14696:                REG8(AH) = 0x83;
                   14697:        } else if(REG8(AL) == 0x00) {
                   14698:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   14699:                REG8(AH) = 0x00;
                   14700:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     14701:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14702:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   14703:                                REG8(AH) = 0xa1;
                   14704:                                return;
                   14705:                        }
                   14706:                }
                   14707:                REG8(AH) = 0x00;
                   14708:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   14709:        } else {
1.1.1.22  root     14710:                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     14711:                REG8(AH) = 0x8f;
1.1.1.19  root     14712:        }
                   14713: }
                   14714: 
                   14715: inline void msdos_int_67h_54h()
                   14716: {
                   14717:        if(!support_ems) {
                   14718:                REG8(AH) = 0x84;
                   14719:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     14720:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14721:                        if(ems_handles[i].allocated) {
                   14722:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   14723:                        } else {
                   14724:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   14725:                        }
                   14726:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   14727:                }
                   14728:                REG8(AH) = 0x00;
                   14729:                REG8(AL) = MAX_EMS_HANDLES;
                   14730:        } else if(REG8(AL) == 0x01) {
                   14731:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     14732:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14733:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   14734:                                REG8(AH) = 0x00;
                   14735:                                REG16(DX) = i;
                   14736:                                break;
                   14737:                        }
                   14738:                }
                   14739:        } else if(REG8(AL) == 0x02) {
                   14740:                REG8(AH) = 0x00;
                   14741:                REG16(BX) = MAX_EMS_HANDLES;
                   14742:        } else {
1.1.1.22  root     14743:                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     14744:                REG8(AH) = 0x8f;
                   14745:        }
                   14746: }
                   14747: 
                   14748: inline void msdos_int_67h_57h_tmp()
                   14749: {
                   14750:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   14751:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   14752:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   14753:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   14754:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   14755:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   14756:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   14757:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   14758:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   14759:        
1.1.1.32  root     14760:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     14761:        UINT32 src_addr, dest_addr;
                   14762:        UINT32 src_addr_max, dest_addr_max;
                   14763:        
                   14764:        if(src_type == 0) {
                   14765:                src_buffer = mem;
                   14766:                src_addr = (src_seg << 4) + src_ofs;
                   14767:                src_addr_max = MAX_MEM;
                   14768:        } else {
1.1.1.31  root     14769:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     14770:                        REG8(AH) = 0x83;
                   14771:                        return;
                   14772:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   14773:                        REG8(AH) = 0x8a;
                   14774:                        return;
                   14775:                }
1.1.1.32  root     14776:                if(ems_handles[src_handle].buffer != NULL) {
                   14777:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   14778:                }
1.1.1.20  root     14779:                src_addr = src_ofs;
1.1.1.32  root     14780:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     14781:        }
                   14782:        if(dest_type == 0) {
                   14783:                dest_buffer = mem;
                   14784:                dest_addr = (dest_seg << 4) + dest_ofs;
                   14785:                dest_addr_max = MAX_MEM;
                   14786:        } else {
1.1.1.31  root     14787:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     14788:                        REG8(AH) = 0x83;
                   14789:                        return;
                   14790:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   14791:                        REG8(AH) = 0x8a;
                   14792:                        return;
                   14793:                }
1.1.1.32  root     14794:                if(ems_handles[dest_handle].buffer != NULL) {
                   14795:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   14796:                }
1.1.1.20  root     14797:                dest_addr = dest_ofs;
1.1.1.32  root     14798:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     14799:        }
1.1.1.32  root     14800:        if(src_buffer != NULL && dest_buffer != NULL) {
                   14801:                for(int i = 0; i < copy_length; i++) {
                   14802:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   14803:                                if(REG8(AL) == 0x00) {
                   14804:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   14805:                                } else if(REG8(AL) == 0x01) {
                   14806:                                        UINT8 tmp = dest_buffer[dest_addr];
                   14807:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   14808:                                        src_buffer[src_addr++] = tmp;
                   14809:                                }
                   14810:                        } else {
                   14811:                                REG8(AH) = 0x93;
                   14812:                                return;
1.1.1.20  root     14813:                        }
                   14814:                }
1.1.1.32  root     14815:                REG8(AH) = 0x00;
                   14816:        } else {
                   14817:                REG8(AH) = 0x80;
1.1.1.20  root     14818:        }
                   14819: }
                   14820: 
                   14821: inline void msdos_int_67h_57h()
                   14822: {
                   14823:        if(!support_ems) {
                   14824:                REG8(AH) = 0x84;
                   14825:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   14826:                struct {
                   14827:                        UINT16 handle;
                   14828:                        UINT16 page;
                   14829:                        bool mapped;
                   14830:                } tmp_pages[4];
                   14831:                
                   14832:                // unmap pages to copy memory data to ems buffer
                   14833:                for(int i = 0; i < 4; i++) {
                   14834:                        tmp_pages[i].handle = ems_pages[i].handle;
                   14835:                        tmp_pages[i].page   = ems_pages[i].page;
                   14836:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   14837:                        ems_unmap_page(i);
                   14838:                }
                   14839:                
                   14840:                // run move/exchange operation
                   14841:                msdos_int_67h_57h_tmp();
                   14842:                
                   14843:                // restore unmapped pages
                   14844:                for(int i = 0; i < 4; i++) {
                   14845:                        if(tmp_pages[i].mapped) {
                   14846:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   14847:                        }
                   14848:                }
                   14849:        } else {
1.1.1.22  root     14850:                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     14851:                REG8(AH) = 0x8f;
                   14852:        }
                   14853: }
                   14854: 
                   14855: inline void msdos_int_67h_58h()
                   14856: {
                   14857:        if(!support_ems) {
                   14858:                REG8(AH) = 0x84;
                   14859:        } else if(REG8(AL) == 0x00) {
                   14860:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     14861:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   14862:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     14863:                }
                   14864:                REG8(AH) = 0x00;
                   14865:                REG16(CX) = 4;
                   14866:        } else if(REG8(AL) == 0x01) {
                   14867:                REG8(AH) = 0x00;
                   14868:                REG16(CX) = 4;
                   14869:        } else {
1.1.1.22  root     14870:                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     14871:                REG8(AH) = 0x8f;
                   14872:        }
                   14873: }
                   14874: 
1.1.1.42  root     14875: inline void msdos_int_67h_59h()
                   14876: {
                   14877:        if(!support_ems) {
                   14878:                REG8(AH) = 0x84;
                   14879:        } else if(REG8(AL) == 0x00) {
                   14880:                REG8(AH) = 0xa4; // access denied by operating system
                   14881:        } else if(REG8(AL) == 0x01) {
                   14882:                REG8(AH) = 0x00;
                   14883:                REG16(BX) = free_ems_pages;
                   14884:                REG16(DX) = MAX_EMS_PAGES;
                   14885:        } else {
                   14886:                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));
                   14887:                REG8(AH) = 0x8f;
                   14888:        }
                   14889: }
                   14890: 
1.1.1.20  root     14891: inline void msdos_int_67h_5ah()
                   14892: {
                   14893:        if(!support_ems) {
1.1.1.19  root     14894:                REG8(AH) = 0x84;
1.1.1.20  root     14895:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   14896:                REG8(AH) = 0x87;
                   14897:        } else if(REG16(BX) > free_ems_pages) {
                   14898:                REG8(AH) = 0x88;
                   14899: //     } else if(REG16(BX) == 0) {
                   14900: //             REG8(AH) = 0x89;
                   14901:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     14902:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     14903:                        if(!ems_handles[i].allocated) {
                   14904:                                ems_allocate_pages(i, REG16(BX));
                   14905:                                REG8(AH) = 0x00;
                   14906:                                REG16(DX) = i;
                   14907:                                return;
                   14908:                        }
                   14909:                }
                   14910:                REG8(AH) = 0x85;
                   14911:        } else {
1.1.1.22  root     14912:                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     14913:                REG8(AH) = 0x8f;
1.1.1.19  root     14914:        }
                   14915: }
                   14916: 
1.1.1.43  root     14917: inline void msdos_int_67h_5dh()
                   14918: {
                   14919:        if(!support_ems) {
                   14920:                REG8(AH) = 0x84;
                   14921:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14922:                REG8(AH) = 0xa4; // operating system denied access
                   14923:        } else {
                   14924:                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));
                   14925:                REG8(AH) = 0x8f;
                   14926:        }
                   14927: }
                   14928: 
1.1.1.30  root     14929: inline void msdos_int_67h_deh()
                   14930: {
                   14931:        REG8(AH) = 0x84;
                   14932: }
                   14933: 
1.1.1.19  root     14934: #ifdef SUPPORT_XMS
                   14935: 
1.1.1.32  root     14936: void msdos_xms_init()
1.1.1.26  root     14937: {
1.1.1.30  root     14938:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   14939:        emb_handle_top->address = EMB_TOP;
                   14940:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     14941:        xms_a20_local_enb_count = 0;
                   14942: }
                   14943: 
1.1.1.32  root     14944: void msdos_xms_finish()
                   14945: {
                   14946:        msdos_xms_release();
                   14947: }
                   14948: 
                   14949: void msdos_xms_release()
1.1.1.30  root     14950: {
                   14951:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   14952:                emb_handle_t *next_handle = emb_handle->next;
                   14953:                free(emb_handle);
                   14954:                emb_handle = next_handle;
                   14955:        }
                   14956: }
                   14957: 
                   14958: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   14959: {
                   14960:        if(handle != 0) {
                   14961:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   14962:                        if(emb_handle->handle == handle) {
                   14963:                                return(emb_handle);
                   14964:                        }
                   14965:                }
                   14966:        }
                   14967:        return(NULL);
                   14968: }
                   14969: 
                   14970: int msdos_xms_get_unused_emb_handle_id()
                   14971: {
                   14972:        for(int handle = 1;; handle++) {
                   14973:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   14974:                        return(handle);
                   14975:                }
                   14976:        }
                   14977:        return(0);
                   14978: }
                   14979: 
                   14980: int msdos_xms_get_unused_emb_handle_count()
                   14981: {
                   14982:        int count = 64; //255;
                   14983:        
                   14984:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   14985:                if(emb_handle->handle != 0) {
                   14986:                        if(--count == 1) {
                   14987:                                break;
                   14988:                        }
                   14989:                }
                   14990:        }
                   14991:        return(count);
                   14992: }
                   14993: 
                   14994: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   14995: {
                   14996:        if(emb_handle->size_kb > size_kb) {
                   14997:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   14998:                
                   14999:                new_handle->address = emb_handle->address + size_kb * 1024;
                   15000:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   15001:                emb_handle->size_kb = size_kb;
                   15002:                
                   15003:                new_handle->prev = emb_handle;
                   15004:                new_handle->next = emb_handle->next;
                   15005:                if(emb_handle->next != NULL) {
                   15006:                        emb_handle->next->prev = new_handle;
                   15007:                }
                   15008:                emb_handle->next = new_handle;
                   15009:        }
                   15010: }
                   15011: 
                   15012: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   15013: {
                   15014:        emb_handle_t *next_handle = emb_handle->next;
                   15015:        
                   15016:        if(next_handle != NULL) {
                   15017:                emb_handle->size_kb += next_handle->size_kb;
                   15018:                
                   15019:                if(next_handle->next != NULL) {
                   15020:                        next_handle->next->prev = emb_handle;
                   15021:                }
                   15022:                emb_handle->next = next_handle->next;
                   15023:                free(next_handle);
                   15024:        }
                   15025: }
                   15026: 
                   15027: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   15028: {
                   15029:        emb_handle_t *target_handle = NULL;
                   15030:        
                   15031:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15032:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   15033:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   15034:                                target_handle = emb_handle;
                   15035:                        }
                   15036:                }
                   15037:        }
                   15038:        if(target_handle != NULL) {
                   15039:                if(target_handle->size_kb > size_kb) {
                   15040:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   15041:                }
                   15042: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   15043:                return(target_handle);
                   15044:        }
                   15045:        return(NULL);
                   15046: }
                   15047: 
                   15048: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   15049: {
                   15050:        emb_handle_t *prev_handle = emb_handle->prev;
                   15051:        emb_handle_t *next_handle = emb_handle->next;
                   15052:        
                   15053:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   15054:                msdos_xms_combine_emb_handles(prev_handle);
                   15055:                emb_handle = prev_handle;
                   15056:        }
                   15057:        if(next_handle != NULL && next_handle->handle == 0) {
                   15058:                msdos_xms_combine_emb_handles(emb_handle);
                   15059:        }
                   15060:        emb_handle->handle = 0;
                   15061: }
                   15062: 
1.1.1.19  root     15063: inline void msdos_call_xms_00h()
                   15064: {
1.1.1.29  root     15065: #if defined(HAS_I386)
                   15066:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.45! root     15067: //     REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
1.1.1.29  root     15068:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   15069: #else
                   15070:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   15071:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   15072: #endif
                   15073: //     REG16(DX) = 0x0000; // HMA does not exist
                   15074:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     15075: }
                   15076: 
                   15077: inline void msdos_call_xms_01h()
                   15078: {
1.1.1.29  root     15079:        if(REG8(AL) == 0x40) {
                   15080:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   15081:                // DX=KB free extended memory returned by last call of function 08h
                   15082:                REG16(AX) = 0x0000;
                   15083:                REG8(BL) = 0x91;
                   15084:                REG16(DX) = xms_dx_after_call_08h;
                   15085:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   15086:                REG16(AX) = 0x0000;
                   15087:                REG8(BL) = 0x81; // Vdisk was detected
                   15088: #ifdef SUPPORT_HMA
                   15089:        } else if(is_hma_used_by_int_2fh) {
                   15090:                REG16(AX) = 0x0000;
                   15091:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   15092:        } else if(is_hma_used_by_xms) {
                   15093:                REG16(AX) = 0x0000;
                   15094:                REG8(BL) = 0x91; // HMA is already in use
                   15095:        } else {
                   15096:                REG16(AX) = 0x0001;
                   15097:                is_hma_used_by_xms = true;
                   15098: #else
                   15099:        } else {
                   15100:                REG16(AX) = 0x0000;
                   15101:                REG8(BL) = 0x91; // HMA is already in use
                   15102: #endif
                   15103:        }
1.1.1.19  root     15104: }
                   15105: 
                   15106: inline void msdos_call_xms_02h()
                   15107: {
1.1.1.29  root     15108:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   15109:                REG16(AX) = 0x0000;
                   15110:                REG8(BL) = 0x81; // Vdisk was detected
                   15111: #ifdef SUPPORT_HMA
                   15112:        } else if(is_hma_used_by_int_2fh) {
                   15113:                REG16(AX) = 0x0000;
                   15114:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   15115:        } else if(!is_hma_used_by_xms) {
                   15116:                REG16(AX) = 0x0000;
                   15117:                REG8(BL) = 0x93; // HMA is not allocated
                   15118:        } else {
                   15119:                REG16(AX) = 0x0001;
                   15120:                is_hma_used_by_xms = false;
                   15121:                // restore first free mcb in high memory area
                   15122:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   15123: #else
                   15124:        } else {
                   15125:                REG16(AX) = 0x0000;
                   15126:                REG8(BL) = 0x91; // HMA is already in use
                   15127: #endif
                   15128:        }
1.1.1.19  root     15129: }
                   15130: 
                   15131: inline void msdos_call_xms_03h()
                   15132: {
                   15133:        i386_set_a20_line(1);
                   15134:        REG16(AX) = 0x0001;
                   15135:        REG8(BL) = 0x00;
                   15136: }
                   15137: 
                   15138: inline void msdos_call_xms_04h()
                   15139: {
1.1.1.21  root     15140:        i386_set_a20_line(0);
                   15141:        REG16(AX) = 0x0001;
                   15142:        REG8(BL) = 0x00;
1.1.1.19  root     15143: }
                   15144: 
                   15145: inline void msdos_call_xms_05h()
                   15146: {
                   15147:        i386_set_a20_line(1);
                   15148:        REG16(AX) = 0x0001;
                   15149:        REG8(BL) = 0x00;
1.1.1.21  root     15150:        xms_a20_local_enb_count++;
1.1.1.19  root     15151: }
                   15152: 
                   15153: void msdos_call_xms_06h()
                   15154: {
1.1.1.21  root     15155:        if(xms_a20_local_enb_count > 0) {
1.1.1.45! root     15156:                if(--xms_a20_local_enb_count == 0) {
        !          15157:                        i386_set_a20_line(0);
        !          15158:                        REG16(AX) = 0x0001;
        !          15159:                        REG8(BL) = 0x00;
        !          15160:                } else {
        !          15161:                        REG16(AX) = 0x0000;
        !          15162:                        REG8(BL) = 0x94;
        !          15163:                }
1.1.1.21  root     15164:        } else {
1.1.1.45! root     15165:                i386_set_a20_line(0);
1.1.1.21  root     15166:                REG16(AX) = 0x0001;
                   15167:                REG8(BL) = 0x00;
1.1.1.19  root     15168:        }
                   15169: }
                   15170: 
                   15171: inline void msdos_call_xms_07h()
                   15172: {
                   15173:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   15174:        REG8(BL) = 0x00;
                   15175: }
                   15176: 
                   15177: inline void msdos_call_xms_08h()
                   15178: {
1.1.1.45! root     15179:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     15180:        
1.1.1.30  root     15181:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15182:                if(emb_handle->handle == 0) {
1.1.1.45! root     15183:                        if(eax < emb_handle->size_kb) {
        !          15184:                                eax = emb_handle->size_kb;
1.1.1.19  root     15185:                        }
1.1.1.45! root     15186:                        edx += emb_handle->size_kb;
1.1.1.19  root     15187:                }
                   15188:        }
1.1.1.45! root     15189:        if(eax > 65535) {
        !          15190:                eax = 65535;
        !          15191:        }
        !          15192:        if(edx > 65535) {
        !          15193:                edx = 65535;
        !          15194:        }
        !          15195:        if(eax == 0 && edx == 0) {
1.1.1.19  root     15196:                REG8(BL) = 0xa0;
                   15197:        } else {
                   15198:                REG8(BL) = 0x00;
                   15199:        }
1.1.1.45! root     15200: #if defined(HAS_I386)
        !          15201:        REG32(EAX) = eax;
        !          15202:        REG32(EDX) = edx;
        !          15203: #else
        !          15204:        REG16(AX) = (UINT16)eax;
        !          15205:        REG16(DX) = (UINT16)edx;
        !          15206: #endif
1.1.1.29  root     15207:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     15208: }
                   15209: 
1.1.1.30  root     15210: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     15211: {
1.1.1.30  root     15212:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   15213:        
                   15214:        if(emb_handle != NULL) {
                   15215:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   15216:                
                   15217:                REG16(AX) = 0x0001;
                   15218:                REG16(DX) = emb_handle->handle;
                   15219:                REG8(BL) = 0x00;
                   15220:        } else {
                   15221:                REG16(AX) = REG16(DX) = 0x0000;
                   15222:                REG8(BL) = 0xa0;
1.1.1.19  root     15223:        }
1.1.1.30  root     15224: }
                   15225: 
                   15226: inline void msdos_call_xms_09h()
                   15227: {
                   15228:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     15229: }
                   15230: 
                   15231: inline void msdos_call_xms_0ah()
                   15232: {
1.1.1.30  root     15233:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15234:        
                   15235:        if(emb_handle == NULL) {
1.1.1.19  root     15236:                REG16(AX) = 0x0000;
                   15237:                REG8(BL) = 0xa2;
1.1.1.45! root     15238: //     } else if(emb_handle->lock > 0) {
        !          15239: //             REG16(AX) = 0x0000;
        !          15240: //             REG8(BL) = 0xab;
1.1.1.19  root     15241:        } else {
1.1.1.30  root     15242:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     15243:                
                   15244:                REG16(AX) = 0x0001;
                   15245:                REG8(BL) = 0x00;
                   15246:        }
                   15247: }
                   15248: 
                   15249: inline void msdos_call_xms_0bh()
                   15250: {
                   15251:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15252:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15253:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   15254:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   15255:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15256:        
                   15257:        UINT8 *src_buffer, *dest_buffer;
                   15258:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     15259:        emb_handle_t *emb_handle;
1.1.1.19  root     15260:        
                   15261:        if(src_handle == 0) {
                   15262:                src_buffer = mem;
                   15263:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   15264:                src_addr_max = MAX_MEM;
                   15265:        } else {
1.1.1.30  root     15266:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     15267:                        REG16(AX) = 0x0000;
                   15268:                        REG8(BL) = 0xa3;
                   15269:                        return;
                   15270:                }
1.1.1.30  root     15271:                src_buffer = mem + emb_handle->address;
                   15272:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     15273:        }
                   15274:        if(dest_handle == 0) {
                   15275:                dest_buffer = mem;
                   15276:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   15277:                dest_addr_max = MAX_MEM;
                   15278:        } else {
1.1.1.30  root     15279:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     15280:                        REG16(AX) = 0x0000;
                   15281:                        REG8(BL) = 0xa5;
                   15282:                        return;
                   15283:                }
1.1.1.30  root     15284:                dest_buffer = mem + emb_handle->address;
                   15285:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     15286:        }
                   15287:        for(int i = 0; i < copy_length; i++) {
                   15288:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   15289:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   15290:                } else {
                   15291:                        break;
                   15292:                }
                   15293:        }
                   15294:        REG16(AX) = 0x0001;
                   15295:        REG8(BL) = 0x00;
                   15296: }
                   15297: 
                   15298: inline void msdos_call_xms_0ch()
                   15299: {
1.1.1.30  root     15300:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15301:        
                   15302:        if(emb_handle == NULL) {
1.1.1.19  root     15303:                REG16(AX) = 0x0000;
                   15304:                REG8(BL) = 0xa2;
                   15305:        } else {
1.1.1.45! root     15306:                if(emb_handle->lock < 255) {
        !          15307:                        emb_handle->lock++;
        !          15308:                }
1.1.1.19  root     15309:                REG16(AX) = 0x0001;
1.1.1.30  root     15310:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   15311:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     15312:        }
                   15313: }
                   15314: 
                   15315: inline void msdos_call_xms_0dh()
                   15316: {
1.1.1.30  root     15317:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15318:        
                   15319:        if(emb_handle == NULL) {
1.1.1.19  root     15320:                REG16(AX) = 0x0000;
                   15321:                REG8(BL) = 0xa2;
1.1.1.30  root     15322:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     15323:                REG16(AX) = 0x0000;
                   15324:                REG8(BL) = 0xaa;
                   15325:        } else {
1.1.1.30  root     15326:                emb_handle->lock--;
1.1.1.19  root     15327:                REG16(AX) = 0x0001;
                   15328:                REG8(BL) = 0x00;
                   15329:        }
                   15330: }
                   15331: 
                   15332: inline void msdos_call_xms_0eh()
                   15333: {
1.1.1.30  root     15334:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15335:        
                   15336:        if(emb_handle == NULL) {
1.1.1.19  root     15337:                REG16(AX) = 0x0000;
                   15338:                REG8(BL) = 0xa2;
                   15339:        } else {
                   15340:                REG16(AX) = 0x0001;
1.1.1.30  root     15341:                REG8(BH) = emb_handle->lock;
                   15342:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   15343:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     15344:        }
                   15345: }
                   15346: 
1.1.1.30  root     15347: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     15348: {
1.1.1.30  root     15349:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15350:        
                   15351:        if(emb_handle == NULL) {
1.1.1.19  root     15352:                REG16(AX) = 0x0000;
                   15353:                REG8(BL) = 0xa2;
1.1.1.30  root     15354:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     15355:                REG16(AX) = 0x0000;
                   15356:                REG8(BL) = 0xab;
                   15357:        } else {
1.1.1.30  root     15358:                if(emb_handle->size_kb < size_kb) {
                   15359:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   15360:                                msdos_xms_combine_emb_handles(emb_handle);
                   15361:                                if(emb_handle->size_kb > size_kb) {
                   15362:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   15363:                                }
                   15364:                        } else {
                   15365:                                int old_handle = emb_handle->handle;
                   15366:                                int old_size_kb = emb_handle->size_kb;
                   15367:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   15368:                                
                   15369:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   15370:                                msdos_xms_free_emb_handle(emb_handle);
                   15371:                                
                   15372:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   15373:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   15374:                                }
                   15375:                                emb_handle->handle = old_handle;
                   15376:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   15377:                                free(buffer);
                   15378:                        }
                   15379:                } else if(emb_handle->size_kb > size_kb) {
                   15380:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   15381:                }
                   15382:                if(emb_handle->size_kb != size_kb) {
                   15383:                        REG16(AX) = 0x0000;
                   15384:                        REG8(BL) = 0xa0;
                   15385:                } else {
                   15386:                        REG16(AX) = 0x0001;
                   15387:                        REG8(BL) = 0x00;
                   15388:                }
1.1.1.19  root     15389:        }
                   15390: }
                   15391: 
1.1.1.30  root     15392: inline void msdos_call_xms_0fh()
                   15393: {
                   15394:        msdos_call_xms_0fh(REG16(BX));
                   15395: }
                   15396: 
1.1.1.19  root     15397: inline void msdos_call_xms_10h()
                   15398: {
                   15399:        int seg;
                   15400:        
                   15401:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   15402:                REG16(AX) = 0x0001;
                   15403:                REG16(BX) = seg;
                   15404:        } else {
                   15405:                REG16(AX) = 0x0000;
                   15406:                REG8(BL) = 0xb0;
                   15407:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   15408:        }
                   15409: }
                   15410: 
                   15411: inline void msdos_call_xms_11h()
                   15412: {
                   15413:        int mcb_seg = REG16(DX) - 1;
                   15414:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   15415:        
                   15416:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   15417:                msdos_mem_free(REG16(DX));
                   15418:                REG16(AX) = 0x0001;
                   15419:                REG8(BL) = 0x00;
                   15420:        } else {
                   15421:                REG16(AX) = 0x0000;
                   15422:                REG8(BL) = 0xb2;
                   15423:        }
                   15424: }
                   15425: 
                   15426: inline void msdos_call_xms_12h()
                   15427: {
                   15428:        int mcb_seg = REG16(DX) - 1;
                   15429:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   15430:        int max_paragraphs;
                   15431:        
                   15432:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   15433:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   15434:                        REG16(AX) = 0x0001;
                   15435:                        REG8(BL) = 0x00;
                   15436:                } else {
                   15437:                        REG16(AX) = 0x0000;
                   15438:                        REG8(BL) = 0xb0;
                   15439:                        REG16(DX) = max_paragraphs;
                   15440:                }
                   15441:        } else {
                   15442:                REG16(AX) = 0x0000;
                   15443:                REG8(BL) = 0xb2;
                   15444:        }
                   15445: }
                   15446: 
1.1.1.29  root     15447: #if defined(HAS_I386)
                   15448: 
                   15449: inline void msdos_call_xms_88h()
                   15450: {
                   15451:        REG32(EAX) = REG32(EDX) = 0x0000;
                   15452:        
1.1.1.30  root     15453:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15454:                if(emb_handle->handle == 0) {
                   15455:                        if(REG32(EAX) < emb_handle->size_kb) {
                   15456:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     15457:                        }
1.1.1.30  root     15458:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     15459:                }
                   15460:        }
                   15461:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   15462:                REG8(BL) = 0xa0;
                   15463:        } else {
                   15464:                REG8(BL) = 0x00;
                   15465:        }
                   15466:        REG32(ECX) = EMB_END - 1;
                   15467: }
                   15468: 
                   15469: inline void msdos_call_xms_89h()
                   15470: {
1.1.1.30  root     15471:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     15472: }
                   15473: 
                   15474: inline void msdos_call_xms_8eh()
                   15475: {
1.1.1.30  root     15476:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15477:        
                   15478:        if(emb_handle == NULL) {
1.1.1.29  root     15479:                REG16(AX) = 0x0000;
                   15480:                REG8(BL) = 0xa2;
                   15481:        } else {
                   15482:                REG16(AX) = 0x0001;
1.1.1.30  root     15483:                REG8(BH) = emb_handle->lock;
                   15484:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   15485:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     15486:        }
                   15487: }
                   15488: 
                   15489: inline void msdos_call_xms_8fh()
                   15490: {
1.1.1.30  root     15491:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     15492: }
                   15493: 
                   15494: #endif
1.1.1.19  root     15495: #endif
                   15496: 
1.1.1.26  root     15497: UINT16 msdos_get_equipment()
                   15498: {
                   15499:        static UINT16 equip = 0;
                   15500:        
                   15501:        if(equip == 0) {
                   15502: #ifdef SUPPORT_FPU
                   15503:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   15504: #endif
                   15505:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   15506:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   15507: //             equip |= (1 << 8);      // 0 if DMA installed
                   15508:                equip |= (2 << 9);      // number of serial ports
                   15509:                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     15510:                
                   15511:                // check only A: and B: if it is floppy drive
                   15512:                int n = 0;
                   15513:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     15514:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   15515:                                n++;
1.1.1.28  root     15516:                        }
                   15517:                }
                   15518:                if(n != 0) {
                   15519:                        equip |= (1 << 0);      // floppy disk(s) installed
                   15520:                        n--;
                   15521:                        equip |= (n << 6);      // number of floppies installed less 1
                   15522:                }
                   15523: //             if(joyGetNumDevs() != 0) {
                   15524: //                     equip |= (1 << 12);     // game port installed
                   15525: //             }
1.1.1.26  root     15526:        }
                   15527:        return(equip);
                   15528: }
                   15529: 
1.1       root     15530: void msdos_syscall(unsigned num)
                   15531: {
1.1.1.22  root     15532: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     15533:        if(num == 0x08 || num == 0x1c) {
                   15534:                // don't log the timer interrupts
1.1.1.45! root     15535: //             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.43  root     15536:        } else if(num == 0x68) {
1.1.1.22  root     15537:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     15538:                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     15539:        } else if(num == 0x69) {
                   15540:                // dummy interrupt for XMS (call far)
1.1.1.33  root     15541:                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.45! root     15542:        } else if(num >= 0x6a && num < 0x70) {
        !          15543:                // dummy interrupt
1.1.1.22  root     15544:        } else {
1.1.1.33  root     15545:                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     15546:        }
                   15547: #endif
1.1.1.36  root     15548:        // update cursor position
                   15549:        if(cursor_moved) {
                   15550:                pcbios_update_cursor_position();
                   15551:                cursor_moved = false;
                   15552:        }
1.1.1.33  root     15553:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     15554:        
1.1       root     15555:        switch(num) {
                   15556:        case 0x00:
1.1.1.28  root     15557:                try {
                   15558:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   15559:                        error("division by zero\n");
                   15560:                } catch(...) {
                   15561:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   15562:                }
1.1       root     15563:                break;
                   15564:        case 0x04:
1.1.1.28  root     15565:                try {
                   15566:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   15567:                        error("overflow\n");
                   15568:                } catch(...) {
                   15569:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   15570:                }
1.1       root     15571:                break;
                   15572:        case 0x06:
                   15573:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     15574:                if(!ignore_illegal_insn) {
1.1.1.28  root     15575:                        try {
                   15576:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   15577:                                error("illegal instruction\n");
                   15578:                        } catch(...) {
                   15579:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   15580:                        }
1.1.1.14  root     15581:                } else {
                   15582: #if defined(HAS_I386)
1.1.1.39  root     15583:                        m_eip = m_int6h_skip_eip;
                   15584: #elif defined(HAS_I286)
                   15585:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     15586: #else
1.1.1.39  root     15587:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     15588: #endif
                   15589:                }
1.1       root     15590:                break;
1.1.1.33  root     15591:        case 0x09:
                   15592:                // ctrl-break is pressed
                   15593:                if(raise_int_1bh) {
                   15594: #if defined(HAS_I386)
                   15595:                        m_ext = 0; // not an external interrupt
                   15596:                        i386_trap(0x1b, 1, 0);
                   15597:                        m_ext = 1;
                   15598: #else
                   15599:                        PREFIX86(_interrupt)(0x1b);
                   15600: #endif
                   15601:                        raise_int_1bh = false;
                   15602:                }
1.1.1.8   root     15603:        case 0x08:
1.1.1.14  root     15604: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     15605:        case 0x0b:
                   15606:        case 0x0c:
                   15607:        case 0x0d:
                   15608:        case 0x0e:
                   15609:        case 0x0f:
                   15610:                // EOI
                   15611:                pic[0].isr &= ~(1 << (num - 0x08));
                   15612:                pic_update();
                   15613:                break;
1.1       root     15614:        case 0x10:
                   15615:                // PC BIOS - Video
1.1.1.14  root     15616:                if(!restore_console_on_exit) {
1.1.1.15  root     15617:                        change_console_size(scr_width, scr_height);
1.1       root     15618:                }
1.1.1.3   root     15619:                m_CF = 0;
1.1       root     15620:                switch(REG8(AH)) {
1.1.1.16  root     15621:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     15622:                case 0x01: pcbios_int_10h_01h(); break;
                   15623:                case 0x02: pcbios_int_10h_02h(); break;
                   15624:                case 0x03: pcbios_int_10h_03h(); break;
                   15625:                case 0x05: pcbios_int_10h_05h(); break;
                   15626:                case 0x06: pcbios_int_10h_06h(); break;
                   15627:                case 0x07: pcbios_int_10h_07h(); break;
                   15628:                case 0x08: pcbios_int_10h_08h(); break;
                   15629:                case 0x09: pcbios_int_10h_09h(); break;
                   15630:                case 0x0a: pcbios_int_10h_0ah(); break;
                   15631:                case 0x0b: break;
1.1.1.40  root     15632:                case 0x0c: pcbios_int_10h_0ch(); break;
                   15633:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     15634:                case 0x0e: pcbios_int_10h_0eh(); break;
                   15635:                case 0x0f: pcbios_int_10h_0fh(); break;
                   15636:                case 0x10: break;
1.1.1.14  root     15637:                case 0x11: pcbios_int_10h_11h(); break;
                   15638:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     15639:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     15640:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     15641:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     15642:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   15643:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     15644:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     15645:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   15646:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     15647:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     15648:                case 0x6f: break;
1.1.1.22  root     15649:                case 0x80: m_CF = 1; break; // unknown
                   15650:                case 0x81: m_CF = 1; break; // unknown
1.1       root     15651:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     15652:                case 0x83: pcbios_int_10h_83h(); break;
                   15653:                case 0x8b: break;
                   15654:                case 0x8c: m_CF = 1; break; // unknown
                   15655:                case 0x8d: m_CF = 1; break; // unknown
                   15656:                case 0x8e: m_CF = 1; break; // unknown
                   15657:                case 0x90: pcbios_int_10h_90h(); break;
                   15658:                case 0x91: pcbios_int_10h_91h(); break;
                   15659:                case 0x92: break;
                   15660:                case 0x93: break;
                   15661:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     15662:                case 0xfa: break; // ega register interface library is not installed
1.1       root     15663:                case 0xfe: pcbios_int_10h_feh(); break;
                   15664:                case 0xff: pcbios_int_10h_ffh(); break;
                   15665:                default:
1.1.1.22  root     15666:                        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));
                   15667:                        m_CF = 1;
1.1       root     15668:                        break;
                   15669:                }
                   15670:                break;
                   15671:        case 0x11:
                   15672:                // PC BIOS - Get Equipment List
1.1.1.26  root     15673:                REG16(AX) = msdos_get_equipment();
1.1       root     15674:                break;
                   15675:        case 0x12:
                   15676:                // PC BIOS - Get Memory Size
1.1.1.33  root     15677:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     15678:                break;
                   15679:        case 0x13:
1.1.1.42  root     15680:                // PC BIOS - Disk I/O
                   15681:                {
                   15682:                        static UINT8 last = 0x00;
                   15683:                        switch(REG8(AH)) {
                   15684:                        case 0x00: pcbios_int_13h_00h(); break;
                   15685:                        case 0x01: // get last status
                   15686:                                REG8(AH) = last;
                   15687:                                break;
                   15688:                        case 0x02: pcbios_int_13h_02h(); break;
                   15689:                        case 0x03: pcbios_int_13h_03h(); break;
                   15690:                        case 0x04: pcbios_int_13h_04h(); break;
                   15691:                        case 0x08: pcbios_int_13h_08h(); break;
                   15692:                        case 0x0a: pcbios_int_13h_02h(); break;
                   15693:                        case 0x0b: pcbios_int_13h_03h(); break;
                   15694:                        case 0x0d: pcbios_int_13h_00h(); break;
                   15695:                        case 0x10: pcbios_int_13h_10h(); break;
                   15696:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     15697:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     15698:                        case 0x05: // format
                   15699:                        case 0x06:
                   15700:                        case 0x07:
                   15701:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   15702:                                m_CF = 1;
                   15703:                                break;
                   15704:                        case 0x09:
                   15705:                        case 0x0c: // seek
                   15706:                        case 0x11: // recalib
                   15707:                        case 0x14:
                   15708:                        case 0x17:
                   15709:                                REG8(AH) = 0x00; // successful completion
                   15710:                                break;
1.1.1.43  root     15711:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   15712:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   15713:                                REG8(AH) = 0x01; // invalid function
                   15714:                                m_CF = 1;
                   15715:                                break;
1.1.1.42  root     15716:                        default:
                   15717:                                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));
                   15718:                                REG8(AH) = 0x01; // invalid function
                   15719:                                m_CF = 1;
                   15720:                                break;
                   15721:                        }
                   15722:                        last = REG8(AH);
                   15723:                }
1.1       root     15724:                break;
                   15725:        case 0x14:
                   15726:                // PC BIOS - Serial I/O
1.1.1.25  root     15727:                switch(REG8(AH)) {
                   15728:                case 0x00: pcbios_int_14h_00h(); break;
                   15729:                case 0x01: pcbios_int_14h_01h(); break;
                   15730:                case 0x02: pcbios_int_14h_02h(); break;
                   15731:                case 0x03: pcbios_int_14h_03h(); break;
                   15732:                case 0x04: pcbios_int_14h_04h(); break;
                   15733:                case 0x05: pcbios_int_14h_05h(); break;
                   15734:                default:
                   15735:                        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));
                   15736:                        break;
                   15737:                }
1.1       root     15738:                break;
                   15739:        case 0x15:
                   15740:                // PC BIOS
1.1.1.3   root     15741:                m_CF = 0;
1.1       root     15742:                switch(REG8(AH)) {
1.1.1.14  root     15743:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     15744:                case 0x23: pcbios_int_15h_23h(); break;
                   15745:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     15746:                case 0x41: break;
1.1       root     15747:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     15748:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     15749:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     15750:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     15751:                case 0x86: pcbios_int_15h_86h(); break;
                   15752:                case 0x87: pcbios_int_15h_87h(); break;
                   15753:                case 0x88: pcbios_int_15h_88h(); break;
                   15754:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     15755:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     15756:                case 0xc0: // PS/2 ???
                   15757:                case 0xc1:
                   15758:                case 0xc2:
1.1.1.30  root     15759:                case 0xc3: // PS50+ ???
                   15760:                case 0xc4:
1.1.1.22  root     15761:                        REG8(AH) = 0x86;
                   15762:                        m_CF = 1;
                   15763:                        break;
1.1.1.3   root     15764: #if defined(HAS_I386)
1.1       root     15765:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     15766: #endif
1.1       root     15767:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     15768:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     15769:                default:
1.1.1.22  root     15770:                        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));
                   15771:                        REG8(AH) = 0x86;
1.1.1.3   root     15772:                        m_CF = 1;
1.1       root     15773:                        break;
                   15774:                }
                   15775:                break;
                   15776:        case 0x16:
                   15777:                // PC BIOS - Keyboard
1.1.1.3   root     15778:                m_CF = 0;
1.1       root     15779:                switch(REG8(AH)) {
                   15780:                case 0x00: pcbios_int_16h_00h(); break;
                   15781:                case 0x01: pcbios_int_16h_01h(); break;
                   15782:                case 0x02: pcbios_int_16h_02h(); break;
                   15783:                case 0x03: pcbios_int_16h_03h(); break;
                   15784:                case 0x05: pcbios_int_16h_05h(); break;
                   15785:                case 0x10: pcbios_int_16h_00h(); break;
                   15786:                case 0x11: pcbios_int_16h_01h(); break;
                   15787:                case 0x12: pcbios_int_16h_12h(); break;
                   15788:                case 0x13: pcbios_int_16h_13h(); break;
                   15789:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     15790:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     15791:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     15792:                case 0xda: break; // unknown
1.1.1.43  root     15793:                case 0xdb: break; // unknown
1.1.1.22  root     15794:                case 0xff: break; // unknown
1.1       root     15795:                default:
1.1.1.22  root     15796:                        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     15797:                        break;
                   15798:                }
                   15799:                break;
                   15800:        case 0x17:
                   15801:                // PC BIOS - Printer
1.1.1.37  root     15802:                m_CF = 0;
                   15803:                switch(REG8(AH)) {
                   15804:                case 0x00: pcbios_int_17h_00h(); break;
                   15805:                case 0x01: pcbios_int_17h_01h(); break;
                   15806:                case 0x02: pcbios_int_17h_02h(); break;
                   15807:                case 0x03: pcbios_int_17h_03h(); break;
                   15808:                case 0x50: pcbios_int_17h_50h(); break;
                   15809:                case 0x51: pcbios_int_17h_51h(); break;
                   15810:                case 0x52: pcbios_int_17h_52h(); break;
                   15811:                case 0x84: pcbios_int_17h_84h(); break;
                   15812:                case 0x85: pcbios_int_17h_85h(); break;
                   15813:                default:
                   15814:                        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));
                   15815:                        break;
                   15816:                }
1.1       root     15817:                break;
                   15818:        case 0x1a:
                   15819:                // PC BIOS - Timer
1.1.1.3   root     15820:                m_CF = 0;
1.1       root     15821:                switch(REG8(AH)) {
                   15822:                case 0x00: pcbios_int_1ah_00h(); break;
                   15823:                case 0x01: break;
                   15824:                case 0x02: pcbios_int_1ah_02h(); break;
                   15825:                case 0x03: break;
                   15826:                case 0x04: pcbios_int_1ah_04h(); break;
                   15827:                case 0x05: break;
                   15828:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   15829:                case 0x0b: break;
1.1.1.14  root     15830:                case 0x35: break; // Word Perfect Third Party Interface?
                   15831:                case 0x36: break; // Word Perfect Third Party Interface
                   15832:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     15833:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     15834:                case 0xb1: break; // PCI BIOS v2.0c+
                   15835:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     15836:                default:
1.1.1.22  root     15837:                        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     15838:                        break;
                   15839:                }
                   15840:                break;
1.1.1.33  root     15841:        case 0x1b:
                   15842:                mem[0x471] = 0x00;
                   15843:                break;
1.1       root     15844:        case 0x20:
1.1.1.28  root     15845:                try {
                   15846:                        msdos_process_terminate(SREG(CS), retval, 1);
                   15847:                } catch(...) {
                   15848:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   15849:                }
1.1       root     15850:                break;
                   15851:        case 0x21:
                   15852:                // MS-DOS System Call
1.1.1.3   root     15853:                m_CF = 0;
1.1.1.28  root     15854:                try {
                   15855:                        switch(REG8(AH)) {
                   15856:                        case 0x00: msdos_int_21h_00h(); break;
                   15857:                        case 0x01: msdos_int_21h_01h(); break;
                   15858:                        case 0x02: msdos_int_21h_02h(); break;
                   15859:                        case 0x03: msdos_int_21h_03h(); break;
                   15860:                        case 0x04: msdos_int_21h_04h(); break;
                   15861:                        case 0x05: msdos_int_21h_05h(); break;
                   15862:                        case 0x06: msdos_int_21h_06h(); break;
                   15863:                        case 0x07: msdos_int_21h_07h(); break;
                   15864:                        case 0x08: msdos_int_21h_08h(); break;
                   15865:                        case 0x09: msdos_int_21h_09h(); break;
                   15866:                        case 0x0a: msdos_int_21h_0ah(); break;
                   15867:                        case 0x0b: msdos_int_21h_0bh(); break;
                   15868:                        case 0x0c: msdos_int_21h_0ch(); break;
                   15869:                        case 0x0d: msdos_int_21h_0dh(); break;
                   15870:                        case 0x0e: msdos_int_21h_0eh(); break;
                   15871:                        case 0x0f: msdos_int_21h_0fh(); break;
                   15872:                        case 0x10: msdos_int_21h_10h(); break;
                   15873:                        case 0x11: msdos_int_21h_11h(); break;
                   15874:                        case 0x12: msdos_int_21h_12h(); break;
                   15875:                        case 0x13: msdos_int_21h_13h(); break;
                   15876:                        case 0x14: msdos_int_21h_14h(); break;
                   15877:                        case 0x15: msdos_int_21h_15h(); break;
                   15878:                        case 0x16: msdos_int_21h_16h(); break;
                   15879:                        case 0x17: msdos_int_21h_17h(); break;
                   15880:                        case 0x18: msdos_int_21h_18h(); break;
                   15881:                        case 0x19: msdos_int_21h_19h(); break;
                   15882:                        case 0x1a: msdos_int_21h_1ah(); break;
                   15883:                        case 0x1b: msdos_int_21h_1bh(); break;
                   15884:                        case 0x1c: msdos_int_21h_1ch(); break;
                   15885:                        case 0x1d: msdos_int_21h_1dh(); break;
                   15886:                        case 0x1e: msdos_int_21h_1eh(); break;
                   15887:                        case 0x1f: msdos_int_21h_1fh(); break;
                   15888:                        case 0x20: msdos_int_21h_20h(); break;
                   15889:                        case 0x21: msdos_int_21h_21h(); break;
                   15890:                        case 0x22: msdos_int_21h_22h(); break;
                   15891:                        case 0x23: msdos_int_21h_23h(); break;
                   15892:                        case 0x24: msdos_int_21h_24h(); break;
                   15893:                        case 0x25: msdos_int_21h_25h(); break;
                   15894:                        case 0x26: msdos_int_21h_26h(); break;
                   15895:                        case 0x27: msdos_int_21h_27h(); break;
                   15896:                        case 0x28: msdos_int_21h_28h(); break;
                   15897:                        case 0x29: msdos_int_21h_29h(); break;
                   15898:                        case 0x2a: msdos_int_21h_2ah(); break;
                   15899:                        case 0x2b: msdos_int_21h_2bh(); break;
                   15900:                        case 0x2c: msdos_int_21h_2ch(); break;
                   15901:                        case 0x2d: msdos_int_21h_2dh(); break;
                   15902:                        case 0x2e: msdos_int_21h_2eh(); break;
                   15903:                        case 0x2f: msdos_int_21h_2fh(); break;
                   15904:                        case 0x30: msdos_int_21h_30h(); break;
                   15905:                        case 0x31: msdos_int_21h_31h(); break;
                   15906:                        case 0x32: msdos_int_21h_32h(); break;
                   15907:                        case 0x33: msdos_int_21h_33h(); break;
                   15908:                        case 0x34: msdos_int_21h_34h(); break;
                   15909:                        case 0x35: msdos_int_21h_35h(); break;
                   15910:                        case 0x36: msdos_int_21h_36h(); break;
                   15911:                        case 0x37: msdos_int_21h_37h(); break;
                   15912:                        case 0x38: msdos_int_21h_38h(); break;
                   15913:                        case 0x39: msdos_int_21h_39h(0); break;
                   15914:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   15915:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   15916:                        case 0x3c: msdos_int_21h_3ch(); break;
                   15917:                        case 0x3d: msdos_int_21h_3dh(); break;
                   15918:                        case 0x3e: msdos_int_21h_3eh(); break;
                   15919:                        case 0x3f: msdos_int_21h_3fh(); break;
                   15920:                        case 0x40: msdos_int_21h_40h(); break;
                   15921:                        case 0x41: msdos_int_21h_41h(0); break;
                   15922:                        case 0x42: msdos_int_21h_42h(); break;
                   15923:                        case 0x43: msdos_int_21h_43h(0); break;
                   15924:                        case 0x44: msdos_int_21h_44h(); break;
                   15925:                        case 0x45: msdos_int_21h_45h(); break;
                   15926:                        case 0x46: msdos_int_21h_46h(); break;
                   15927:                        case 0x47: msdos_int_21h_47h(0); break;
                   15928:                        case 0x48: msdos_int_21h_48h(); break;
                   15929:                        case 0x49: msdos_int_21h_49h(); break;
                   15930:                        case 0x4a: msdos_int_21h_4ah(); break;
                   15931:                        case 0x4b: msdos_int_21h_4bh(); break;
                   15932:                        case 0x4c: msdos_int_21h_4ch(); break;
                   15933:                        case 0x4d: msdos_int_21h_4dh(); break;
                   15934:                        case 0x4e: msdos_int_21h_4eh(); break;
                   15935:                        case 0x4f: msdos_int_21h_4fh(); break;
                   15936:                        case 0x50: msdos_int_21h_50h(); break;
                   15937:                        case 0x51: msdos_int_21h_51h(); break;
                   15938:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     15939:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     15940:                        case 0x54: msdos_int_21h_54h(); break;
                   15941:                        case 0x55: msdos_int_21h_55h(); break;
                   15942:                        case 0x56: msdos_int_21h_56h(0); break;
                   15943:                        case 0x57: msdos_int_21h_57h(); break;
                   15944:                        case 0x58: msdos_int_21h_58h(); break;
                   15945:                        case 0x59: msdos_int_21h_59h(); break;
                   15946:                        case 0x5a: msdos_int_21h_5ah(); break;
                   15947:                        case 0x5b: msdos_int_21h_5bh(); break;
                   15948:                        case 0x5c: msdos_int_21h_5ch(); break;
                   15949:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     15950:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     15951:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     15952:                        case 0x60: msdos_int_21h_60h(0); break;
                   15953:                        case 0x61: msdos_int_21h_61h(); break;
                   15954:                        case 0x62: msdos_int_21h_62h(); break;
                   15955:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     15956:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     15957:                        case 0x65: msdos_int_21h_65h(); break;
                   15958:                        case 0x66: msdos_int_21h_66h(); break;
                   15959:                        case 0x67: msdos_int_21h_67h(); break;
                   15960:                        case 0x68: msdos_int_21h_68h(); break;
                   15961:                        case 0x69: msdos_int_21h_69h(); break;
                   15962:                        case 0x6a: msdos_int_21h_6ah(); break;
                   15963:                        case 0x6b: msdos_int_21h_6bh(); break;
                   15964:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.33  root     15965:                        // 0x6d: Find First ROM Program
                   15966:                        // 0x6e: Find Next ROM Program
                   15967:                        // 0x6f: Get/Set ROM Scan Start Address
1.1.1.43  root     15968:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.28  root     15969:                        case 0x71:
1.1.1.33  root     15970:                                // Windows95 - Long Filename Functions
1.1.1.28  root     15971:                                switch(REG8(AL)) {
                   15972:                                case 0x0d: msdos_int_21h_710dh(); break;
                   15973:                                case 0x39: msdos_int_21h_39h(1); break;
                   15974:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   15975:                                case 0x3b: msdos_int_21h_3bh(1); break;
                   15976:                                case 0x41: msdos_int_21h_7141h(1); break;
                   15977:                                case 0x43: msdos_int_21h_43h(1); break;
                   15978:                                case 0x47: msdos_int_21h_47h(1); break;
                   15979:                                case 0x4e: msdos_int_21h_714eh(); break;
                   15980:                                case 0x4f: msdos_int_21h_714fh(); break;
                   15981:                                case 0x56: msdos_int_21h_56h(1); break;
                   15982:                                case 0x60: msdos_int_21h_60h(1); break;
                   15983:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   15984:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   15985:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   15986:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   15987:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   15988:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45! root     15989:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     15990:                                case 0xaa: msdos_int_21h_71aah(); break;
                   15991:                                default:
                   15992:                                        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));
                   15993:                                        REG16(AX) = 0x7100;
                   15994:                                        m_CF = 1;
                   15995:                                        break;
                   15996:                                }
                   15997:                                break;
                   15998:                        // 0x72: Windows95 beta - LFN FindClose
                   15999:                        case 0x73:
1.1.1.33  root     16000:                                // Windows95 - FAT32 Functions
1.1.1.28  root     16001:                                switch(REG8(AL)) {
                   16002:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     16003:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     16004:                                case 0x02: msdos_int_21h_7302h(); break;
                   16005:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     16006:                                // 0x04: Set DPB to Use for Formatting
                   16007:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     16008:                                default:
                   16009:                                        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));
                   16010:                                        REG16(AX) = 0x7300;
                   16011:                                        m_CF = 1;
                   16012:                                        break;
                   16013:                                }
1.1       root     16014:                                break;
1.1.1.30  root     16015:                        case 0xdb: msdos_int_21h_dbh(); break;
                   16016:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     16017:                        default:
1.1.1.22  root     16018:                                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     16019:                                REG16(AX) = 0x01;
1.1.1.3   root     16020:                                m_CF = 1;
1.1       root     16021:                                break;
                   16022:                        }
1.1.1.28  root     16023:                } catch(int error) {
                   16024:                        REG16(AX) = error;
                   16025:                        m_CF = 1;
                   16026:                } catch(...) {
                   16027:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     16028:                        m_CF = 1;
1.1       root     16029:                }
1.1.1.3   root     16030:                if(m_CF) {
1.1.1.23  root     16031:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   16032:                        sda->extended_error_code = REG16(AX);
                   16033:                        switch(sda->extended_error_code) {
                   16034:                        case  4: // Too many open files
                   16035:                        case  8: // Insufficient memory
                   16036:                                sda->error_class = 1; // Out of resource
                   16037:                                break;
                   16038:                        case  5: // Access denied
                   16039:                                sda->error_class = 3; // Authorization
                   16040:                                break;
                   16041:                        case  7: // Memory control block destroyed
                   16042:                                sda->error_class = 4; // Internal
                   16043:                                break;
                   16044:                        case  2: // File not found
                   16045:                        case  3: // Path not found
                   16046:                        case 15: // Invaid drive specified
                   16047:                        case 18: // No more files
                   16048:                                sda->error_class = 8; // Not found
                   16049:                                break;
                   16050:                        case 32: // Sharing violation
                   16051:                        case 33: // Lock violation
                   16052:                                sda->error_class = 10; // Locked
                   16053:                                break;
                   16054: //                     case 16: // Removal of current directory attempted
                   16055:                        case 19: // Attempted write on protected disk
                   16056:                        case 21: // Drive not ready
                   16057: //                     case 29: // Write failure
                   16058: //                     case 30: // Read failure
                   16059: //                     case 82: // Cannot create subdirectory
                   16060:                                sda->error_class = 11; // Media
                   16061:                                break;
                   16062:                        case 80: // File already exists
                   16063:                                sda->error_class = 12; // Already exist
                   16064:                                break;
                   16065:                        default:
                   16066:                                sda->error_class = 13; // Unknown
                   16067:                                break;
                   16068:                        }
                   16069:                        sda->suggested_action = 1; // Retry
                   16070:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     16071:                }
1.1.1.33  root     16072:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     16073:                        // raise int 23h
                   16074: #if defined(HAS_I386)
                   16075:                        m_ext = 0; // not an external interrupt
                   16076:                        i386_trap(0x23, 1, 0);
                   16077:                        m_ext = 1;
                   16078: #else
                   16079:                        PREFIX86(_interrupt)(0x23);
                   16080: #endif
                   16081:                }
1.1       root     16082:                break;
                   16083:        case 0x22:
                   16084:                fatalerror("int 22h (terminate address)\n");
                   16085:        case 0x23:
1.1.1.28  root     16086:                try {
                   16087:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   16088:                } catch(...) {
                   16089:                        fatalerror("failed to terminate the current process by int 23h\n");
                   16090:                }
1.1       root     16091:                break;
                   16092:        case 0x24:
1.1.1.32  root     16093: /*
1.1.1.28  root     16094:                try {
                   16095:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16096:                } catch(...) {
                   16097:                        fatalerror("failed to terminate the current process by int 24h\n");
                   16098:                }
1.1.1.32  root     16099: */
                   16100:                msdos_int_24h();
1.1       root     16101:                break;
                   16102:        case 0x25:
                   16103:                msdos_int_25h();
                   16104:                break;
                   16105:        case 0x26:
                   16106:                msdos_int_26h();
                   16107:                break;
                   16108:        case 0x27:
1.1.1.28  root     16109:                try {
                   16110:                        msdos_int_27h();
                   16111:                } catch(...) {
                   16112:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   16113:                }
1.1       root     16114:                break;
                   16115:        case 0x28:
                   16116:                Sleep(10);
1.1.1.35  root     16117:                REQUEST_HARDWRE_UPDATE();
1.1       root     16118:                break;
                   16119:        case 0x29:
                   16120:                msdos_int_29h();
                   16121:                break;
                   16122:        case 0x2e:
                   16123:                msdos_int_2eh();
                   16124:                break;
                   16125:        case 0x2f:
                   16126:                // multiplex interrupt
                   16127:                switch(REG8(AH)) {
1.1.1.22  root     16128:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     16129:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     16130:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     16131:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     16132:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     16133:                case 0x14: msdos_int_2fh_14h(); break;
                   16134:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     16135:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     16136:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     16137:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     16138:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     16139:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     16140:                case 0x46: msdos_int_2fh_46h(); break;
                   16141:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     16142:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     16143:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     16144:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     16145:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     16146:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     16147:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     16148:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     16149:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     16150:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     16151:                default:
1.1.1.30  root     16152:                        switch(REG8(AL)) {
                   16153:                        case 0x00:
                   16154:                                // This is not installed
                   16155: //                             REG8(AL) = 0x00;
                   16156:                                break;
1.1.1.33  root     16157:                        case 0x01:
1.1.1.42  root     16158:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   16159:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   16160:                                        break;
                   16161:                                }
1.1.1.33  root     16162:                                // Banyan VINES v4+ is not installed
                   16163:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   16164:                                        break;
                   16165:                                }
1.1.1.42  root     16166:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   16167:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   16168:                                        break;
                   16169:                                }
1.1.1.30  root     16170:                        default:
1.1.1.42  root     16171:                                // NORTON UTILITIES 5.0+
                   16172:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   16173:                                        break;
                   16174:                                }
1.1.1.30  root     16175:                                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     16176:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     16177:                                m_CF = 1;
                   16178:                                break;
                   16179:                        }
                   16180:                        break;
1.1       root     16181:                }
                   16182:                break;
1.1.1.24  root     16183:        case 0x33:
                   16184:                switch(REG8(AH)) {
                   16185:                case 0x00:
                   16186:                        // Mouse
                   16187:                        switch(REG8(AL)) {
                   16188:                        case 0x00: msdos_int_33h_0000h(); break;
                   16189:                        case 0x01: msdos_int_33h_0001h(); break;
                   16190:                        case 0x02: msdos_int_33h_0002h(); break;
                   16191:                        case 0x03: msdos_int_33h_0003h(); break;
1.1.1.34  root     16192:                        case 0x04: msdos_int_33h_0004h(); break;
1.1.1.24  root     16193:                        case 0x05: msdos_int_33h_0005h(); break;
                   16194:                        case 0x06: msdos_int_33h_0006h(); break;
                   16195:                        case 0x07: msdos_int_33h_0007h(); break;
                   16196:                        case 0x08: msdos_int_33h_0008h(); break;
                   16197:                        case 0x09: msdos_int_33h_0009h(); break;
1.1.1.34  root     16198:                        case 0x0a: break; // Define Text Cursor
1.1.1.24  root     16199:                        case 0x0b: msdos_int_33h_000bh(); break;
                   16200:                        case 0x0c: msdos_int_33h_000ch(); break;
1.1.1.34  root     16201:                        case 0x0d: break; // Light Pen Emulation On
                   16202:                        case 0x0e: break; // Light Pen Emulation Off
1.1.1.24  root     16203:                        case 0x0f: msdos_int_33h_000fh(); break;
1.1.1.34  root     16204:                        case 0x10: break; // Define Screen Region for Updating
1.1.1.24  root     16205:                        case 0x11: msdos_int_33h_0011h(); break;
1.1.1.34  root     16206:                        case 0x12: REG16(AX) = 0xffff; break; // Set Large Graphics Cursor Block
                   16207:                        case 0x13: break; // Define Double-Speed Threshold
1.1.1.24  root     16208:                        case 0x14: msdos_int_33h_0014h(); break;
                   16209:                        case 0x15: msdos_int_33h_0015h(); break;
                   16210:                        case 0x16: msdos_int_33h_0016h(); break;
                   16211:                        case 0x17: msdos_int_33h_0017h(); break;
1.1.1.43  root     16212:                        case 0x18: msdos_int_33h_0018h(); break;
                   16213:                        case 0x19: msdos_int_33h_0019h(); break;
1.1.1.24  root     16214:                        case 0x1a: msdos_int_33h_001ah(); break;
                   16215:                        case 0x1b: msdos_int_33h_001bh(); break;
                   16216:                        case 0x1d: msdos_int_33h_001dh(); break;
                   16217:                        case 0x1e: msdos_int_33h_001eh(); break;
1.1.1.34  root     16218:                        case 0x1f: msdos_int_33h_001fh(); break;
                   16219:                        case 0x20: msdos_int_33h_0020h(); break;
1.1.1.24  root     16220:                        case 0x21: msdos_int_33h_0021h(); break;
                   16221:                        case 0x22: msdos_int_33h_0022h(); break;
                   16222:                        case 0x23: msdos_int_33h_0023h(); break;
                   16223:                        case 0x24: msdos_int_33h_0024h(); break;
                   16224:                        case 0x26: msdos_int_33h_0026h(); break;
                   16225:                        case 0x2a: msdos_int_33h_002ah(); break;
1.1.1.34  root     16226:                        case 0x2f: break; // Mouse Hardware Reset
1.1.1.24  root     16227:                        case 0x31: msdos_int_33h_0031h(); break;
                   16228:                        case 0x32: msdos_int_33h_0032h(); break;
                   16229:                        default:
                   16230:                                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));
                   16231:                                break;
                   16232:                        }
                   16233:                        break;
                   16234:                default:
                   16235:                        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));
                   16236:                        break;
                   16237:                }
                   16238:                break;
1.1.1.19  root     16239:        case 0x68:
                   16240:                // dummy interrupt for EMS (int 67h)
                   16241:                switch(REG8(AH)) {
                   16242:                case 0x40: msdos_int_67h_40h(); break;
                   16243:                case 0x41: msdos_int_67h_41h(); break;
                   16244:                case 0x42: msdos_int_67h_42h(); break;
                   16245:                case 0x43: msdos_int_67h_43h(); break;
                   16246:                case 0x44: msdos_int_67h_44h(); break;
                   16247:                case 0x45: msdos_int_67h_45h(); break;
                   16248:                case 0x46: msdos_int_67h_46h(); break;
                   16249:                case 0x47: msdos_int_67h_47h(); break;
                   16250:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     16251:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   16252:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     16253:                case 0x4b: msdos_int_67h_4bh(); break;
                   16254:                case 0x4c: msdos_int_67h_4ch(); break;
                   16255:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     16256:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     16257:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     16258:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     16259:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     16260:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     16261:                case 0x53: msdos_int_67h_53h(); break;
                   16262:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.20  root     16263:                // 0x55: LIM EMS 4.0 - Alter Page Map And JUMP
                   16264:                // 0x56: LIM EMS 4.0 - Alter Page Map And CALL
                   16265:                case 0x57: msdos_int_67h_57h(); break;
                   16266:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     16267:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     16268:                case 0x5a: msdos_int_67h_5ah(); break;
                   16269:                // 0x5b: LIM EMS 4.0 - Alternate Map Register Set (for DOS Kernel)
1.1.1.43  root     16270:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   16271:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.31  root     16272:                // 0x60: EEMS - Get Physical Window Array
                   16273:                // 0x61: EEMS - Generic Accelerator Card Support
                   16274:                // 0x68: EEMS - Get Address of All Pge Frames om System
                   16275:                // 0x69: EEMS - Map Page into Frame
                   16276:                // 0x6a: EEMS - Page Mapping
                   16277:                // 0xde: VCPI
1.1.1.30  root     16278:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     16279:                default:
1.1.1.22  root     16280:                        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     16281:                        REG8(AH) = 0x84;
                   16282:                        break;
                   16283:                }
                   16284:                break;
                   16285: #ifdef SUPPORT_XMS
                   16286:        case 0x69:
                   16287:                // dummy interrupt for XMS (call far)
1.1.1.28  root     16288:                try {
                   16289:                        switch(REG8(AH)) {
                   16290:                        case 0x00: msdos_call_xms_00h(); break;
                   16291:                        case 0x01: msdos_call_xms_01h(); break;
                   16292:                        case 0x02: msdos_call_xms_02h(); break;
                   16293:                        case 0x03: msdos_call_xms_03h(); break;
                   16294:                        case 0x04: msdos_call_xms_04h(); break;
                   16295:                        case 0x05: msdos_call_xms_05h(); break;
                   16296:                        case 0x06: msdos_call_xms_06h(); break;
                   16297:                        case 0x07: msdos_call_xms_07h(); break;
                   16298:                        case 0x08: msdos_call_xms_08h(); break;
                   16299:                        case 0x09: msdos_call_xms_09h(); break;
                   16300:                        case 0x0a: msdos_call_xms_0ah(); break;
                   16301:                        case 0x0b: msdos_call_xms_0bh(); break;
                   16302:                        case 0x0c: msdos_call_xms_0ch(); break;
                   16303:                        case 0x0d: msdos_call_xms_0dh(); break;
                   16304:                        case 0x0e: msdos_call_xms_0eh(); break;
                   16305:                        case 0x0f: msdos_call_xms_0fh(); break;
                   16306:                        case 0x10: msdos_call_xms_10h(); break;
                   16307:                        case 0x11: msdos_call_xms_11h(); break;
                   16308:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     16309: #if defined(HAS_I386)
                   16310:                        case 0x88: msdos_call_xms_88h(); break;
                   16311:                        case 0x89: msdos_call_xms_89h(); break;
                   16312:                        case 0x8e: msdos_call_xms_8eh(); break;
                   16313:                        case 0x8f: msdos_call_xms_8fh(); break;
                   16314: #endif
1.1.1.28  root     16315:                        default:
                   16316:                                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));
                   16317:                                REG16(AX) = 0x0000;
                   16318:                                REG8(BL) = 0x80; // function not implemented
                   16319:                                break;
                   16320:                        }
                   16321:                } catch(...) {
1.1.1.19  root     16322:                        REG16(AX) = 0x0000;
1.1.1.28  root     16323:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     16324:                }
                   16325:                break;
                   16326: #endif
                   16327:        case 0x6a:
1.1.1.24  root     16328:                // irq12 (mouse)
                   16329:                mouse_push_ax = REG16(AX);
                   16330:                mouse_push_bx = REG16(BX);
                   16331:                mouse_push_cx = REG16(CX);
                   16332:                mouse_push_dx = REG16(DX);
                   16333:                mouse_push_si = REG16(SI);
                   16334:                mouse_push_di = REG16(DI);
                   16335:                
1.1.1.43  root     16336:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     16337:                        REG16(AX) = mouse.status_irq;
                   16338:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     16339:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   16340:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     16341:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   16342:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   16343:                        
                   16344:                        mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   16345:                        mem[0xfffd0 + 0x03] = mouse.call_addr.w.l & 0xff;
                   16346:                        mem[0xfffd0 + 0x04] = mouse.call_addr.w.l >> 8;
                   16347:                        mem[0xfffd0 + 0x05] = mouse.call_addr.w.h & 0xff;
                   16348:                        mem[0xfffd0 + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.43  root     16349:                        break;
1.1.1.24  root     16350:                }
1.1.1.43  root     16351:                for(int i = 0; i < 8; i++) {
                   16352:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   16353:                                REG16(AX) = mouse.status_irq_alt;
                   16354:                                REG16(BX) = mouse.get_buttons();
                   16355:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   16356:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   16357:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   16358:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   16359:                                
                   16360:                                mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   16361:                                mem[0xfffd0 + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   16362:                                mem[0xfffd0 + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   16363:                                mem[0xfffd0 + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   16364:                                mem[0xfffd0 + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
                   16365:                                break;
                   16366:                        }
                   16367:                }
                   16368:                // invalid call addr :-(
                   16369:                mem[0xfffd0 + 0x02] = 0x90;     // nop
                   16370:                mem[0xfffd0 + 0x03] = 0x90;     // nop
                   16371:                mem[0xfffd0 + 0x04] = 0x90;     // nop
                   16372:                mem[0xfffd0 + 0x05] = 0x90;     // nop
                   16373:                mem[0xfffd0 + 0x06] = 0x90;     // nop
1.1.1.24  root     16374:                break;
                   16375:        case 0x6b:
                   16376:                // end of irq12 (mouse)
                   16377:                REG16(AX) = mouse_push_ax;
                   16378:                REG16(BX) = mouse_push_bx;
                   16379:                REG16(CX) = mouse_push_cx;
                   16380:                REG16(DX) = mouse_push_dx;
                   16381:                REG16(SI) = mouse_push_si;
                   16382:                REG16(DI) = mouse_push_di;
                   16383:                
                   16384:                // EOI
                   16385:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   16386:                        pic[0].isr &= ~(1 << 2); // master
                   16387:                }
                   16388:                pic_update();
                   16389:                break;
                   16390:        case 0x6c:
1.1.1.19  root     16391:                // dummy interrupt for case map routine pointed in the country info
                   16392:                if(REG8(AL) >= 0x80) {
                   16393:                        char tmp[2] = {0};
                   16394:                        tmp[0] = REG8(AL);
                   16395:                        my_strupr(tmp);
                   16396:                        REG8(AL) = tmp[0];
                   16397:                }
                   16398:                break;
1.1.1.27  root     16399:        case 0x6d:
                   16400:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   16401:                REG8(AL) = 0x86; // not supported
                   16402:                m_CF = 1;
                   16403:                break;
1.1.1.32  root     16404:        case 0x6e:
                   16405:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   16406:                {
                   16407:                        UINT16 code = REG16(AX);
                   16408:                        if(code & 0xf0) {
                   16409:                                code = (code & 7) | ((code & 0x10) >> 1);
                   16410:                        }
                   16411:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   16412:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   16413:                                        const char *message = NULL;
                   16414:                                        if(active_code_page == 932) {
                   16415:                                                message = param_error_table[i].message_japanese;
                   16416:                                        }
                   16417:                                        if(message == NULL) {
                   16418:                                                message = param_error_table[i].message_english;
                   16419:                                        }
                   16420:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   16421:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   16422:                                        
                   16423:                                        SREG(ES) = WORK_TOP >> 4;
                   16424:                                        i386_load_segment_descriptor(ES);
                   16425:                                        REG16(DI) = 0x0000;
                   16426:                                        break;
                   16427:                                }
                   16428:                        }
                   16429:                }
                   16430:                break;
1.1.1.8   root     16431:        case 0x70:
                   16432:        case 0x71:
                   16433:        case 0x72:
                   16434:        case 0x73:
                   16435:        case 0x74:
                   16436:        case 0x75:
                   16437:        case 0x76:
                   16438:        case 0x77:
                   16439:                // EOI
                   16440:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   16441:                        pic[0].isr &= ~(1 << 2); // master
                   16442:                }
                   16443:                pic_update();
                   16444:                break;
1.1       root     16445:        default:
1.1.1.22  root     16446: //             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     16447:                break;
                   16448:        }
                   16449:        
                   16450:        // update cursor position
                   16451:        if(cursor_moved) {
1.1.1.36  root     16452:                pcbios_update_cursor_position();
1.1       root     16453:                cursor_moved = false;
                   16454:        }
                   16455: }
                   16456: 
                   16457: // init
                   16458: 
                   16459: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   16460: {
                   16461:        // init file handler
                   16462:        memset(file_handler, 0, sizeof(file_handler));
                   16463:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   16464:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   16465:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     16466: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     16467:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     16468: #else
                   16469:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   16470: #endif
                   16471:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     16472:        }
1.1.1.21  root     16473:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45! root     16474: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
        !          16475:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     16476:        }
1.1       root     16477:        _dup2(0, DUP_STDIN);
                   16478:        _dup2(1, DUP_STDOUT);
                   16479:        _dup2(2, DUP_STDERR);
1.1.1.21  root     16480:        _dup2(3, DUP_STDAUX);
                   16481:        _dup2(4, DUP_STDPRN);
1.1       root     16482:        
1.1.1.24  root     16483:        // init mouse
                   16484:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     16485:        mouse.enabled = true;   // from DOSBox
                   16486:        mouse.hidden = 1;       // hidden in default ???
                   16487:        mouse.old_hidden = 1;   // from DOSBox
                   16488:        mouse.max_position.x = 8 * (scr_width  - 1);
                   16489:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     16490:        mouse.mickey.x = 8;
                   16491:        mouse.mickey.y = 16;
                   16492:        
1.1.1.26  root     16493: #ifdef SUPPORT_XMS
                   16494:        // init xms
                   16495:        msdos_xms_init();
                   16496: #endif
                   16497:        
1.1       root     16498:        // init process
                   16499:        memset(process, 0, sizeof(process));
                   16500:        
1.1.1.13  root     16501:        // init dtainfo
                   16502:        msdos_dta_info_init();
                   16503:        
1.1       root     16504:        // init memory
                   16505:        memset(mem, 0, sizeof(mem));
                   16506:        
                   16507:        // bios data area
1.1.1.23  root     16508:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     16509:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   16510:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     16511:        CONSOLE_FONT_INFO cfi;
                   16512:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   16513:        
                   16514:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   16515:        text_vram_top_address = TEXT_VRAM_TOP;
                   16516:        text_vram_end_address = text_vram_top_address + regen;
                   16517:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   16518:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   16519:        
                   16520:        if(regen > 0x4000) {
                   16521:                regen = 0x8000;
                   16522:                vram_pages = 1;
                   16523:        } else if(regen > 0x2000) {
                   16524:                regen = 0x4000;
                   16525:                vram_pages = 2;
                   16526:        } else if(regen > 0x1000) {
                   16527:                regen = 0x2000;
                   16528:                vram_pages = 4;
                   16529:        } else {
                   16530:                regen = 0x1000;
                   16531:                vram_pages = 8;
                   16532:        }
1.1       root     16533:        
1.1.1.25  root     16534:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   16535:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     16536:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   16537:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     16538:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     16539:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   16540:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     16541: #ifdef EXT_BIOS_TOP
1.1.1.25  root     16542:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     16543: #endif
1.1.1.26  root     16544:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     16545:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     16546:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   16547:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     16548:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     16549:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   16550:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     16551:        *(UINT16 *)(mem + 0x44e) = 0;
                   16552:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     16553:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     16554:        *(UINT8  *)(mem + 0x460) = 7;
                   16555:        *(UINT8  *)(mem + 0x461) = 7;
                   16556:        *(UINT8  *)(mem + 0x462) = 0;
                   16557:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     16558:        *(UINT8  *)(mem + 0x465) = 0x09;
                   16559:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     16560:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     16561:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   16562:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     16563:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   16564:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   16565:        *(UINT8  *)(mem + 0x487) = 0x60;
                   16566:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     16567: #ifdef EXT_BIOS_TOP
1.1.1.25  root     16568:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     16569: #endif
1.1.1.14  root     16570:        
                   16571:        // initial screen
                   16572:        SMALL_RECT rect;
                   16573:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   16574:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   16575:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   16576:                for(int x = 0; x < scr_width; x++) {
                   16577:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   16578:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   16579:                }
                   16580:        }
1.1       root     16581:        
1.1.1.19  root     16582:        // init mcb
1.1       root     16583:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     16584:        
                   16585:        // iret table
                   16586:        // note: int 2eh vector should address the routine in command.com,
                   16587:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   16588:        // so move iret table into allocated memory block
                   16589:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33  root     16590:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19  root     16591:        IRET_TOP = seg << 4;
                   16592:        seg += IRET_SIZE >> 4;
1.1.1.25  root     16593:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     16594:        
                   16595:        // dummy xms/ems device
1.1.1.33  root     16596:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     16597:        XMS_TOP = seg << 4;
                   16598:        seg += XMS_SIZE >> 4;
                   16599:        
                   16600:        // environment
1.1.1.33  root     16601:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     16602:        int env_seg = seg;
                   16603:        int ofs = 0;
1.1.1.32  root     16604:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   16605:        char comspec_added = 0;
1.1.1.33  root     16606:        char lastdrive_added = 0;
1.1.1.32  root     16607:        char env_msdos_path[ENV_SIZE] = {0};
                   16608:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     16609:        char prompt_added = 0;
1.1.1.32  root     16610:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     16611:        char tz_added = 0;
1.1.1.45! root     16612:        const char *path, *short_path;
1.1.1.32  root     16613:        
                   16614:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   16615:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16616:                        strcpy(env_append, short_path);
                   16617:                }
                   16618:        }
                   16619:        if((path = getenv("APPEND")) != NULL) {
                   16620:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16621:                        if(env_append[0] != '\0') {
                   16622:                                strcat(env_append, ";");
                   16623:                        }
                   16624:                        strcat(env_append, short_path);
                   16625:                }
                   16626:        }
                   16627:        
                   16628:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   16629:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16630:                        strcpy(comspec_path, short_path);
                   16631:                }
                   16632:        }
                   16633:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   16634:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16635:                        strcpy(comspec_path, short_path);
                   16636:                }
                   16637:        }
1.1       root     16638:        
1.1.1.28  root     16639:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     16640:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16641:                        strcpy(env_msdos_path, short_path);
                   16642:                        strcpy(env_path, short_path);
1.1.1.14  root     16643:                }
                   16644:        }
1.1.1.28  root     16645:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     16646:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16647:                        if(env_path[0] != '\0') {
                   16648:                                strcat(env_path, ";");
                   16649:                        }
                   16650:                        strcat(env_path, short_path);
1.1.1.9   root     16651:                }
                   16652:        }
1.1.1.32  root     16653:        
                   16654:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   16655:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     16656:        }
1.1.1.32  root     16657:        for(int i = 0; i < 4; i++) {
                   16658:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   16659:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   16660:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16661:                                strcpy(env_temp, short_path);
                   16662:                                break;
                   16663:                        }
                   16664:                }
1.1.1.24  root     16665:        }
1.1.1.32  root     16666:        
1.1.1.9   root     16667:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     16668:                // lower to upper
1.1.1.28  root     16669:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     16670:                strcpy(tmp, *p);
                   16671:                for(int i = 0;; i++) {
                   16672:                        if(tmp[i] == '=') {
                   16673:                                tmp[i] = '\0';
                   16674:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     16675:                                my_strupr(name);
1.1       root     16676:                                tmp[i] = '=';
                   16677:                                break;
                   16678:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     16679:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     16680:                        }
                   16681:                }
1.1.1.33  root     16682:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   16683:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   16684:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     16685:                        // ignore non standard environments
                   16686:                } else {
1.1.1.33  root     16687:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     16688:                                if(env_append[0] != '\0') {
                   16689:                                        sprintf(tmp, "APPEND=%s", env_append);
                   16690:                                } else {
                   16691:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   16692:                                }
                   16693:                                append_added = 1;
                   16694:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     16695:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     16696:                                comspec_added = 1;
1.1.1.33  root     16697:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   16698:                                char *env = getenv("MSDOS_LASTDRIVE");
                   16699:                                if(env != NULL) {
                   16700:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   16701:                                }
                   16702:                                lastdrive_added = 1;
                   16703:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     16704:                                if(env_msdos_path[0] != '\0') {
                   16705:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   16706:                                } else {
                   16707:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   16708:                                }
1.1.1.33  root     16709:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     16710:                                if(env_path[0] != '\0') {
                   16711:                                        sprintf(tmp, "PATH=%s", env_path);
                   16712:                                } else {
                   16713:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   16714:                                }
1.1.1.32  root     16715:                                path_added = 1;
1.1.1.33  root     16716:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   16717:                                prompt_added = 1;
1.1.1.28  root     16718:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   16719:                                if(env_temp[0] != '\0') {
                   16720:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   16721:                                } else {
                   16722:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   16723:                                }
1.1.1.32  root     16724:                                temp_added = 1;
1.1.1.33  root     16725:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     16726:                                if(env_temp[0] != '\0') {
                   16727:                                        sprintf(tmp, "TMP=%s", env_temp);
                   16728:                                } else {
                   16729:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     16730:                                }
1.1.1.32  root     16731:                                tmp_added = 1;
1.1.1.33  root     16732:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   16733:                                char *env = getenv("MSDOS_TZ");
                   16734:                                if(env != NULL) {
                   16735:                                        sprintf(tmp, "TZ=%s", env);
                   16736:                                }
                   16737:                                tz_added = 1;
1.1       root     16738:                        }
                   16739:                        int len = strlen(tmp);
1.1.1.14  root     16740:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     16741:                                fatalerror("too many environments\n");
                   16742:                        }
                   16743:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   16744:                        ofs += len + 1;
                   16745:                }
                   16746:        }
1.1.1.32  root     16747:        if(!append_added && env_append[0] != '\0') {
                   16748:                #define SET_ENV(name, value) { \
                   16749:                        char tmp[ENV_SIZE]; \
                   16750:                        sprintf(tmp, "%s=%s", name, value); \
                   16751:                        int len = strlen(tmp); \
                   16752:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   16753:                                fatalerror("too many environments\n"); \
                   16754:                        } \
                   16755:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   16756:                        ofs += len + 1; \
                   16757:                }
                   16758:                SET_ENV("APPEND", env_append);
                   16759:        }
                   16760:        if(!comspec_added) {
                   16761:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   16762:        }
1.1.1.33  root     16763:        if(!lastdrive_added) {
                   16764:                SET_ENV("LASTDRIVE", "Z");
                   16765:        }
1.1.1.32  root     16766:        if(!path_added) {
                   16767:                SET_ENV("PATH", env_path);
                   16768:        }
1.1.1.33  root     16769:        if(!prompt_added) {
                   16770:                SET_ENV("PROMPT", "$P$G");
                   16771:        }
1.1.1.32  root     16772:        if(!temp_added) {
                   16773:                SET_ENV("TEMP", env_temp);
                   16774:        }
                   16775:        if(!tmp_added) {
                   16776:                SET_ENV("TMP", env_temp);
                   16777:        }
1.1.1.33  root     16778:        if(!tz_added) {
                   16779:                TIME_ZONE_INFORMATION tzi;
                   16780:                HKEY hKey, hSubKey;
                   16781:                char tzi_std_name[64];
                   16782:                char tz_std[8] = "GMT";
                   16783:                char tz_dlt[8] = "GST";
                   16784:                char tz_value[32];
                   16785:                
                   16786:                // timezone name from GetTimeZoneInformation may not be english
                   16787:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   16788:                setlocale(LC_CTYPE, "");
                   16789:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   16790:                
                   16791:                // get english timezone name from registry
                   16792:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   16793:                        for(DWORD i = 0; !tz_added; i++) {
                   16794:                                char reg_name[256], sub_key[1024], std_name[256];
                   16795:                                DWORD size;
                   16796:                                FILETIME ftTime;
                   16797:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   16798:                                
                   16799:                                if(result == ERROR_SUCCESS) {
                   16800:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   16801:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   16802:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   16803:                                                        // search english timezone name from table
1.1.1.37  root     16804:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     16805:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   16806:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   16807:                                                                                if(tz_table[j].std != NULL) {
                   16808:                                                                                        strcpy(tz_std, tz_table[j].std);
                   16809:                                                                                }
                   16810:                                                                                if(tz_table[j].dlt != NULL) {
                   16811:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   16812:                                                                                }
                   16813:                                                                                tz_added = 1;
                   16814:                                                                                break;
                   16815:                                                                        }
                   16816:                                                                }
                   16817:                                                        }
                   16818:                                                }
                   16819:                                                RegCloseKey(hSubKey);
                   16820:                                        }
                   16821:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   16822:                                        break;
                   16823:                                }
                   16824:                        }
                   16825:                        RegCloseKey(hKey);
                   16826:                }
                   16827:                if((tzi.Bias % 60) != 0) {
                   16828:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   16829:                } else {
                   16830:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   16831:                }
                   16832:                if(daylight) {
                   16833:                        strcat(tz_value, tz_dlt);
                   16834:                }
                   16835:                SET_ENV("TZ", tz_value);
                   16836:        }
1.1       root     16837:        seg += (ENV_SIZE >> 4);
                   16838:        
                   16839:        // psp
1.1.1.33  root     16840:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     16841:        current_psp = seg;
1.1.1.35  root     16842:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   16843:        psp->parent_psp = current_psp;
1.1       root     16844:        seg += (PSP_SIZE >> 4);
                   16845:        
1.1.1.19  root     16846:        // first free mcb in conventional memory
1.1.1.33  root     16847:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     16848:        first_mcb = seg;
                   16849:        
1.1.1.19  root     16850:        // dummy mcb to link to umb
1.1.1.33  root     16851: #if 0
1.1.1.39  root     16852:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     16853: #else
1.1.1.39  root     16854:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     16855: #endif
1.1.1.19  root     16856:        
                   16857:        // first free mcb in upper memory block
1.1.1.8   root     16858:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     16859:        
1.1.1.29  root     16860: #ifdef SUPPORT_HMA
                   16861:        // first free mcb in high memory area
                   16862:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   16863: #endif
                   16864:        
1.1.1.26  root     16865:        // interrupt vector
                   16866:        for(int i = 0; i < 0x80; i++) {
                   16867:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   16868:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   16869:        }
1.1.1.35  root     16870:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffd:0018 irq0 (system timer)
1.1.1.26  root     16871:        *(UINT16 *)(mem + 4 * 0x08 + 2) = 0xfffd;
                   16872:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   16873:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   16874:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   16875:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
                   16876:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffd:0000 irq12 (mouse)
                   16877:        *(UINT16 *)(mem + 4 * 0x74 + 2) = 0xfffd;
                   16878:        
1.1.1.29  root     16879:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     16880:        static const struct {
                   16881:                UINT16 attributes;
                   16882:                char *dev_name;
                   16883:        } dummy_devices[] = {
                   16884:                {0x8013, "CON     "},
                   16885:                {0x8000, "AUX     "},
                   16886:                {0xa0c0, "PRN     "},
                   16887:                {0x8008, "CLOCK$  "},
                   16888:                {0x8000, "COM1    "},
                   16889:                {0xa0c0, "LPT1    "},
                   16890:                {0xa0c0, "LPT2    "},
                   16891:                {0xa0c0, "LPT3    "},
                   16892:                {0x8000, "COM2    "},
                   16893:                {0x8000, "COM3    "},
                   16894:                {0x8000, "COM4    "},
1.1.1.30  root     16895: //             {0xc000, "CONFIG$ "},
                   16896:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     16897:        };
                   16898:        static const UINT8 dummy_device_routine[] = {
                   16899:                // from NUL device of Windows 98 SE
                   16900:                // or word ptr ES:[BX+03],0100
                   16901:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   16902:                // retf
                   16903:                0xcb,
                   16904:        };
1.1.1.29  root     16905:        device_t *last = NULL;
1.1.1.32  root     16906:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     16907:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     16908:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   16909:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     16910:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     16911:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   16912:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     16913:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     16914:                last = device;
                   16915:        }
                   16916:        if(last != NULL) {
                   16917:                last->next_driver.w.l = 0;
                   16918:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     16919:        }
1.1.1.29  root     16920:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     16921:        
1.1.1.25  root     16922:        // dos info
                   16923:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   16924:        dos_info->magic_word = 1;
                   16925:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   16926:        dos_info->first_dpb.w.l = 0;
                   16927:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   16928:        dos_info->first_sft.w.l = 0;
                   16929:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     16930:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     16931:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     16932:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     16933:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   16934:        dos_info->max_sector_len = 512;
                   16935:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   16936:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   16937:        dos_info->cds.w.l = 0;
                   16938:        dos_info->cds.w.h = CDS_TOP >> 4;
                   16939:        dos_info->fcb_table.w.l = 0;
                   16940:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   16941:        dos_info->last_drive = 'Z' - 'A' + 1;
                   16942:        dos_info->buffers_x = 20;
                   16943:        dos_info->buffers_y = 0;
                   16944:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     16945:        dos_info->nul_device.next_driver.w.l = 22;
                   16946:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     16947:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     16948:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   16949:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     16950:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   16951:        dos_info->disk_buf_heads.w.l = 0;
                   16952:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     16953:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     16954:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   16955:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     16956:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     16957:        
                   16958:        char *env;
                   16959:        if((env = getenv("LASTDRIVE")) != NULL) {
                   16960:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   16961:                        dos_info->last_drive = env[0] - 'A' + 1;
                   16962:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   16963:                        dos_info->last_drive = env[0] - 'a' + 1;
                   16964:                }
                   16965:        }
                   16966:        if((env = getenv("windir")) != NULL) {
                   16967:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   16968:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   16969:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   16970:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   16971:                }
                   16972:        }
                   16973: #if defined(HAS_I386)
                   16974:        dos_info->i386_or_later = 1;
                   16975: #else
                   16976:        dos_info->i386_or_later = 0;
                   16977: #endif
                   16978:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   16979:        
1.1.1.27  root     16980:        // ems (int 67h) and xms
1.1.1.25  root     16981:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   16982:        xms_device->next_driver.w.l = 0xffff;
                   16983:        xms_device->next_driver.w.h = 0xffff;
                   16984:        xms_device->attributes = 0xc000;
1.1.1.29  root     16985:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   16986:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     16987:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   16988:        
1.1.1.26  root     16989:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   16990:        mem[XMS_TOP + 0x13] = 0x68;
                   16991:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     16992: #ifdef SUPPORT_XMS
                   16993:        if(support_xms) {
1.1.1.26  root     16994:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   16995:                mem[XMS_TOP + 0x16] = 0x69;
                   16996:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     16997:        } else
                   16998: #endif
1.1.1.26  root     16999:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     17000:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     17001:        
1.1.1.26  root     17002:        // irq12 routine (mouse)
1.1.1.24  root     17003:        mem[0xfffd0 + 0x00] = 0xcd;     // int 6ah (dummy)
                   17004:        mem[0xfffd0 + 0x01] = 0x6a;
                   17005:        mem[0xfffd0 + 0x02] = 0x9a;     // call far mouse
                   17006:        mem[0xfffd0 + 0x03] = 0xff;
                   17007:        mem[0xfffd0 + 0x04] = 0xff;
                   17008:        mem[0xfffd0 + 0x05] = 0xff;
                   17009:        mem[0xfffd0 + 0x06] = 0xff;
                   17010:        mem[0xfffd0 + 0x07] = 0xcd;     // int 6bh (dummy)
                   17011:        mem[0xfffd0 + 0x08] = 0x6b;
                   17012:        mem[0xfffd0 + 0x09] = 0xcf;     // iret
                   17013:        
1.1.1.27  root     17014:        // case map routine
                   17015:        mem[0xfffd0 + 0x0a] = 0xcd;     // int 6ch (dummy)
                   17016:        mem[0xfffd0 + 0x0b] = 0x6c;
                   17017:        mem[0xfffd0 + 0x0c] = 0xcb;     // retf
                   17018:        
                   17019:        // font read routine
                   17020:        mem[0xfffd0 + 0x0d] = 0xcd;     // int 6dh (dummy)
                   17021:        mem[0xfffd0 + 0x0e] = 0x6d;
                   17022:        mem[0xfffd0 + 0x0f] = 0xcb;     // retf
1.1.1.19  root     17023:        
1.1.1.32  root     17024:        // error message read routine
                   17025:        mem[0xfffd0 + 0x10] = 0xcd;     // int 6eh (dummy)
                   17026:        mem[0xfffd0 + 0x11] = 0x6e;
                   17027:        mem[0xfffd0 + 0x12] = 0xcb;     // retf
                   17028:        
1.1.1.35  root     17029:        // dummy loop to wait BIOS/DOS service is done
                   17030:        mem[0xfffd0 + 0x13] = 0xe6;     // out f7h, al
                   17031:        mem[0xfffd0 + 0x14] = 0xf7;
                   17032:        mem[0xfffd0 + 0x15] = 0x78;     // js/jns -4
                   17033:        mem[0xfffd0 + 0x16] = 0xfc;
                   17034:        mem[0xfffd0 + 0x17] = 0xcb;     // retf
                   17035:        
1.1.1.26  root     17036:        // irq0 routine (system time)
1.1.1.35  root     17037:        mem[0xfffd0 + 0x18] = 0xcd;     // int 1ch
                   17038:        mem[0xfffd0 + 0x19] = 0x1c;
                   17039:        mem[0xfffd0 + 0x1a] = 0xea;     // jmp far (IRET_TOP >> 4):0008
                   17040:        mem[0xfffd0 + 0x1b] = 0x08;
                   17041:        mem[0xfffd0 + 0x1c] = 0x00;
                   17042:        mem[0xfffd0 + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   17043:        mem[0xfffd0 + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
1.1.1.14  root     17044:        
1.1.1.26  root     17045:        // boot routine
1.1       root     17046:        mem[0xffff0] = 0xf4;    // halt
                   17047:        mem[0xffff1] = 0xcd;    // int 21h
                   17048:        mem[0xffff2] = 0x21;
                   17049:        mem[0xffff3] = 0xcb;    // retf
                   17050:        
1.1.1.24  root     17051:        mem[0xffff5] = '0';     // rom date
                   17052:        mem[0xffff6] = '2';
                   17053:        mem[0xffff7] = '/';
                   17054:        mem[0xffff8] = '2';
                   17055:        mem[0xffff9] = '2';
                   17056:        mem[0xffffa] = '/';
                   17057:        mem[0xffffb] = '0';
                   17058:        mem[0xffffc] = '6';
                   17059:        mem[0xffffe] = 0xfc;    // machine id
                   17060:        mem[0xfffff] = 0x00;
                   17061:        
1.1       root     17062:        // param block
                   17063:        // + 0: param block (22bytes)
                   17064:        // +24: fcb1/2 (20bytes)
                   17065:        // +44: command tail (128bytes)
                   17066:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   17067:        param->env_seg = 0;
                   17068:        param->cmd_line.w.l = 44;
                   17069:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   17070:        param->fcb1.w.l = 24;
                   17071:        param->fcb1.w.h = (WORK_TOP >> 4);
                   17072:        param->fcb2.w.l = 24;
                   17073:        param->fcb2.w.h = (WORK_TOP >> 4);
                   17074:        
                   17075:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   17076:        
                   17077:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   17078:        if(argc > 1) {
                   17079:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   17080:                for(int i = 2; i < argc; i++) {
                   17081:                        char tmp[128];
                   17082:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   17083:                        strcpy(cmd_line->cmd, tmp);
                   17084:                }
                   17085:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   17086:        } else {
                   17087:                cmd_line->len = 0;
                   17088:        }
                   17089:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   17090:        
                   17091:        // system file table
1.1.1.21  root     17092:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   17093:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     17094:        
1.1.1.19  root     17095:        // disk buffer header (from DOSBox)
                   17096:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   17097:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   17098:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   17099:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   17100:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   17101:        
1.1       root     17102:        // fcb table
                   17103:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     17104:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     17105:        
1.1.1.41  root     17106:        // drive parameter block
1.1.1.42  root     17107:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     17108:                // may be a floppy drive
1.1.1.44  root     17109:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   17110:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   17111:                cds->drive_attrib = 0x4000;     // physical drive
                   17112:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   17113:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   17114:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   17115:                cds->bs_offset = 2;
                   17116:                
1.1.1.41  root     17117:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   17118:                dpb->drive_num = i;
                   17119:                dpb->unit_num = i;
1.1.1.43  root     17120:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   17121:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     17122:        }
                   17123:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     17124:                msdos_cds_update(i);
1.1.1.42  root     17125:                UINT16 seg, ofs;
                   17126:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     17127:        }
                   17128:        
1.1.1.17  root     17129:        // nls stuff
                   17130:        msdos_nls_tables_init();
1.1       root     17131:        
                   17132:        // execute command
1.1.1.28  root     17133:        try {
                   17134:                if(msdos_process_exec(argv[0], param, 0)) {
                   17135:                        fatalerror("'%s' not found\n", argv[0]);
                   17136:                }
                   17137:        } catch(...) {
                   17138:                // we should not reach here :-(
                   17139:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     17140:        }
                   17141:        retval = 0;
                   17142:        return(0);
                   17143: }
                   17144: 
                   17145: #define remove_std_file(path) { \
                   17146:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   17147:        if(fd != -1) { \
                   17148:                _lseek(fd, 0, SEEK_END); \
                   17149:                int size = _tell(fd); \
                   17150:                _close(fd); \
                   17151:                if(size == 0) { \
                   17152:                        remove(path); \
                   17153:                } \
                   17154:        } \
                   17155: }
                   17156: 
                   17157: void msdos_finish()
                   17158: {
                   17159:        for(int i = 0; i < MAX_FILES; i++) {
                   17160:                if(file_handler[i].valid) {
                   17161:                        _close(i);
                   17162:                }
                   17163:        }
1.1.1.21  root     17164: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     17165:        remove_std_file("stdaux.txt");
1.1.1.21  root     17166: #endif
1.1.1.30  root     17167: #ifdef SUPPORT_XMS
                   17168:        msdos_xms_finish();
                   17169: #endif
1.1       root     17170:        msdos_dbcs_table_finish();
                   17171: }
                   17172: 
                   17173: /* ----------------------------------------------------------------------------
                   17174:        PC/AT hardware emulation
                   17175: ---------------------------------------------------------------------------- */
                   17176: 
                   17177: void hardware_init()
                   17178: {
1.1.1.3   root     17179:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     17180:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     17181:        m_IF = 1;
1.1.1.3   root     17182: #if defined(HAS_I386)
1.1       root     17183:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   17184:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     17185: #endif
                   17186:        i386_set_a20_line(0);
1.1.1.14  root     17187:        
1.1.1.19  root     17188:        ems_init();
1.1.1.25  root     17189:        dma_init();
1.1       root     17190:        pic_init();
1.1.1.25  root     17191:        pio_init();
1.1.1.8   root     17192: #ifdef PIT_ALWAYS_RUNNING
                   17193:        pit_init();
                   17194: #else
1.1       root     17195:        pit_active = 0;
                   17196: #endif
1.1.1.25  root     17197:        sio_init();
1.1.1.8   root     17198:        cmos_init();
                   17199:        kbd_init();
1.1       root     17200: }
                   17201: 
1.1.1.10  root     17202: void hardware_finish()
                   17203: {
                   17204: #if defined(HAS_I386)
                   17205:        vtlb_free(m_vtlb);
                   17206: #endif
1.1.1.19  root     17207:        ems_finish();
1.1.1.37  root     17208:        pio_finish();
1.1.1.25  root     17209:        sio_finish();
1.1.1.10  root     17210: }
                   17211: 
1.1.1.28  root     17212: void hardware_release()
                   17213: {
                   17214:        // release hardware resources when this program will be terminated abnormally
                   17215: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     17216:        if(fp_debug_log != NULL) {
                   17217:                fclose(fp_debug_log);
                   17218:                fp_debug_log = NULL;
1.1.1.28  root     17219:        }
                   17220: #endif
                   17221: #if defined(HAS_I386)
                   17222:        vtlb_free(m_vtlb);
                   17223: #endif
                   17224:        ems_release();
1.1.1.37  root     17225:        pio_release();
1.1.1.28  root     17226:        sio_release();
                   17227: }
                   17228: 
1.1       root     17229: void hardware_run()
                   17230: {
1.1.1.22  root     17231: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     17232:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     17233:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     17234: #endif
1.1.1.3   root     17235:        while(!m_halted) {
                   17236: #if defined(HAS_I386)
1.1       root     17237:                CPU_EXECUTE_CALL(i386);
1.1.1.14  root     17238:                if(m_eip != m_prev_eip) {
1.1.1.35  root     17239:                        idle_ops++;
                   17240:                }
1.1.1.14  root     17241: #else
1.1.1.35  root     17242:                CPU_EXECUTE_CALL(CPU_MODEL);
1.1.1.14  root     17243:                if(m_pc != m_prevpc) {
1.1.1.35  root     17244:                        idle_ops++;
1.1.1.14  root     17245:                }
1.1.1.35  root     17246: #endif
                   17247:                if(++update_ops == UPDATE_OPS) {
1.1       root     17248:                        hardware_update();
1.1.1.35  root     17249:                        update_ops = 0;
1.1       root     17250:                }
                   17251:        }
1.1.1.22  root     17252: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     17253:        if(fp_debug_log != NULL) {
                   17254:                fclose(fp_debug_log);
                   17255:                fp_debug_log = NULL;
1.1.1.28  root     17256:        }
1.1.1.22  root     17257: #endif
1.1       root     17258: }
                   17259: 
                   17260: void hardware_update()
                   17261: {
1.1.1.8   root     17262:        static UINT32 prev_time = 0;
                   17263:        UINT32 cur_time = timeGetTime();
                   17264:        
                   17265:        if(prev_time != cur_time) {
                   17266:                // update pit and raise irq0
                   17267: #ifndef PIT_ALWAYS_RUNNING
                   17268:                if(pit_active)
                   17269: #endif
                   17270:                {
                   17271:                        if(pit_run(0, cur_time)) {
                   17272:                                pic_req(0, 0, 1);
                   17273:                        }
                   17274:                        pit_run(1, cur_time);
                   17275:                        pit_run(2, cur_time);
                   17276:                }
1.1.1.24  root     17277:                
1.1.1.25  root     17278:                // update sio and raise irq4/3
1.1.1.29  root     17279:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     17280:                        sio_update(c);
                   17281:                }
                   17282:                
1.1.1.24  root     17283:                // update keyboard and mouse
1.1.1.14  root     17284:                static UINT32 prev_tick = 0;
                   17285:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     17286:                
1.1.1.14  root     17287:                if(prev_tick != cur_tick) {
                   17288:                        // update keyboard flags
                   17289:                        UINT8 state;
1.1.1.24  root     17290:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   17291:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   17292:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   17293:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   17294:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   17295:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   17296:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   17297:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     17298:                        mem[0x417] = state;
                   17299:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   17300:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   17301:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   17302:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     17303: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   17304: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     17305:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   17306:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   17307:                        mem[0x418] = state;
                   17308:                        
1.1.1.24  root     17309:                        // update console input if needed
1.1.1.34  root     17310:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     17311:                                update_console_input();
                   17312:                        }
                   17313:                        
                   17314:                        // raise irq1 if key is pressed/released
                   17315:                        if(key_changed) {
1.1.1.8   root     17316:                                pic_req(0, 1, 1);
1.1.1.24  root     17317:                                key_changed = false;
                   17318:                        }
                   17319:                        
                   17320:                        // raise irq12 if mouse status is changed
1.1.1.43  root     17321:                        if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
                   17322:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   17323:                                mouse.status_irq_alt = 0; // ???
1.1.1.24  root     17324:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     17325:                                pic_req(1, 4, 1);
                   17326:                        } else {
                   17327:                                for(int i = 0; i < 8; i++) {
                   17328:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17329:                                                mouse.status_irq = 0; // ???
                   17330:                                                mouse.status_irq_alt = 0;
                   17331:                                                for(int j = 0; j < 8; j++) {
                   17332:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   17333:                                                                mouse.status_irq_alt |=  (1 << j);
                   17334:                                                                mouse.status_alt     &= ~(1 << j);
                   17335:                                                        }
                   17336:                                                }
                   17337:                                                pic_req(1, 4, 1);
                   17338:                                                break;
                   17339:                                        }
                   17340:                                }
1.1.1.8   root     17341:                        }
1.1.1.24  root     17342:                        
1.1.1.14  root     17343:                        prev_tick = cur_tick;
1.1.1.8   root     17344:                }
1.1.1.24  root     17345:                
1.1.1.19  root     17346:                // update daily timer counter
                   17347:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     17348:                
1.1.1.8   root     17349:                prev_time = cur_time;
1.1       root     17350:        }
                   17351: }
                   17352: 
1.1.1.19  root     17353: // ems
                   17354: 
                   17355: void ems_init()
                   17356: {
                   17357:        memset(ems_handles, 0, sizeof(ems_handles));
                   17358:        memset(ems_pages, 0, sizeof(ems_pages));
                   17359:        free_ems_pages = MAX_EMS_PAGES;
                   17360: }
                   17361: 
                   17362: void ems_finish()
                   17363: {
1.1.1.28  root     17364:        ems_release();
                   17365: }
                   17366: 
                   17367: void ems_release()
                   17368: {
1.1.1.31  root     17369:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     17370:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     17371:                        free(ems_handles[i].buffer);
                   17372:                        ems_handles[i].buffer = NULL;
                   17373:                }
                   17374:        }
                   17375: }
                   17376: 
                   17377: void ems_allocate_pages(int handle, int pages)
                   17378: {
                   17379:        if(pages > 0) {
                   17380:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   17381:        } else {
                   17382:                ems_handles[handle].buffer = NULL;
                   17383:        }
                   17384:        ems_handles[handle].pages = pages;
                   17385:        ems_handles[handle].allocated = true;
                   17386:        free_ems_pages -= pages;
                   17387: }
                   17388: 
                   17389: void ems_reallocate_pages(int handle, int pages)
                   17390: {
                   17391:        if(ems_handles[handle].allocated) {
                   17392:                if(ems_handles[handle].pages != pages) {
                   17393:                        UINT8 *new_buffer = NULL;
                   17394:                        
                   17395:                        if(pages > 0) {
                   17396:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   17397:                        }
1.1.1.32  root     17398:                        if(ems_handles[handle].buffer != NULL) {
                   17399:                                if(new_buffer != NULL) {
1.1.1.19  root     17400:                                        if(pages > ems_handles[handle].pages) {
                   17401:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   17402:                                        } else {
                   17403:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   17404:                                        }
                   17405:                                }
                   17406:                                free(ems_handles[handle].buffer);
                   17407:                                ems_handles[handle].buffer = NULL;
                   17408:                        }
                   17409:                        free_ems_pages += ems_handles[handle].pages;
                   17410:                        
                   17411:                        ems_handles[handle].buffer = new_buffer;
                   17412:                        ems_handles[handle].pages = pages;
                   17413:                        free_ems_pages -= pages;
                   17414:                }
                   17415:        } else {
                   17416:                ems_allocate_pages(handle, pages);
                   17417:        }
                   17418: }
                   17419: 
                   17420: void ems_release_pages(int handle)
                   17421: {
                   17422:        if(ems_handles[handle].allocated) {
1.1.1.32  root     17423:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     17424:                        free(ems_handles[handle].buffer);
                   17425:                        ems_handles[handle].buffer = NULL;
                   17426:                }
                   17427:                free_ems_pages += ems_handles[handle].pages;
                   17428:                ems_handles[handle].allocated = false;
                   17429:        }
                   17430: }
                   17431: 
                   17432: void ems_map_page(int physical, int handle, int logical)
                   17433: {
                   17434:        if(ems_pages[physical].mapped) {
                   17435:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   17436:                        return;
                   17437:                }
                   17438:                ems_unmap_page(physical);
                   17439:        }
1.1.1.32  root     17440:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     17441:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   17442:        }
                   17443:        ems_pages[physical].handle = handle;
                   17444:        ems_pages[physical].page = logical;
                   17445:        ems_pages[physical].mapped = true;
                   17446: }
                   17447: 
                   17448: void ems_unmap_page(int physical)
                   17449: {
                   17450:        if(ems_pages[physical].mapped) {
                   17451:                int handle = ems_pages[physical].handle;
                   17452:                int logical = ems_pages[physical].page;
                   17453:                
1.1.1.32  root     17454:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     17455:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   17456:                }
                   17457:                ems_pages[physical].mapped = false;
                   17458:        }
                   17459: }
                   17460: 
1.1.1.25  root     17461: // dma
1.1       root     17462: 
1.1.1.25  root     17463: void dma_init()
1.1       root     17464: {
1.1.1.26  root     17465:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     17466:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     17467: //             for(int ch = 0; ch < 4; ch++) {
                   17468: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   17469: //             }
1.1.1.25  root     17470:                dma_reset(c);
                   17471:        }
1.1       root     17472: }
                   17473: 
1.1.1.25  root     17474: void dma_reset(int c)
1.1       root     17475: {
1.1.1.25  root     17476:        dma[c].low_high = false;
                   17477:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   17478:        dma[c].mask = 0xff;
                   17479: }
                   17480: 
                   17481: void dma_write(int c, UINT32 addr, UINT8 data)
                   17482: {
                   17483:        int ch = (addr >> 1) & 3;
                   17484:        UINT8 bit = 1 << (data & 3);
                   17485:        
                   17486:        switch(addr & 0x0f) {
                   17487:        case 0x00: case 0x02: case 0x04: case 0x06:
                   17488:                if(dma[c].low_high) {
                   17489:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     17490:                } else {
1.1.1.25  root     17491:                        dma[c].ch[ch].bareg.b.l = data;
                   17492:                }
                   17493:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   17494:                dma[c].low_high = !dma[c].low_high;
                   17495:                break;
                   17496:        case 0x01: case 0x03: case 0x05: case 0x07:
                   17497:                if(dma[c].low_high) {
                   17498:                        dma[c].ch[ch].bcreg.b.h = data;
                   17499:                } else {
                   17500:                        dma[c].ch[ch].bcreg.b.l = data;
                   17501:                }
                   17502:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   17503:                dma[c].low_high = !dma[c].low_high;
                   17504:                break;
                   17505:        case 0x08:
                   17506:                // command register
                   17507:                dma[c].cmd = data;
                   17508:                break;
                   17509:        case 0x09:
                   17510:                // dma[c].request register
                   17511:                if(data & 4) {
                   17512:                        if(!(dma[c].req & bit)) {
                   17513:                                dma[c].req |= bit;
                   17514: //                             dma_run(c, ch);
                   17515:                        }
                   17516:                } else {
                   17517:                        dma[c].req &= ~bit;
                   17518:                }
                   17519:                break;
                   17520:        case 0x0a:
                   17521:                // single mask register
                   17522:                if(data & 4) {
                   17523:                        dma[c].mask |= bit;
                   17524:                } else {
                   17525:                        dma[c].mask &= ~bit;
                   17526:                }
                   17527:                break;
                   17528:        case 0x0b:
                   17529:                // mode register
                   17530:                dma[c].ch[data & 3].mode = data;
                   17531:                break;
                   17532:        case 0x0c:
                   17533:                dma[c].low_high = false;
                   17534:                break;
                   17535:        case 0x0d:
                   17536:                // clear master
                   17537:                dma_reset(c);
                   17538:                break;
                   17539:        case 0x0e:
                   17540:                // clear mask register
                   17541:                dma[c].mask = 0;
                   17542:                break;
                   17543:        case 0x0f:
                   17544:                // all mask register
                   17545:                dma[c].mask = data & 0x0f;
                   17546:                break;
                   17547:        }
                   17548: }
                   17549: 
                   17550: UINT8 dma_read(int c, UINT32 addr)
                   17551: {
                   17552:        int ch = (addr >> 1) & 3;
                   17553:        UINT8 val = 0xff;
                   17554:        
                   17555:        switch(addr & 0x0f) {
                   17556:        case 0x00: case 0x02: case 0x04: case 0x06:
                   17557:                if(dma[c].low_high) {
                   17558:                        val = dma[c].ch[ch].areg.b.h;
                   17559:                } else {
                   17560:                        val = dma[c].ch[ch].areg.b.l;
                   17561:                }
                   17562:                dma[c].low_high = !dma[c].low_high;
                   17563:                return(val);
                   17564:        case 0x01: case 0x03: case 0x05: case 0x07:
                   17565:                if(dma[c].low_high) {
                   17566:                        val = dma[c].ch[ch].creg.b.h;
                   17567:                } else {
                   17568:                        val = dma[c].ch[ch].creg.b.l;
                   17569:                }
                   17570:                dma[c].low_high = !dma[c].low_high;
                   17571:                return(val);
                   17572:        case 0x08:
                   17573:                // status register
                   17574:                val = (dma[c].req << 4) | dma[c].tc;
                   17575:                dma[c].tc = 0;
                   17576:                return(val);
                   17577:        case 0x0d:
1.1.1.26  root     17578:                // temporary register (intel 82374 does not support)
1.1.1.25  root     17579:                return(dma[c].tmp & 0xff);
1.1.1.26  root     17580:        case 0x0f:
                   17581:                // mask register (intel 82374 does support)
                   17582:                return(dma[c].mask);
1.1.1.25  root     17583:        }
                   17584:        return(0xff);
                   17585: }
                   17586: 
                   17587: void dma_page_write(int c, int ch, UINT8 data)
                   17588: {
                   17589:        dma[c].ch[ch].pagereg = data;
                   17590: }
                   17591: 
                   17592: UINT8 dma_page_read(int c, int ch)
                   17593: {
                   17594:        return(dma[c].ch[ch].pagereg);
                   17595: }
                   17596: 
                   17597: void dma_run(int c, int ch)
                   17598: {
                   17599:        UINT8 bit = 1 << ch;
                   17600:        
                   17601:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   17602:                // execute dma
                   17603:                while(dma[c].req & bit) {
                   17604:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   17605:                                // memory -> memory
                   17606:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   17607:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   17608:                                
                   17609:                                if(c == 0) {
                   17610:                                        dma[c].tmp = read_byte(saddr);
                   17611:                                        write_byte(daddr, dma[c].tmp);
                   17612:                                } else {
                   17613:                                        dma[c].tmp = read_word(saddr << 1);
                   17614:                                        write_word(daddr << 1, dma[c].tmp);
                   17615:                                }
                   17616:                                if(!(dma[c].cmd & 0x02)) {
                   17617:                                        if(dma[c].ch[0].mode & 0x20) {
                   17618:                                                dma[c].ch[0].areg.w--;
                   17619:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   17620:                                                        dma[c].ch[0].pagereg--;
                   17621:                                                }
                   17622:                                        } else {
                   17623:                                                dma[c].ch[0].areg.w++;
                   17624:                                                if(dma[c].ch[0].areg.w == 0) {
                   17625:                                                        dma[c].ch[0].pagereg++;
                   17626:                                                }
                   17627:                                        }
                   17628:                                }
                   17629:                                if(dma[c].ch[1].mode & 0x20) {
                   17630:                                        dma[c].ch[1].areg.w--;
                   17631:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   17632:                                                dma[c].ch[1].pagereg--;
                   17633:                                        }
                   17634:                                } else {
                   17635:                                        dma[c].ch[1].areg.w++;
                   17636:                                        if(dma[c].ch[1].areg.w == 0) {
                   17637:                                                dma[c].ch[1].pagereg++;
                   17638:                                        }
                   17639:                                }
                   17640:                                
                   17641:                                // check dma condition
                   17642:                                if(dma[c].ch[0].creg.w-- == 0) {
                   17643:                                        if(dma[c].ch[0].mode & 0x10) {
                   17644:                                                // self initialize
                   17645:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   17646:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   17647:                                        } else {
                   17648: //                                             dma[c].mask |= bit;
                   17649:                                        }
                   17650:                                }
                   17651:                                if(dma[c].ch[1].creg.w-- == 0) {
                   17652:                                        // terminal count
                   17653:                                        if(dma[c].ch[1].mode & 0x10) {
                   17654:                                                // self initialize
                   17655:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   17656:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   17657:                                        } else {
                   17658:                                                dma[c].mask |= bit;
                   17659:                                        }
                   17660:                                        dma[c].req &= ~bit;
                   17661:                                        dma[c].tc |= bit;
                   17662:                                }
                   17663:                        } else {
                   17664:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   17665:                                
                   17666:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   17667:                                        // verify
                   17668:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   17669:                                        // io -> memory
                   17670:                                        if(c == 0) {
                   17671:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   17672:                                                write_byte(addr, dma[c].tmp);
                   17673:                                        } else {
                   17674:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   17675:                                                write_word(addr << 1, dma[c].tmp);
                   17676:                                        }
                   17677:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   17678:                                        // memory -> io
                   17679:                                        if(c == 0) {
                   17680:                                                dma[c].tmp = read_byte(addr);
                   17681:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   17682:                                        } else {
                   17683:                                                dma[c].tmp = read_word(addr << 1);
                   17684:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   17685:                                        }
                   17686:                                }
                   17687:                                if(dma[c].ch[ch].mode & 0x20) {
                   17688:                                        dma[c].ch[ch].areg.w--;
                   17689:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   17690:                                                dma[c].ch[ch].pagereg--;
                   17691:                                        }
                   17692:                                } else {
                   17693:                                        dma[c].ch[ch].areg.w++;
                   17694:                                        if(dma[c].ch[ch].areg.w == 0) {
                   17695:                                                dma[c].ch[ch].pagereg++;
                   17696:                                        }
                   17697:                                }
                   17698:                                
                   17699:                                // check dma condition
                   17700:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   17701:                                        // terminal count
                   17702:                                        if(dma[c].ch[ch].mode & 0x10) {
                   17703:                                                // self initialize
                   17704:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   17705:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   17706:                                        } else {
                   17707:                                                dma[c].mask |= bit;
                   17708:                                        }
                   17709:                                        dma[c].req &= ~bit;
                   17710:                                        dma[c].tc |= bit;
                   17711:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   17712:                                        // single mode
                   17713:                                        break;
                   17714:                                }
                   17715:                        }
                   17716:                }
                   17717:        }
                   17718: }
                   17719: 
                   17720: // pic
                   17721: 
                   17722: void pic_init()
                   17723: {
                   17724:        memset(pic, 0, sizeof(pic));
                   17725:        pic[0].imr = pic[1].imr = 0xff;
                   17726:        
                   17727:        // from bochs bios
                   17728:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   17729:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   17730:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   17731:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   17732:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   17733:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   17734:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   17735:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   17736:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   17737: }
                   17738: 
                   17739: void pic_write(int c, UINT32 addr, UINT8 data)
                   17740: {
                   17741:        if(addr & 1) {
                   17742:                if(pic[c].icw2_r) {
                   17743:                        // icw2
                   17744:                        pic[c].icw2 = data;
                   17745:                        pic[c].icw2_r = 0;
                   17746:                } else if(pic[c].icw3_r) {
                   17747:                        // icw3
                   17748:                        pic[c].icw3 = data;
                   17749:                        pic[c].icw3_r = 0;
                   17750:                } else if(pic[c].icw4_r) {
                   17751:                        // icw4
                   17752:                        pic[c].icw4 = data;
                   17753:                        pic[c].icw4_r = 0;
                   17754:                } else {
                   17755:                        // ocw1
1.1       root     17756:                        pic[c].imr = data;
                   17757:                }
                   17758:        } else {
                   17759:                if(data & 0x10) {
                   17760:                        // icw1
                   17761:                        pic[c].icw1 = data;
                   17762:                        pic[c].icw2_r = 1;
                   17763:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   17764:                        pic[c].icw4_r = data & 1;
                   17765:                        pic[c].irr = 0;
                   17766:                        pic[c].isr = 0;
                   17767:                        pic[c].imr = 0;
                   17768:                        pic[c].prio = 0;
                   17769:                        if(!(pic[c].icw1 & 1)) {
                   17770:                                pic[c].icw4 = 0;
                   17771:                        }
                   17772:                        pic[c].ocw3 = 0;
                   17773:                } else if(data & 8) {
                   17774:                        // ocw3
                   17775:                        if(!(data & 2)) {
                   17776:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   17777:                        }
                   17778:                        if(!(data & 0x40)) {
                   17779:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   17780:                        }
                   17781:                        pic[c].ocw3 = data;
                   17782:                } else {
                   17783:                        // ocw2
                   17784:                        int level = 0;
                   17785:                        if(data & 0x40) {
                   17786:                                level = data & 7;
                   17787:                        } else {
                   17788:                                if(!pic[c].isr) {
                   17789:                                        return;
                   17790:                                }
                   17791:                                level = pic[c].prio;
                   17792:                                while(!(pic[c].isr & (1 << level))) {
                   17793:                                        level = (level + 1) & 7;
                   17794:                                }
                   17795:                        }
                   17796:                        if(data & 0x80) {
                   17797:                                pic[c].prio = (level + 1) & 7;
                   17798:                        }
                   17799:                        if(data & 0x20) {
                   17800:                                pic[c].isr &= ~(1 << level);
                   17801:                        }
                   17802:                }
                   17803:        }
                   17804:        pic_update();
                   17805: }
                   17806: 
                   17807: UINT8 pic_read(int c, UINT32 addr)
                   17808: {
                   17809:        if(addr & 1) {
                   17810:                return(pic[c].imr);
                   17811:        } else {
                   17812:                // polling mode is not supported...
                   17813:                //if(pic[c].ocw3 & 4) {
                   17814:                //      return ???;
                   17815:                //}
                   17816:                if(pic[c].ocw3 & 1) {
                   17817:                        return(pic[c].isr);
                   17818:                } else {
                   17819:                        return(pic[c].irr);
                   17820:                }
                   17821:        }
                   17822: }
                   17823: 
                   17824: void pic_req(int c, int level, int signal)
                   17825: {
                   17826:        if(signal) {
                   17827:                pic[c].irr |= (1 << level);
                   17828:        } else {
                   17829:                pic[c].irr &= ~(1 << level);
                   17830:        }
                   17831:        pic_update();
                   17832: }
                   17833: 
                   17834: int pic_ack()
                   17835: {
                   17836:        // ack (INTA=L)
                   17837:        pic[pic_req_chip].isr |= pic_req_bit;
                   17838:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   17839:        if(pic_req_chip > 0) {
                   17840:                // update isr and irr of master
                   17841:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   17842:                pic[pic_req_chip - 1].isr |= slave;
                   17843:                pic[pic_req_chip - 1].irr &= ~slave;
                   17844:        }
                   17845:        //if(pic[pic_req_chip].icw4 & 1) {
                   17846:                // 8086 mode
                   17847:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   17848:        //} else {
                   17849:        //      // 8080 mode
                   17850:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   17851:        //      if(pic[pic_req_chip].icw1 & 4) {
                   17852:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   17853:        //      } else {
                   17854:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   17855:        //      }
                   17856:        //      vector = 0xcd | (addr << 8);
                   17857:        //}
                   17858:        if(pic[pic_req_chip].icw4 & 2) {
                   17859:                // auto eoi
                   17860:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   17861:        }
                   17862:        return(vector);
                   17863: }
                   17864: 
                   17865: void pic_update()
                   17866: {
                   17867:        for(int c = 0; c < 2; c++) {
                   17868:                UINT8 irr = pic[c].irr;
                   17869:                if(c + 1 < 2) {
                   17870:                        // this is master
                   17871:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   17872:                                // request from slave
                   17873:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   17874:                        }
                   17875:                }
                   17876:                irr &= (~pic[c].imr);
                   17877:                if(!irr) {
                   17878:                        break;
                   17879:                }
                   17880:                if(!(pic[c].ocw3 & 0x20)) {
                   17881:                        irr |= pic[c].isr;
                   17882:                }
                   17883:                int level = pic[c].prio;
                   17884:                UINT8 bit = 1 << level;
                   17885:                while(!(irr & bit)) {
                   17886:                        level = (level + 1) & 7;
                   17887:                        bit = 1 << level;
                   17888:                }
                   17889:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   17890:                        // check slave
                   17891:                        continue;
                   17892:                }
                   17893:                if(pic[c].isr & bit) {
                   17894:                        break;
                   17895:                }
                   17896:                // interrupt request
                   17897:                pic_req_chip = c;
                   17898:                pic_req_level = level;
                   17899:                pic_req_bit = bit;
1.1.1.3   root     17900:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     17901:                return;
                   17902:        }
1.1.1.3   root     17903:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     17904: }
1.1       root     17905: 
1.1.1.25  root     17906: // pio
                   17907: 
                   17908: void pio_init()
                   17909: {
1.1.1.38  root     17910: //     bool conv_mode = (GetConsoleCP() == 932);
                   17911:        
1.1.1.26  root     17912:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     17913:        
1.1.1.25  root     17914:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     17915:                pio[c].stat = 0xdf;
1.1.1.25  root     17916:                pio[c].ctrl = 0x0c;
1.1.1.38  root     17917: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     17918:        }
                   17919: }
                   17920: 
1.1.1.37  root     17921: void pio_finish()
                   17922: {
                   17923:        pio_release();
                   17924: }
                   17925: 
                   17926: void pio_release()
                   17927: {
                   17928:        for(int c = 0; c < 2; c++) {
                   17929:                if(pio[c].fp != NULL) {
1.1.1.38  root     17930:                        if(pio[c].jis_mode) {
                   17931:                                fputc(0x1c, pio[c].fp);
                   17932:                                fputc(0x2e, pio[c].fp);
                   17933:                        }
1.1.1.37  root     17934:                        fclose(pio[c].fp);
                   17935:                        pio[c].fp = NULL;
                   17936:                }
                   17937:        }
                   17938: }
                   17939: 
1.1.1.25  root     17940: void pio_write(int c, UINT32 addr, UINT8 data)
                   17941: {
                   17942:        switch(addr & 3) {
                   17943:        case 0:
                   17944:                pio[c].data = data;
                   17945:                break;
                   17946:        case 2:
1.1.1.37  root     17947:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   17948:                        // strobe H -> L
                   17949:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   17950:                                // auto feed
                   17951:                                printer_out(c, 0x0d);
                   17952:                                printer_out(c, 0x0a);
                   17953:                        } else {
                   17954:                                printer_out(c, pio[c].data);
                   17955:                        }
                   17956:                        pio[c].stat &= ~0x40; // set ack
                   17957:                }
1.1.1.25  root     17958:                pio[c].ctrl = data;
                   17959:                break;
                   17960:        }
                   17961: }
                   17962: 
                   17963: UINT8 pio_read(int c, UINT32 addr)
                   17964: {
                   17965:        switch(addr & 3) {
                   17966:        case 0:
1.1.1.37  root     17967:                if(pio[c].ctrl & 0x20) {
                   17968:                        // input mode
                   17969:                        return(0xff);
                   17970:                }
1.1.1.25  root     17971:                return(pio[c].data);
                   17972:        case 1:
1.1.1.37  root     17973:                {
                   17974:                        UINT8 stat = pio[c].stat;
                   17975:                        pio[c].stat |= 0x40; // clear ack
                   17976:                        return(stat);
                   17977:                }
1.1.1.25  root     17978:        case 2:
                   17979:                return(pio[c].ctrl);
                   17980:        }
                   17981:        return(0xff);
                   17982: }
                   17983: 
1.1.1.37  root     17984: void printer_out(int c, UINT8 data)
                   17985: {
                   17986:        SYSTEMTIME time;
1.1.1.38  root     17987:        bool jis_mode = false;
1.1.1.37  root     17988:        
                   17989:        GetLocalTime(&time);
                   17990:        
                   17991:        if(pio[c].fp != NULL) {
                   17992:                // if at least 1000ms passed from last written, close the current file
                   17993:                FILETIME ftime1;
                   17994:                FILETIME ftime2;
                   17995:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   17996:                SystemTimeToFileTime(&time, &ftime2);
                   17997:                INT64 *time1 = (INT64 *)&ftime1;
                   17998:                INT64 *time2 = (INT64 *)&ftime2;
                   17999:                INT64 msec = (*time2 - *time1) / 10000;
                   18000:                
                   18001:                if(msec >= 1000) {
1.1.1.38  root     18002:                        if(pio[c].jis_mode) {
                   18003:                                fputc(0x1c, pio[c].fp);
                   18004:                                fputc(0x2e, pio[c].fp);
                   18005:                                jis_mode = true;
                   18006:                        }
1.1.1.37  root     18007:                        fclose(pio[c].fp);
                   18008:                        pio[c].fp = NULL;
                   18009:                }
                   18010:        }
                   18011:        if(pio[c].fp == NULL) {
                   18012:                // create a new file in the temp folder
                   18013:                char file_name[MAX_PATH];
                   18014:                
                   18015:                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);
                   18016:                if(GetTempPath(MAX_PATH, pio[c].path)) {
                   18017:                        strcat(pio[c].path, file_name);
                   18018:                } else {
                   18019:                        strcpy(pio[c].path, file_name);
                   18020:                }
1.1.1.38  root     18021:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     18022:        }
                   18023:        if(pio[c].fp != NULL) {
1.1.1.38  root     18024:                if(jis_mode) {
                   18025:                        fputc(0x1c, pio[c].fp);
                   18026:                        fputc(0x26, pio[c].fp);
                   18027:                }
1.1.1.37  root     18028:                fputc(data, pio[c].fp);
1.1.1.38  root     18029:                
                   18030:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   18031:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   18032:                        UINT8 buffer[4];
                   18033:                        fseek(pio[c].fp, 0, SEEK_SET);
                   18034:                        fread(buffer, 4, 1, pio[c].fp);
                   18035:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   18036:                                fclose(pio[c].fp);
                   18037:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   18038:                        }
                   18039:                }
1.1.1.37  root     18040:                pio[c].time = time;
                   18041:        }
                   18042: }
                   18043: 
1.1       root     18044: // pit
                   18045: 
1.1.1.22  root     18046: #define PIT_FREQ 1193182ULL
1.1       root     18047: #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)
                   18048: 
                   18049: void pit_init()
                   18050: {
1.1.1.8   root     18051:        memset(pit, 0, sizeof(pit));
1.1       root     18052:        for(int ch = 0; ch < 3; ch++) {
                   18053:                pit[ch].count = 0x10000;
                   18054:                pit[ch].ctrl_reg = 0x34;
                   18055:                pit[ch].mode = 3;
                   18056:        }
                   18057:        
                   18058:        // from bochs bios
                   18059:        pit_write(3, 0x34);
                   18060:        pit_write(0, 0x00);
                   18061:        pit_write(0, 0x00);
                   18062: }
                   18063: 
                   18064: void pit_write(int ch, UINT8 val)
                   18065: {
1.1.1.8   root     18066: #ifndef PIT_ALWAYS_RUNNING
1.1       root     18067:        if(!pit_active) {
                   18068:                pit_active = 1;
                   18069:                pit_init();
                   18070:        }
1.1.1.8   root     18071: #endif
1.1       root     18072:        switch(ch) {
                   18073:        case 0:
                   18074:        case 1:
                   18075:        case 2:
                   18076:                // write count register
                   18077:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   18078:                        if(pit[ch].ctrl_reg & 0x10) {
                   18079:                                pit[ch].low_write = 1;
                   18080:                        }
                   18081:                        if(pit[ch].ctrl_reg & 0x20) {
                   18082:                                pit[ch].high_write = 1;
                   18083:                        }
                   18084:                }
                   18085:                if(pit[ch].low_write) {
                   18086:                        pit[ch].count_reg = val;
                   18087:                        pit[ch].low_write = 0;
                   18088:                } else if(pit[ch].high_write) {
                   18089:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   18090:                                pit[ch].count_reg = val << 8;
                   18091:                        } else {
                   18092:                                pit[ch].count_reg |= val << 8;
                   18093:                        }
                   18094:                        pit[ch].high_write = 0;
                   18095:                }
                   18096:                // start count
1.1.1.8   root     18097:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   18098:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   18099:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   18100:                                pit[ch].prev_time = timeGetTime();
                   18101:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     18102:                        }
                   18103:                }
                   18104:                break;
                   18105:        case 3: // ctrl reg
                   18106:                if((val & 0xc0) == 0xc0) {
                   18107:                        // i8254 read-back command
                   18108:                        for(ch = 0; ch < 3; ch++) {
                   18109:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   18110:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   18111:                                        pit[ch].status_latched = 1;
                   18112:                                }
                   18113:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   18114:                                        pit_latch_count(ch);
                   18115:                                }
                   18116:                        }
                   18117:                        break;
                   18118:                }
                   18119:                ch = (val >> 6) & 3;
                   18120:                if(val & 0x30) {
1.1.1.35  root     18121:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     18122:                        pit[ch].mode = modes[(val >> 1) & 7];
                   18123:                        pit[ch].count_latched = 0;
                   18124:                        pit[ch].low_read = pit[ch].high_read = 0;
                   18125:                        pit[ch].low_write = pit[ch].high_write = 0;
                   18126:                        pit[ch].ctrl_reg = val;
                   18127:                        // stop count
1.1.1.8   root     18128:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     18129:                        pit[ch].count_reg = 0;
                   18130:                } else if(!pit[ch].count_latched) {
                   18131:                        pit_latch_count(ch);
                   18132:                }
                   18133:                break;
                   18134:        }
                   18135: }
                   18136: 
                   18137: UINT8 pit_read(int ch)
                   18138: {
1.1.1.8   root     18139: #ifndef PIT_ALWAYS_RUNNING
1.1       root     18140:        if(!pit_active) {
                   18141:                pit_active = 1;
                   18142:                pit_init();
                   18143:        }
1.1.1.8   root     18144: #endif
1.1       root     18145:        switch(ch) {
                   18146:        case 0:
                   18147:        case 1:
                   18148:        case 2:
                   18149:                if(pit[ch].status_latched) {
                   18150:                        pit[ch].status_latched = 0;
                   18151:                        return(pit[ch].status);
                   18152:                }
                   18153:                // if not latched, through current count
                   18154:                if(!pit[ch].count_latched) {
                   18155:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   18156:                                pit_latch_count(ch);
                   18157:                        }
                   18158:                }
                   18159:                // return latched count
                   18160:                if(pit[ch].low_read) {
                   18161:                        pit[ch].low_read = 0;
                   18162:                        if(!pit[ch].high_read) {
                   18163:                                pit[ch].count_latched = 0;
                   18164:                        }
                   18165:                        return(pit[ch].latch & 0xff);
                   18166:                } else if(pit[ch].high_read) {
                   18167:                        pit[ch].high_read = 0;
                   18168:                        pit[ch].count_latched = 0;
                   18169:                        return((pit[ch].latch >> 8) & 0xff);
                   18170:                }
                   18171:        }
                   18172:        return(0xff);
                   18173: }
                   18174: 
1.1.1.8   root     18175: int pit_run(int ch, UINT32 cur_time)
1.1       root     18176: {
1.1.1.8   root     18177:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     18178:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     18179:                pit[ch].prev_time = pit[ch].expired_time;
                   18180:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   18181:                if(cur_time >= pit[ch].expired_time) {
                   18182:                        pit[ch].prev_time = cur_time;
                   18183:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     18184:                }
1.1.1.8   root     18185:                return(1);
1.1       root     18186:        }
1.1.1.8   root     18187:        return(0);
1.1       root     18188: }
                   18189: 
                   18190: void pit_latch_count(int ch)
                   18191: {
1.1.1.8   root     18192:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     18193:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     18194:                pit_run(ch, cur_time);
                   18195:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     18196:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   18197:                
                   18198:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   18199:                        // decrement counter in 1msec period
                   18200:                        if(pit[ch].next_latch == 0) {
                   18201:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   18202:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   18203:                        }
                   18204:                        if(pit[ch].latch > pit[ch].next_latch) {
                   18205:                                pit[ch].latch--;
                   18206:                        }
                   18207:                } else {
                   18208:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   18209:                        pit[ch].next_latch = 0;
                   18210:                }
1.1.1.8   root     18211:        } else {
                   18212:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     18213:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     18214:        }
                   18215:        pit[ch].count_latched = 1;
                   18216:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   18217:                // lower byte
                   18218:                pit[ch].low_read = 1;
                   18219:                pit[ch].high_read = 0;
                   18220:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   18221:                // upper byte
                   18222:                pit[ch].low_read = 0;
                   18223:                pit[ch].high_read = 1;
                   18224:        } else {
                   18225:                // lower -> upper
1.1.1.14  root     18226:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     18227:        }
                   18228: }
                   18229: 
1.1.1.8   root     18230: int pit_get_expired_time(int ch)
1.1       root     18231: {
1.1.1.22  root     18232:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   18233:        UINT64 val = pit[ch].accum >> 10;
                   18234:        pit[ch].accum -= val << 10;
                   18235:        return((val != 0) ? val : 1);
1.1.1.8   root     18236: }
                   18237: 
1.1.1.25  root     18238: // sio
                   18239: 
                   18240: void sio_init()
                   18241: {
1.1.1.26  root     18242:        memset(sio, 0, sizeof(sio));
                   18243:        memset(sio_mt, 0, sizeof(sio_mt));
                   18244:        
1.1.1.29  root     18245:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     18246:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   18247:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   18248:                
                   18249:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   18250:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     18251:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   18252:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     18253:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   18254:                sio[c].irq_identify = 0x01;     // no pending irq
                   18255:                
                   18256:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   18257:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   18258:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   18259:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   18260:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   18261:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   18262:                
1.1.1.26  root     18263:                if(sio_port_number[c] != 0) {
1.1.1.25  root     18264:                        sio[c].channel = c;
                   18265:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   18266:                }
                   18267:        }
                   18268: }
                   18269: 
                   18270: void sio_finish()
                   18271: {
1.1.1.29  root     18272:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     18273:                if(sio_mt[c].hThread != NULL) {
                   18274:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   18275:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     18276:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     18277:                }
                   18278:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   18279:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   18280:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   18281:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   18282:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   18283:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     18284:        }
                   18285:        sio_release();
                   18286: }
                   18287: 
                   18288: void sio_release()
                   18289: {
1.1.1.29  root     18290:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     18291:                // sio_thread() may access the resources :-(
1.1.1.32  root     18292:                bool running = (sio_mt[c].hThread != NULL);
                   18293:                
                   18294:                if(running) {
                   18295:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   18296:                }
                   18297:                if(sio[c].send_buffer != NULL) {
                   18298:                        sio[c].send_buffer->release();
                   18299:                        delete sio[c].send_buffer;
                   18300:                        sio[c].send_buffer = NULL;
                   18301:                }
                   18302:                if(running) {
                   18303:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   18304:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   18305:                }
                   18306:                if(sio[c].recv_buffer != NULL) {
                   18307:                        sio[c].recv_buffer->release();
                   18308:                        delete sio[c].recv_buffer;
                   18309:                        sio[c].recv_buffer = NULL;
                   18310:                }
                   18311:                if(running) {
                   18312:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     18313:                }
1.1.1.25  root     18314:        }
                   18315: }
                   18316: 
                   18317: void sio_write(int c, UINT32 addr, UINT8 data)
                   18318: {
                   18319:        switch(addr & 7) {
                   18320:        case 0:
                   18321:                if(sio[c].selector & 0x80) {
                   18322:                        if(sio[c].divisor.b.l != data) {
                   18323:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   18324:                                sio[c].divisor.b.l = data;
                   18325:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   18326:                        }
                   18327:                } else {
                   18328:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     18329:                        if(sio[c].send_buffer != NULL) {
                   18330:                                sio[c].send_buffer->write(data);
                   18331:                        }
1.1.1.25  root     18332:                        // transmitter holding/shift registers are not empty
                   18333:                        sio[c].line_stat_buf &= ~0x60;
                   18334:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   18335:                        
                   18336:                        if(sio[c].irq_enable & 0x02) {
                   18337:                                sio_update_irq(c);
                   18338:                        }
                   18339:                }
                   18340:                break;
                   18341:        case 1:
                   18342:                if(sio[c].selector & 0x80) {
                   18343:                        if(sio[c].divisor.b.h != data) {
                   18344:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   18345:                                sio[c].divisor.b.h = data;
                   18346:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   18347:                        }
                   18348:                } else {
                   18349:                        if(sio[c].irq_enable != data) {
                   18350:                                sio[c].irq_enable = data;
                   18351:                                sio_update_irq(c);
                   18352:                        }
                   18353:                }
                   18354:                break;
                   18355:        case 3:
                   18356:                {
                   18357:                        UINT8 line_ctrl = data & 0x3f;
                   18358:                        bool set_brk = ((data & 0x40) != 0);
                   18359:                        
                   18360:                        if(sio[c].line_ctrl != line_ctrl) {
                   18361:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   18362:                                sio[c].line_ctrl = line_ctrl;
                   18363:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   18364:                        }
                   18365:                        if(sio[c].set_brk != set_brk) {
                   18366:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   18367:                                sio[c].set_brk = set_brk;
                   18368:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   18369:                        }
                   18370:                }
                   18371:                sio[c].selector = data;
                   18372:                break;
                   18373:        case 4:
                   18374:                {
                   18375:                        bool set_dtr = ((data & 0x01) != 0);
                   18376:                        bool set_rts = ((data & 0x02) != 0);
                   18377:                        
                   18378:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     18379: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     18380:                                sio[c].set_dtr = set_dtr;
                   18381:                                sio[c].set_rts = set_rts;
1.1.1.26  root     18382: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   18383:                                
                   18384:                                bool state_changed = false;
                   18385:                                
                   18386:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   18387:                                if(set_dtr) {
                   18388:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   18389:                                } else {
                   18390:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   18391:                                }
                   18392:                                if(set_rts) {
                   18393:                                        sio[c].modem_stat |= 0x10;      // cts on
                   18394:                                } else {
                   18395:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   18396:                                }
                   18397:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   18398:                                        if(!(sio[c].modem_stat & 0x02)) {
                   18399:                                                if(sio[c].irq_enable & 0x08) {
                   18400:                                                        state_changed = true;
                   18401:                                                }
                   18402:                                                sio[c].modem_stat |= 0x02;
                   18403:                                        }
                   18404:                                }
                   18405:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   18406:                                        if(!(sio[c].modem_stat & 0x01)) {
                   18407:                                                if(sio[c].irq_enable & 0x08) {
                   18408:                                                        state_changed = true;
                   18409:                                                }
                   18410:                                                sio[c].modem_stat |= 0x01;
                   18411:                                        }
                   18412:                                }
                   18413:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   18414:                                
                   18415:                                if(state_changed) {
                   18416:                                        sio_update_irq(c);
                   18417:                                }
1.1.1.25  root     18418:                        }
                   18419:                }
                   18420:                sio[c].modem_ctrl = data;
                   18421:                break;
                   18422:        case 7:
                   18423:                sio[c].scratch = data;
                   18424:                break;
                   18425:        }
                   18426: }
                   18427: 
                   18428: UINT8 sio_read(int c, UINT32 addr)
                   18429: {
                   18430:        switch(addr & 7) {
                   18431:        case 0:
                   18432:                if(sio[c].selector & 0x80) {
                   18433:                        return(sio[c].divisor.b.l);
                   18434:                } else {
                   18435:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     18436:                        UINT8 data = 0;
                   18437:                        if(sio[c].recv_buffer != NULL) {
                   18438:                                data = sio[c].recv_buffer->read();
                   18439:                        }
1.1.1.25  root     18440:                        // data is not ready
                   18441:                        sio[c].line_stat_buf &= ~0x01;
                   18442:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   18443:                        
                   18444:                        if(sio[c].irq_enable & 0x01) {
                   18445:                                sio_update_irq(c);
                   18446:                        }
                   18447:                        return(data);
                   18448:                }
                   18449:        case 1:
                   18450:                if(sio[c].selector & 0x80) {
                   18451:                        return(sio[c].divisor.b.h);
                   18452:                } else {
                   18453:                        return(sio[c].irq_enable);
                   18454:                }
                   18455:        case 2:
                   18456:                return(sio[c].irq_identify);
                   18457:        case 3:
                   18458:                return(sio[c].selector);
                   18459:        case 4:
                   18460:                return(sio[c].modem_ctrl);
                   18461:        case 5:
                   18462:                {
                   18463:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   18464:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   18465:                        sio[c].line_stat_err = 0x00;
                   18466:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   18467:                        
                   18468:                        bool state_changed = false;
                   18469:                        
                   18470:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   18471:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     18472:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     18473:                                        // transmitter holding register will be empty first
                   18474:                                        if(sio[c].irq_enable & 0x02) {
                   18475:                                                state_changed = true;
                   18476:                                        }
                   18477:                                        sio[c].line_stat_buf |= 0x20;
                   18478:                                }
                   18479:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   18480:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   18481:                                // transmitter shift register will be empty later
                   18482:                                sio[c].line_stat_buf |= 0x40;
                   18483:                        }
                   18484:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   18485:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     18486:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     18487:                                        // data is ready
                   18488:                                        if(sio[c].irq_enable & 0x01) {
                   18489:                                                state_changed = true;
                   18490:                                        }
                   18491:                                        sio[c].line_stat_buf |= 0x01;
                   18492:                                }
                   18493:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   18494:                        }
                   18495:                        if(state_changed) {
                   18496:                                sio_update_irq(c);
                   18497:                        }
                   18498:                        return(val);
                   18499:                }
                   18500:        case 6:
                   18501:                {
                   18502:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   18503:                        UINT8 val = sio[c].modem_stat;
                   18504:                        sio[c].modem_stat &= 0xf0;
                   18505:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   18506:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   18507:                        
                   18508:                        if(sio[c].modem_ctrl & 0x10) {
                   18509:                                // loop-back
                   18510:                                val &= 0x0f;
                   18511:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   18512:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   18513:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   18514:                        }
                   18515:                        return(val);
                   18516:                }
                   18517:        case 7:
                   18518:                return(sio[c].scratch);
                   18519:        }
                   18520:        return(0xff);
                   18521: }
                   18522: 
                   18523: void sio_update(int c)
                   18524: {
                   18525:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   18526:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     18527:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     18528:                        // transmitter holding/shift registers will be empty
                   18529:                        sio[c].line_stat_buf |= 0x60;
                   18530:                }
                   18531:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   18532:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   18533:                // transmitter shift register will be empty
                   18534:                sio[c].line_stat_buf |= 0x40;
                   18535:        }
                   18536:        if(!(sio[c].line_stat_buf & 0x01)) {
                   18537:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     18538:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     18539:                        // data is ready
                   18540:                        sio[c].line_stat_buf |= 0x01;
                   18541:                }
                   18542:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   18543:        }
                   18544:        sio_update_irq(c);
                   18545: }
                   18546: 
                   18547: void sio_update_irq(int c)
                   18548: {
                   18549:        int level = -1;
                   18550:        
                   18551:        if(sio[c].irq_enable & 0x08) {
                   18552:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   18553:                if((sio[c].modem_stat & 0x0f) != 0) {
                   18554:                        level = 0;
                   18555:                }
                   18556:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   18557:        }
                   18558:        if(sio[c].irq_enable & 0x02) {
                   18559:                if(sio[c].line_stat_buf & 0x20) {
                   18560:                        level = 1;
                   18561:                }
                   18562:        }
                   18563:        if(sio[c].irq_enable & 0x01) {
                   18564:                if(sio[c].line_stat_buf & 0x01) {
                   18565:                        level = 2;
                   18566:                }
                   18567:        }
                   18568:        if(sio[c].irq_enable & 0x04) {
                   18569:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   18570:                if(sio[c].line_stat_err != 0) {
                   18571:                        level = 3;
                   18572:                }
                   18573:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   18574:        }
1.1.1.29  root     18575:        
                   18576:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     18577:        if(level != -1) {
                   18578:                sio[c].irq_identify = level << 1;
1.1.1.29  root     18579:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     18580:        } else {
                   18581:                sio[c].irq_identify = 1;
1.1.1.29  root     18582:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     18583:        }
                   18584: }
                   18585: 
                   18586: DWORD WINAPI sio_thread(void *lpx)
                   18587: {
                   18588:        volatile sio_t *p = (sio_t *)lpx;
                   18589:        sio_mt_t *q = &sio_mt[p->channel];
                   18590:        
                   18591:        char name[] = "COM1";
1.1.1.26  root     18592:        name[3] = '0' + sio_port_number[p->channel];
                   18593:        HANDLE hComm = NULL;
                   18594:        COMMPROP commProp;
                   18595:        DCB dcb;
                   18596:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   18597:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   18598:        
                   18599:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   18600:                if(GetCommProperties(hComm, &commProp)) {
                   18601:                        dwSettableBaud = commProp.dwSettableBaud;
                   18602:                }
1.1.1.25  root     18603:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     18604: //             EscapeCommFunction(hComm, SETRTS);
                   18605: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     18606:                
                   18607:                while(!m_halted) {
                   18608:                        // setup comm port
                   18609:                        bool comm_state_changed = false;
                   18610:                        
                   18611:                        EnterCriticalSection(&q->csLineCtrl);
                   18612:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   18613:                                p->prev_divisor = p->divisor.w;
                   18614:                                p->prev_line_ctrl = p->line_ctrl;
                   18615:                                comm_state_changed = true;
                   18616:                        }
                   18617:                        LeaveCriticalSection(&q->csLineCtrl);
                   18618:                        
                   18619:                        if(comm_state_changed) {
1.1.1.26  root     18620:                                if(GetCommState(hComm, &dcb)) {
                   18621: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   18622:                                        DWORD baud = 115200 / p->prev_divisor;
                   18623:                                        dcb.BaudRate = 9600; // default
                   18624:                                        
                   18625:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   18626:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   18627:                                        // 134.5bps is not supported ???
                   18628: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   18629:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   18630:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   18631:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   18632:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   18633:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   18634:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   18635:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   18636:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   18637:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   18638: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   18639: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   18640: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   18641:                                        
                   18642:                                        switch(p->prev_line_ctrl & 0x03) {
                   18643:                                        case 0x00: dcb.ByteSize = 5; break;
                   18644:                                        case 0x01: dcb.ByteSize = 6; break;
                   18645:                                        case 0x02: dcb.ByteSize = 7; break;
                   18646:                                        case 0x03: dcb.ByteSize = 8; break;
                   18647:                                        }
                   18648:                                        switch(p->prev_line_ctrl & 0x04) {
                   18649:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   18650:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   18651:                                        }
                   18652:                                        switch(p->prev_line_ctrl & 0x38) {
                   18653:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   18654:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   18655:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   18656:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   18657:                                        default:   dcb.Parity = NOPARITY;    break;
                   18658:                                        }
                   18659:                                        dcb.fBinary = TRUE;
                   18660:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   18661:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   18662:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   18663:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   18664:                                        dcb.fTXContinueOnXoff = TRUE;
                   18665:                                        dcb.fOutX = dcb.fInX = FALSE;
                   18666:                                        dcb.fErrorChar = FALSE;
                   18667:                                        dcb.fNull = FALSE;
                   18668:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   18669:                                        dcb.fAbortOnError = FALSE;
                   18670:                                        
                   18671:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     18672:                                }
                   18673:                                
                   18674:                                // check again to apply all comm state changes
                   18675:                                Sleep(10);
                   18676:                                continue;
                   18677:                        }
                   18678:                        
                   18679:                        // set comm pins
                   18680:                        bool change_brk = false;
1.1.1.26  root     18681: //                     bool change_rts = false;
                   18682: //                     bool change_dtr = false;
1.1.1.25  root     18683:                        
                   18684:                        EnterCriticalSection(&q->csModemCtrl);
                   18685:                        if(p->prev_set_brk != p->set_brk) {
                   18686:                                p->prev_set_brk = p->set_brk;
                   18687:                                change_brk = true;
                   18688:                        }
1.1.1.26  root     18689: //                     if(p->prev_set_rts != p->set_rts) {
                   18690: //                             p->prev_set_rts = p->set_rts;
                   18691: //                             change_rts = true;
                   18692: //                     }
                   18693: //                     if(p->prev_set_dtr != p->set_dtr) {
                   18694: //                             p->prev_set_dtr = p->set_dtr;
                   18695: //                             change_dtr = true;
                   18696: //                     }
1.1.1.25  root     18697:                        LeaveCriticalSection(&q->csModemCtrl);
                   18698:                        
                   18699:                        if(change_brk) {
1.1.1.26  root     18700:                                static UINT32 clear_time = 0;
                   18701:                                if(p->prev_set_brk) {
                   18702:                                        EscapeCommFunction(hComm, SETBREAK);
                   18703:                                        clear_time = timeGetTime() + 200;
                   18704:                                } else {
                   18705:                                        // keep break for at least 200msec
                   18706:                                        UINT32 cur_time = timeGetTime();
                   18707:                                        if(clear_time > cur_time) {
                   18708:                                                Sleep(clear_time - cur_time);
                   18709:                                        }
                   18710:                                        EscapeCommFunction(hComm, CLRBREAK);
                   18711:                                }
1.1.1.25  root     18712:                        }
1.1.1.26  root     18713: //                     if(change_rts) {
                   18714: //                             if(p->prev_set_rts) {
                   18715: //                                     EscapeCommFunction(hComm, SETRTS);
                   18716: //                             } else {
                   18717: //                                     EscapeCommFunction(hComm, CLRRTS);
                   18718: //                             }
                   18719: //                     }
                   18720: //                     if(change_dtr) {
                   18721: //                             if(p->prev_set_dtr) {
                   18722: //                                     EscapeCommFunction(hComm, SETDTR);
                   18723: //                             } else {
                   18724: //                                     EscapeCommFunction(hComm, CLRDTR);
                   18725: //                             }
                   18726: //                     }
1.1.1.25  root     18727:                        
                   18728:                        // get comm pins
                   18729:                        DWORD dwModemStat = 0;
                   18730:                        
                   18731:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   18732:                                EnterCriticalSection(&q->csModemStat);
                   18733:                                if(dwModemStat & MS_RLSD_ON) {
                   18734:                                        p->modem_stat |= 0x80;
                   18735:                                } else {
                   18736:                                        p->modem_stat &= ~0x80;
                   18737:                                }
                   18738:                                if(dwModemStat & MS_RING_ON) {
                   18739:                                        p->modem_stat |= 0x40;
                   18740:                                } else {
                   18741:                                        p->modem_stat &= ~0x40;
                   18742:                                }
1.1.1.26  root     18743: //                             if(dwModemStat & MS_DSR_ON) {
                   18744: //                                     p->modem_stat |= 0x20;
                   18745: //                             } else {
                   18746: //                                     p->modem_stat &= ~0x20;
                   18747: //                             }
                   18748: //                             if(dwModemStat & MS_CTS_ON) {
                   18749: //                                     p->modem_stat |= 0x10;
                   18750: //                             } else {
                   18751: //                                     p->modem_stat &= ~0x10;
                   18752: //                             }
1.1.1.25  root     18753:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   18754:                                        p->modem_stat |= 0x08;
                   18755:                                }
                   18756:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   18757:                                        p->modem_stat |= 0x04;
                   18758:                                }
1.1.1.26  root     18759: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   18760: //                                     p->modem_stat |= 0x02;
                   18761: //                             }
                   18762: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   18763: //                                     p->modem_stat |= 0x01;
                   18764: //                             }
1.1.1.25  root     18765:                                LeaveCriticalSection(&q->csModemStat);
                   18766:                        }
                   18767:                        
                   18768:                        // send data
                   18769:                        DWORD dwSend = 0;
                   18770:                        
                   18771:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     18772:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     18773:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   18774:                        }
                   18775:                        LeaveCriticalSection(&q->csSendData);
                   18776:                        
                   18777:                        if(dwSend != 0) {
                   18778:                                DWORD dwWritten = 0;
                   18779:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   18780:                        }
                   18781:                        
                   18782:                        // get line status and recv data
                   18783:                        DWORD dwLineStat = 0;
                   18784:                        COMSTAT comStat;
                   18785:                        
                   18786:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   18787:                                EnterCriticalSection(&q->csLineStat);
                   18788:                                if(dwLineStat & CE_BREAK) {
                   18789:                                        p->line_stat_err |= 0x10;
                   18790:                                }
                   18791:                                if(dwLineStat & CE_FRAME) {
                   18792:                                        p->line_stat_err |= 0x08;
                   18793:                                }
                   18794:                                if(dwLineStat & CE_RXPARITY) {
                   18795:                                        p->line_stat_err |= 0x04;
                   18796:                                }
                   18797:                                if(dwLineStat & CE_OVERRUN) {
                   18798:                                        p->line_stat_err |= 0x02;
                   18799:                                }
                   18800:                                LeaveCriticalSection(&q->csLineStat);
                   18801:                                
                   18802:                                if(comStat.cbInQue != 0) {
                   18803:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     18804:                                        DWORD dwRecv = 0;
                   18805:                                        if(p->recv_buffer != NULL) {
                   18806:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   18807:                                        }
1.1.1.25  root     18808:                                        LeaveCriticalSection(&q->csRecvData);
                   18809:                                        
                   18810:                                        if(dwRecv != 0) {
                   18811:                                                DWORD dwRead = 0;
                   18812:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   18813:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     18814:                                                        if(p->recv_buffer != NULL) {
                   18815:                                                                for(int i = 0; i < dwRead; i++) {
                   18816:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   18817:                                                                }
1.1.1.25  root     18818:                                                        }
                   18819:                                                        LeaveCriticalSection(&q->csRecvData);
                   18820:                                                }
                   18821:                                        }
                   18822:                                }
                   18823:                        }
                   18824:                        Sleep(10);
                   18825:                }
                   18826:                CloseHandle(hComm);
                   18827:        }
                   18828:        return 0;
                   18829: }
                   18830: 
1.1.1.8   root     18831: // cmos
                   18832: 
                   18833: void cmos_init()
                   18834: {
                   18835:        memset(cmos, 0, sizeof(cmos));
                   18836:        cmos_addr = 0;
1.1       root     18837:        
1.1.1.8   root     18838:        // from DOSBox
                   18839:        cmos_write(0x0a, 0x26);
                   18840:        cmos_write(0x0b, 0x02);
                   18841:        cmos_write(0x0d, 0x80);
1.1       root     18842: }
                   18843: 
1.1.1.8   root     18844: void cmos_write(int addr, UINT8 val)
1.1       root     18845: {
1.1.1.8   root     18846:        cmos[addr & 0x7f] = val;
                   18847: }
                   18848: 
                   18849: #define CMOS_GET_TIME() { \
                   18850:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   18851:        if(prev_sec != cur_sec) { \
                   18852:                GetLocalTime(&time); \
                   18853:                prev_sec = cur_sec; \
                   18854:        } \
1.1       root     18855: }
1.1.1.8   root     18856: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     18857: 
1.1.1.8   root     18858: UINT8 cmos_read(int addr)
1.1       root     18859: {
1.1.1.8   root     18860:        static SYSTEMTIME time;
                   18861:        static UINT32 prev_sec = 0;
1.1       root     18862:        
1.1.1.8   root     18863:        switch(addr & 0x7f) {
                   18864:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   18865:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   18866:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   18867:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   18868:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   18869:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   18870:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   18871: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   18872:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   18873:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   18874:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   18875:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   18876:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   18877:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   18878:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   18879:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     18880:        }
1.1.1.8   root     18881:        return(cmos[addr & 0x7f]);
1.1       root     18882: }
                   18883: 
1.1.1.7   root     18884: // kbd (a20)
                   18885: 
                   18886: void kbd_init()
                   18887: {
1.1.1.8   root     18888:        kbd_data = kbd_command = 0;
1.1.1.7   root     18889:        kbd_status = 0x18;
                   18890: }
                   18891: 
                   18892: UINT8 kbd_read_data()
                   18893: {
1.1.1.8   root     18894:        kbd_status &= ~1;
1.1.1.7   root     18895:        return(kbd_data);
                   18896: }
                   18897: 
                   18898: void kbd_write_data(UINT8 val)
                   18899: {
                   18900:        switch(kbd_command) {
                   18901:        case 0xd1:
                   18902:                i386_set_a20_line((val >> 1) & 1);
                   18903:                break;
                   18904:        }
                   18905:        kbd_command = 0;
1.1.1.8   root     18906:        kbd_status &= ~8;
1.1.1.7   root     18907: }
                   18908: 
                   18909: UINT8 kbd_read_status()
                   18910: {
                   18911:        return(kbd_status);
                   18912: }
                   18913: 
                   18914: void kbd_write_command(UINT8 val)
                   18915: {
                   18916:        switch(val) {
                   18917:        case 0xd0:
                   18918:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     18919:                kbd_status |= 1;
1.1.1.7   root     18920:                break;
                   18921:        case 0xdd:
                   18922:                i386_set_a20_line(0);
                   18923:                break;
                   18924:        case 0xdf:
                   18925:                i386_set_a20_line(1);
                   18926:                break;
1.1.1.26  root     18927:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   18928:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     18929:                if(!(val & 1)) {
1.1.1.8   root     18930:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     18931:                                // reset pic
                   18932:                                pic_init();
                   18933:                                pic[0].irr = pic[1].irr = 0x00;
                   18934:                                pic[0].imr = pic[1].imr = 0xff;
                   18935:                        }
                   18936:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     18937:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   18938:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   18939:                        i386_jmp_far(selector, address);
1.1.1.7   root     18940:                }
                   18941:                i386_set_a20_line((val >> 1) & 1);
                   18942:                break;
                   18943:        }
                   18944:        kbd_command = val;
1.1.1.8   root     18945:        kbd_status |= 8;
1.1.1.7   root     18946: }
                   18947: 
1.1.1.9   root     18948: // vga
                   18949: 
                   18950: UINT8 vga_read_status()
                   18951: {
                   18952:        // 60hz
                   18953:        static const int period[3] = {16, 17, 17};
                   18954:        static int index = 0;
                   18955:        UINT32 time = timeGetTime() % period[index];
                   18956:        
                   18957:        index = (index + 1) % 3;
1.1.1.14  root     18958:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     18959: }
                   18960: 
1.1       root     18961: // i/o bus
                   18962: 
1.1.1.29  root     18963: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   18964: //#define SW1US_PATCH
                   18965: 
1.1.1.25  root     18966: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     18967: #ifdef USE_DEBUGGER
1.1.1.25  root     18968: {
1.1.1.33  root     18969:        if(now_debugging) {
                   18970:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   18971:                        if(in_break_point.table[i].status == 1) {
                   18972:                                if(addr == in_break_point.table[i].addr) {
                   18973:                                        in_break_point.hit = i + 1;
                   18974:                                        now_suspended = true;
                   18975:                                        break;
                   18976:                                }
                   18977:                        }
                   18978:                }
1.1.1.25  root     18979:        }
1.1.1.33  root     18980:        return(debugger_read_io_byte(addr));
1.1.1.25  root     18981: }
1.1.1.33  root     18982: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     18983: #endif
1.1       root     18984: {
1.1.1.33  root     18985:        UINT8 val = 0xff;
                   18986:        
1.1       root     18987:        switch(addr) {
1.1.1.29  root     18988: #ifdef SW1US_PATCH
                   18989:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   18990:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     18991:                val = sio_read(0, addr - 1);
                   18992:                break;
1.1.1.29  root     18993: #else
1.1.1.25  root     18994:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   18995:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     18996:                val = dma_read(0, addr);
                   18997:                break;
1.1.1.29  root     18998: #endif
1.1.1.25  root     18999:        case 0x20: case 0x21:
1.1.1.33  root     19000:                val = pic_read(0, addr);
                   19001:                break;
1.1.1.25  root     19002:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     19003:                val = pit_read(addr & 0x03);
                   19004:                break;
1.1.1.7   root     19005:        case 0x60:
1.1.1.33  root     19006:                val = kbd_read_data();
                   19007:                break;
1.1.1.9   root     19008:        case 0x61:
1.1.1.33  root     19009:                val = system_port;
                   19010:                break;
1.1.1.7   root     19011:        case 0x64:
1.1.1.33  root     19012:                val = kbd_read_status();
                   19013:                break;
1.1       root     19014:        case 0x71:
1.1.1.33  root     19015:                val = cmos_read(cmos_addr);
                   19016:                break;
1.1.1.25  root     19017:        case 0x81:
1.1.1.33  root     19018:                val = dma_page_read(0, 2);
                   19019:                break;
1.1.1.25  root     19020:        case 0x82:
1.1.1.33  root     19021:                val = dma_page_read(0, 3);
                   19022:                break;
1.1.1.25  root     19023:        case 0x83:
1.1.1.33  root     19024:                val = dma_page_read(0, 1);
                   19025:                break;
1.1.1.25  root     19026:        case 0x87:
1.1.1.33  root     19027:                val = dma_page_read(0, 0);
                   19028:                break;
1.1.1.25  root     19029:        case 0x89:
1.1.1.33  root     19030:                val = dma_page_read(1, 2);
                   19031:                break;
1.1.1.25  root     19032:        case 0x8a:
1.1.1.33  root     19033:                val = dma_page_read(1, 3);
                   19034:                break;
1.1.1.25  root     19035:        case 0x8b:
1.1.1.33  root     19036:                val = dma_page_read(1, 1);
                   19037:                break;
1.1.1.25  root     19038:        case 0x8f:
1.1.1.33  root     19039:                val = dma_page_read(1, 0);
                   19040:                break;
1.1       root     19041:        case 0x92:
1.1.1.33  root     19042:                val = (m_a20_mask >> 19) & 2;
                   19043:                break;
1.1.1.25  root     19044:        case 0xa0: case 0xa1:
1.1.1.33  root     19045:                val = pic_read(1, addr);
                   19046:                break;
1.1.1.25  root     19047:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   19048:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     19049:                val = dma_read(1, (addr - 0xc0) >> 1);
                   19050:                break;
1.1.1.37  root     19051:        case 0x278: case 0x279: case 0x27a:
                   19052:                val = pio_read(1, addr);
                   19053:                break;
1.1.1.29  root     19054:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     19055:                val = sio_read(3, addr);
                   19056:                break;
1.1.1.25  root     19057:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     19058:                val = sio_read(1, addr);
                   19059:                break;
1.1.1.25  root     19060:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     19061:                val = pio_read(0, addr);
                   19062:                break;
1.1.1.25  root     19063:        case 0x3ba: case 0x3da:
1.1.1.33  root     19064:                val = vga_read_status();
                   19065:                break;
1.1.1.37  root     19066:        case 0x3bc: case 0x3bd: case 0x3be:
                   19067:                val = pio_read(2, addr);
                   19068:                break;
1.1.1.29  root     19069:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     19070:                val = sio_read(2, addr);
                   19071:                break;
1.1.1.25  root     19072:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     19073:                val = sio_read(0, addr);
                   19074:                break;
1.1       root     19075:        default:
1.1.1.33  root     19076: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     19077:                break;
                   19078:        }
1.1.1.33  root     19079: #ifdef ENABLE_DEBUG_IOPORT
                   19080:        if(fp_debug_log != NULL) {
                   19081:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   19082:        }
                   19083: #endif
                   19084:        return(val);
1.1       root     19085: }
                   19086: 
                   19087: UINT16 read_io_word(offs_t addr)
                   19088: {
                   19089:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   19090: }
                   19091: 
1.1.1.33  root     19092: #ifdef USE_DEBUGGER
                   19093: UINT16 debugger_read_io_word(offs_t addr)
                   19094: {
                   19095:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   19096: }
                   19097: #endif
                   19098: 
1.1       root     19099: UINT32 read_io_dword(offs_t addr)
                   19100: {
                   19101:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   19102: }
                   19103: 
1.1.1.33  root     19104: #ifdef USE_DEBUGGER
                   19105: UINT32 debugger_read_io_dword(offs_t addr)
                   19106: {
                   19107:        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));
                   19108: }
                   19109: #endif
                   19110: 
1.1       root     19111: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     19112: #ifdef USE_DEBUGGER
                   19113: {
                   19114:        if(now_debugging) {
                   19115:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   19116:                        if(out_break_point.table[i].status == 1) {
                   19117:                                if(addr == out_break_point.table[i].addr) {
                   19118:                                        out_break_point.hit = i + 1;
                   19119:                                        now_suspended = true;
                   19120:                                        break;
                   19121:                                }
                   19122:                        }
                   19123:                }
                   19124:        }
                   19125:        debugger_write_io_byte(addr, val);
                   19126: }
                   19127: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   19128: #endif
1.1       root     19129: {
1.1.1.25  root     19130: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     19131:        if(fp_debug_log != NULL) {
1.1.1.43  root     19132: #ifdef USE_SERVICE_THREAD
                   19133:                if(addr != 0xf7)
                   19134: #endif
1.1.1.33  root     19135:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     19136:        }
                   19137: #endif
1.1       root     19138:        switch(addr) {
1.1.1.29  root     19139: #ifdef SW1US_PATCH
                   19140:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   19141:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   19142:                sio_write(0, addr - 1, val);
                   19143:                break;
                   19144: #else
1.1.1.25  root     19145:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   19146:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   19147:                dma_write(0, addr, val);
                   19148:                break;
1.1.1.29  root     19149: #endif
1.1.1.25  root     19150:        case 0x20: case 0x21:
1.1       root     19151:                pic_write(0, addr, val);
                   19152:                break;
1.1.1.25  root     19153:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     19154:                pit_write(addr & 0x03, val);
                   19155:                break;
1.1.1.7   root     19156:        case 0x60:
                   19157:                kbd_write_data(val);
                   19158:                break;
1.1.1.9   root     19159:        case 0x61:
                   19160:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   19161:                        // beep on
                   19162: //                     MessageBeep(-1);
                   19163:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   19164:                        // beep off
                   19165:                }
                   19166:                system_port = val;
                   19167:                break;
1.1       root     19168:        case 0x64:
1.1.1.7   root     19169:                kbd_write_command(val);
1.1       root     19170:                break;
                   19171:        case 0x70:
                   19172:                cmos_addr = val;
                   19173:                break;
                   19174:        case 0x71:
1.1.1.8   root     19175:                cmos_write(cmos_addr, val);
1.1       root     19176:                break;
1.1.1.25  root     19177:        case 0x81:
                   19178:                dma_page_write(0, 2, val);
                   19179:        case 0x82:
                   19180:                dma_page_write(0, 3, val);
                   19181:        case 0x83:
                   19182:                dma_page_write(0, 1, val);
                   19183:        case 0x87:
                   19184:                dma_page_write(0, 0, val);
                   19185:        case 0x89:
                   19186:                dma_page_write(1, 2, val);
                   19187:        case 0x8a:
                   19188:                dma_page_write(1, 3, val);
                   19189:        case 0x8b:
                   19190:                dma_page_write(1, 1, val);
                   19191:        case 0x8f:
                   19192:                dma_page_write(1, 0, val);
1.1       root     19193:        case 0x92:
1.1.1.7   root     19194:                i386_set_a20_line((val >> 1) & 1);
1.1       root     19195:                break;
1.1.1.25  root     19196:        case 0xa0: case 0xa1:
1.1       root     19197:                pic_write(1, addr, val);
                   19198:                break;
1.1.1.25  root     19199:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   19200:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     19201:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     19202:                break;
1.1.1.35  root     19203: #ifdef USE_SERVICE_THREAD
                   19204:        case 0xf7:
                   19205:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     19206:                if(in_service && cursor_moved) {
                   19207:                        // update cursor position before service is done
                   19208:                        pcbios_update_cursor_position();
                   19209:                        cursor_moved = false;
                   19210:                }
1.1.1.35  root     19211:                finish_service_loop();
                   19212:                break;
                   19213: #endif
1.1.1.37  root     19214:        case 0x278: case 0x279: case 0x27a:
                   19215:                pio_write(1, addr, val);
                   19216:                break;
1.1.1.29  root     19217:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   19218:                sio_write(3, addr, val);
                   19219:                break;
1.1.1.25  root     19220:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   19221:                sio_write(1, addr, val);
                   19222:                break;
                   19223:        case 0x378: case 0x379: case 0x37a:
                   19224:                pio_write(0, addr, val);
                   19225:                break;
1.1.1.37  root     19226:        case 0x3bc: case 0x3bd: case 0x3be:
                   19227:                pio_write(2, addr, val);
                   19228:                break;
1.1.1.29  root     19229:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   19230:                sio_write(2, addr, val);
                   19231:                break;
1.1.1.25  root     19232:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   19233:                sio_write(0, addr, val);
                   19234:                break;
1.1       root     19235:        default:
1.1.1.33  root     19236: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     19237:                break;
                   19238:        }
                   19239: }
                   19240: 
                   19241: void write_io_word(offs_t addr, UINT16 val)
                   19242: {
                   19243:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   19244:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   19245: }
                   19246: 
1.1.1.33  root     19247: #ifdef USE_DEBUGGER
                   19248: void debugger_write_io_word(offs_t addr, UINT16 val)
                   19249: {
                   19250:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   19251:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   19252: }
                   19253: #endif
                   19254: 
1.1       root     19255: void write_io_dword(offs_t addr, UINT32 val)
                   19256: {
                   19257:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   19258:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   19259:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   19260:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   19261: }
1.1.1.33  root     19262: 
                   19263: #ifdef USE_DEBUGGER
                   19264: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   19265: {
                   19266:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   19267:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   19268:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   19269:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   19270: }
                   19271: #endif

unix.superglobalmegacorp.com

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