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

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: 
                    855: void telnet_send(char *string)
                    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;
                   2094:                        setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
                   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: 
                   2206: long get_section_in_exec_file(FILE *fp, char *name)
                   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.44! root     3568: char *msdos_short_path(char *path);
        !          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: 
        !          3695: void msdos_cds_update(int drv, char *path)
        !          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.14  root     3881: char *msdos_remove_double_quote(char *path)
                   3882: {
                   3883:        static char tmp[MAX_PATH];
                   3884:        
                   3885:        memset(tmp, 0, sizeof(tmp));
                   3886:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
                   3887:                memcpy(tmp, path + 1, strlen(path) - 2);
                   3888:        } else {
                   3889:                strcpy(tmp, path);
                   3890:        }
                   3891:        return(tmp);
                   3892: }
                   3893: 
1.1.1.32  root     3894: char *msdos_remove_end_separator(char *path)
                   3895: {
                   3896:        static char tmp[MAX_PATH];
                   3897:        
                   3898:        strcpy(tmp, path);
                   3899:        int len = strlen(tmp);
                   3900:        if(len > 3 && tmp[len - 1] == '\\') {
                   3901:                tmp[len - 1] = '\0';
                   3902:        }
                   3903:        return(tmp);
                   3904: }
                   3905: 
1.1.1.14  root     3906: char *msdos_combine_path(char *dir, const char *file)
                   3907: {
                   3908:        static char tmp[MAX_PATH];
                   3909:        char *tmp_dir = msdos_remove_double_quote(dir);
                   3910:        
                   3911:        if(strlen(tmp_dir) == 0) {
                   3912:                strcpy(tmp, file);
                   3913:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   3914:                sprintf(tmp, "%s%s", tmp_dir, file);
                   3915:        } else {
                   3916:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   3917:        }
                   3918:        return(tmp);
                   3919: }
                   3920: 
1.1       root     3921: char *msdos_trimmed_path(char *path, int lfn)
                   3922: {
                   3923:        static char tmp[MAX_PATH];
                   3924:        
                   3925:        if(lfn) {
                   3926:                strcpy(tmp, path);
                   3927:        } else {
                   3928:                // remove space in the path
                   3929:                char *src = path, *dst = tmp;
                   3930:                
                   3931:                while(*src != '\0') {
                   3932:                        if(msdos_lead_byte_check(*src)) {
                   3933:                                *dst++ = *src++;
                   3934:                                *dst++ = *src++;
                   3935:                        } else if(*src != ' ') {
                   3936:                                *dst++ = *src++;
                   3937:                        } else {
                   3938:                                src++;  // skip space
                   3939:                        }
                   3940:                }
                   3941:                *dst = '\0';
                   3942:        }
1.1.1.14  root     3943:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   3944:                // redirect C:\COMMAND.COM to comspec_path
                   3945:                strcpy(tmp, comspec_path);
                   3946:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   3947:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   3948:                static int root_drive_protected = -1;
                   3949:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   3950:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   3951:                
                   3952:                if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
                   3953:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   3954:                        strcpy(name, name_temp);
                   3955:                        name_temp[0] = '\0';
                   3956:                        
                   3957:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   3958:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   3959:                                if(root_drive_protected == -1) {
                   3960:                                        FILE *fp = NULL;
                   3961:                                        
                   3962:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   3963:                                        root_drive_protected = 1;
                   3964:                                        try {
                   3965:                                                if((fp = fopen(temp, "w")) != NULL) {
                   3966:                                                        if(fprintf(fp, "TEST") == 4) {
                   3967:                                                                root_drive_protected = 0;
                   3968:                                                        }
                   3969:                                                }
                   3970:                                        } catch(...) {
                   3971:                                        }
                   3972:                                        if(fp != NULL) {
                   3973:                                                fclose(fp);
                   3974:                                        }
                   3975:                                        if(_access(temp, 0) == 0) {
                   3976:                                                remove(temp);
                   3977:                                        }
                   3978:                                }
                   3979:                                if(root_drive_protected == 1) {
                   3980:                                        if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
                   3981:                                           GetEnvironmentVariable("TMP",  temp, MAX_PATH) != 0) {
                   3982:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   3983:                                        }
                   3984:                                }
                   3985:                        }
                   3986:                }
                   3987:        }
1.1       root     3988:        return(tmp);
                   3989: }
                   3990: 
1.1.1.28  root     3991: char *msdos_get_multiple_short_path(char *src)
                   3992: {
1.1.1.32  root     3993:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     3994:        static char env_path[ENV_SIZE];
                   3995:        char tmp[ENV_SIZE], *token;
                   3996:        
                   3997:        memset(env_path, 0, sizeof(env_path));
                   3998:        strcpy(tmp, src);
                   3999:        token = my_strtok(tmp, ";");
                   4000:        
                   4001:        while(token != NULL) {
                   4002:                if(token[0] != '\0') {
                   4003:                        char *path = msdos_remove_double_quote(token), short_path[MAX_PATH];
1.1.1.32  root     4004:                        if(path != NULL && strlen(path) != 0) {
                   4005:                                if(env_path[0] != '\0') {
                   4006:                                        strcat(env_path, ";");
                   4007:                                }
1.1.1.28  root     4008:                                if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     4009:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     4010:                                } else {
                   4011:                                        my_strupr(short_path);
1.1.1.32  root     4012:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     4013:                                }
                   4014:                        }
                   4015:                }
                   4016:                token = my_strtok(NULL, ";");
                   4017:        }
                   4018:        return(env_path);
                   4019: }
                   4020: 
1.1       root     4021: bool match(char *text, char *pattern)
                   4022: {
1.1.1.24  root     4023:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     4024:        switch(*pattern) {
1.1       root     4025:        case '\0':
                   4026:                return !*text;
                   4027:        case '*':
1.1.1.14  root     4028:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     4029:        case '?':
                   4030:                return *text && match(text + 1, pattern + 1);
                   4031:        default:
                   4032:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   4033:        }
                   4034: }
                   4035: 
                   4036: bool msdos_match_volume_label(char *path, char *volume)
                   4037: {
                   4038:        char *p;
                   4039:        
1.1.1.14  root     4040:        if(!*volume) {
                   4041:                return false;
                   4042:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     4043:                return msdos_match_volume_label(p + 1, volume);
                   4044:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   4045:                return msdos_match_volume_label(p + 1, volume);
                   4046:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     4047:                char tmp[MAX_PATH];
                   4048:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   4049:                return match(volume, tmp);
1.1       root     4050:        } else {
                   4051:                return match(volume, path);
                   4052:        }
                   4053: }
                   4054: 
                   4055: char *msdos_fcb_path(fcb_t *fcb)
                   4056: {
                   4057:        static char tmp[MAX_PATH];
                   4058:        char name[9], ext[4];
                   4059:        
                   4060:        memset(name, 0, sizeof(name));
                   4061:        memcpy(name, fcb->file_name, 8);
                   4062:        strcpy(name, msdos_trimmed_path(name, 0));
                   4063:        
                   4064:        memset(ext, 0, sizeof(ext));
                   4065:        memcpy(ext, fcb->file_name + 8, 3);
                   4066:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4067:        
                   4068:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4069:                strcpy(name, "*");
                   4070:        }
                   4071:        if(ext[0] == '\0') {
                   4072:                strcpy(tmp, name);
                   4073:        } else {
                   4074:                if(strcmp(ext, "???") == 0) {
                   4075:                        strcpy(ext, "*");
                   4076:                }
                   4077:                sprintf(tmp, "%s.%s", name, ext);
                   4078:        }
                   4079:        return(tmp);
                   4080: }
                   4081: 
                   4082: void msdos_set_fcb_path(fcb_t *fcb, char *path)
                   4083: {
                   4084:        char *ext = my_strchr(path, '.');
                   4085:        
                   4086:        memset(fcb->file_name, 0x20, 8 + 3);
                   4087:        if(ext != NULL && path[0] != '.') {
                   4088:                *ext = '\0';
                   4089:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4090:        }
                   4091:        memcpy(fcb->file_name, path, strlen(path));
                   4092: }
                   4093: 
                   4094: char *msdos_short_path(char *path)
                   4095: {
                   4096:        static char tmp[MAX_PATH];
                   4097:        
1.1.1.24  root     4098:        if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
                   4099:                strcpy(tmp, path);
                   4100:        }
1.1       root     4101:        my_strupr(tmp);
                   4102:        return(tmp);
                   4103: }
                   4104: 
1.1.1.13  root     4105: char *msdos_short_name(WIN32_FIND_DATA *fd)
                   4106: {
                   4107:        static char tmp[MAX_PATH];
                   4108: 
1.1.1.14  root     4109:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4110:                strcpy(tmp, fd->cAlternateFileName);
                   4111:        } else {
                   4112:                strcpy(tmp, fd->cFileName);
                   4113:        }
                   4114:        my_strupr(tmp);
                   4115:        return(tmp);
                   4116: }
                   4117: 
1.1       root     4118: char *msdos_short_full_path(char *path)
                   4119: {
                   4120:        static char tmp[MAX_PATH];
                   4121:        char full[MAX_PATH], *name;
                   4122:        
1.1.1.14  root     4123:        // Full works with non-existent files, but Short does not
1.1       root     4124:        GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14  root     4125:        *tmp = '\0';
                   4126:        if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
                   4127:                name[-1] = '\0';
                   4128:                DWORD len = GetShortPathName(full, tmp, MAX_PATH);
                   4129:                if(len == 0) {
                   4130:                        strcpy(tmp, full);
                   4131:                } else {
                   4132:                        tmp[len++] = '\\';
                   4133:                        strcpy(tmp + len, name);
                   4134:                }
                   4135:        }
1.1       root     4136:        my_strupr(tmp);
                   4137:        return(tmp);
                   4138: }
                   4139: 
                   4140: char *msdos_short_full_dir(char *path)
                   4141: {
                   4142:        static char tmp[MAX_PATH];
                   4143:        char full[MAX_PATH], *name;
                   4144:        
                   4145:        GetFullPathName(path, MAX_PATH, full, &name);
                   4146:        name[-1] = '\0';
1.1.1.24  root     4147:        if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
                   4148:                strcpy(tmp, full);
                   4149:        }
1.1       root     4150:        my_strupr(tmp);
                   4151:        return(tmp);
                   4152: }
                   4153: 
                   4154: char *msdos_local_file_path(char *path, int lfn)
                   4155: {
                   4156:        char *trimmed = msdos_trimmed_path(path, lfn);
1.1.1.14  root     4157: #if 0
                   4158:        // I have forgotten the reason of this routine... :-(
1.1       root     4159:        if(_access(trimmed, 0) != 0) {
                   4160:                process_t *process = msdos_process_info_get(current_psp);
                   4161:                static char tmp[MAX_PATH];
                   4162:                
                   4163:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4164:                if(_access(tmp, 0) == 0) {
                   4165:                        return(tmp);
                   4166:                }
                   4167:        }
1.1.1.14  root     4168: #endif
1.1       root     4169:        return(trimmed);
                   4170: }
                   4171: 
1.1.1.29  root     4172: bool msdos_is_device_path(char *path)
1.1.1.11  root     4173: {
                   4174:        char full[MAX_PATH], *name;
                   4175:        
1.1.1.24  root     4176:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4177:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4178:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4179:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4180:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4181:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4182:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4183:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4184:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4185:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4186:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4187:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4188:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4189:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4190:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4191:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4192:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4193:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4194:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4195:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4196:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4197:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4198:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4199:                        return(true);
                   4200:                } else if(name != NULL) {
                   4201:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4202:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4203:                           _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43  root     4204: //                        _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30  root     4205:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4206:                                return(true);
                   4207:                        }
                   4208:                }
1.1.1.24  root     4209:        }
                   4210:        return(false);
1.1.1.11  root     4211: }
                   4212: 
1.1.1.29  root     4213: bool msdos_is_con_path(char *path)
1.1.1.8   root     4214: {
1.1.1.14  root     4215:        char full[MAX_PATH], *name;
1.1.1.8   root     4216:        
1.1.1.24  root     4217:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4218:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4219:        }
                   4220:        return(false);
                   4221: }
                   4222: 
1.1.1.29  root     4223: int msdos_is_comm_path(char *path)
1.1.1.24  root     4224: {
                   4225:        char full[MAX_PATH], *name;
                   4226:        
                   4227:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4228:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4229:                        return(1);
                   4230:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4231:                        return(2);
                   4232:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4233:                        return(3);
                   4234:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4235:                        return(4);
1.1.1.24  root     4236:                }
                   4237:        }
1.1.1.29  root     4238:        return(0);
                   4239: }
                   4240: 
1.1.1.37  root     4241: void msdos_set_comm_params(int sio_port, char *path)
                   4242: {
                   4243:        // COM1:{110,150,300,600,1200,2400,4800,9600},{N,O,E,M,S},{8,7,6,5},{1,1.5,2}
                   4244:        char *p = NULL;
                   4245:        
                   4246:        if((p = strstr(path, ":")) != NULL) {
                   4247:                UINT8 selector = sio_read(sio_port - 1, 3);
                   4248:                
                   4249:                // baud rate
                   4250:                int baud = max(110, min(9600, atoi(p + 1)));
                   4251:                UINT16 divisor = 115200 / baud;
                   4252:                
                   4253:                if((p = strstr(p + 1, ",")) != NULL) {
                   4254:                        // parity
                   4255:                        if(p[1] == 'N' || p[1] == 'n') {
                   4256:                                selector = (selector & ~0x38) | 0x00;
                   4257:                        } else if(p[1] == 'O' || p[1] == 'o') {
                   4258:                                selector = (selector & ~0x38) | 0x08;
                   4259:                        } else if(p[1] == 'E' || p[1] == 'e') {
                   4260:                                selector = (selector & ~0x38) | 0x18;
                   4261:                        } else if(p[1] == 'M' || p[1] == 'm') {
                   4262:                                selector = (selector & ~0x38) | 0x28;
                   4263:                        } else if(p[1] == 'S' || p[1] == 's') {
                   4264:                                selector = (selector & ~0x38) | 0x38;
                   4265:                        }
                   4266:                        if((p = strstr(p + 1, ",")) != NULL) {
                   4267:                                // word length
                   4268:                                if(p[1] == '8') {
                   4269:                                        selector = (selector & ~0x03) | 0x03;
                   4270:                                } else if(p[1] == '7') {
                   4271:                                        selector = (selector & ~0x03) | 0x02;
                   4272:                                } else if(p[1] == '6') {
                   4273:                                        selector = (selector & ~0x03) | 0x01;
                   4274:                                } else if(p[1] == '5') {
                   4275:                                        selector = (selector & ~0x03) | 0x00;
                   4276:                                }
                   4277:                                if((p = strstr(p + 1, ",")) != NULL) {
                   4278:                                        // stop bits
                   4279:                                        float bits = atof(p + 1);
                   4280:                                        if(bits > 1.0F) {
                   4281:                                                selector |= 0x04;
                   4282:                                        } else {
                   4283:                                                selector &= ~0x04;
                   4284:                                        }
                   4285:                                }
                   4286:                        }
                   4287:                }
                   4288:                sio_write(sio_port - 1, 3, selector | 0x80);
                   4289:                sio_write(sio_port - 1, 0, divisor & 0xff);
                   4290:                sio_write(sio_port - 1, 1, divisor >> 8);
                   4291:                sio_write(sio_port - 1, 3, selector);
                   4292:        }
                   4293: }
                   4294: 
1.1.1.30  root     4295: int msdos_is_prn_path(char *path)
                   4296: {
                   4297:        char full[MAX_PATH], *name;
                   4298:        
                   4299:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4300:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4301:                        return(1);
                   4302:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4303:                        return(1);
                   4304:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4305:                        return(2);
                   4306:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4307:                        return(3);
                   4308:                }
                   4309:        }
                   4310:        return(0);
                   4311: }
                   4312: 
1.1.1.44! root     4313: bool msdos_is_valid_drive(int drv)
        !          4314: {
        !          4315:        return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
        !          4316: }
        !          4317: 
        !          4318: bool msdos_is_removable_drive(int drv)
        !          4319: {
        !          4320:        char volume[] = "A:\\";
        !          4321:        
        !          4322:        volume[0] = 'A' + drv;
        !          4323:        
        !          4324:        return(GetDriveType(volume) == DRIVE_REMOVABLE);
        !          4325: }
        !          4326: 
        !          4327: bool msdos_is_cdrom_drive(int drv)
        !          4328: {
        !          4329:        char volume[] = "A:\\";
        !          4330:        
        !          4331:        volume[0] = 'A' + drv;
        !          4332:        
        !          4333:        return(GetDriveType(volume) == DRIVE_CDROM);
        !          4334: }
        !          4335: 
        !          4336: bool msdos_is_remote_drive(int drv)
        !          4337: {
        !          4338:        char volume[] = "A:\\";
        !          4339:        
        !          4340:        volume[0] = 'A' + drv;
        !          4341:        
        !          4342:        return(GetDriveType(volume) == DRIVE_REMOTE);
        !          4343: }
        !          4344: 
        !          4345: bool msdos_is_subst_drive(int drv)
        !          4346: {
        !          4347:        char device[] = "A:", path[MAX_PATH];
        !          4348:        
        !          4349:        device[0] = 'A' + drv;
        !          4350:        
        !          4351:        if(QueryDosDevice(device, path, MAX_PATH)) {
        !          4352:                if(strncmp(path, "\\??\\", 4) == 0) {
        !          4353:                        return(true);
        !          4354:                }
        !          4355:        }
        !          4356:        return(false);
        !          4357: }
        !          4358: 
1.1.1.24  root     4359: bool msdos_is_existing_file(char *path)
                   4360: {
                   4361:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   4362:        WIN32_FIND_DATA FindData;
                   4363:        HANDLE hFind;
                   4364:        
                   4365:        if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
                   4366:                FindClose(hFind);
                   4367:                return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
                   4368:        }
                   4369:        return(false);
1.1.1.8   root     4370: }
                   4371: 
1.1.1.9   root     4372: char *msdos_search_command_com(char *command_path, char *env_path)
                   4373: {
                   4374:        static char tmp[MAX_PATH];
1.1.1.28  root     4375:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4376:        
1.1.1.28  root     4377:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.9   root     4378:        if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
                   4379:                sprintf(file_name, "COMMAND.COM");
                   4380:                if(_access(tmp, 0) == 0) {
                   4381:                        return(tmp);
                   4382:                }
                   4383:        }
1.1.1.28  root     4384:        
                   4385:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.9   root     4386:        if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
                   4387:                sprintf(file_name, "COMMAND.COM");
                   4388:                if(_access(tmp, 0) == 0) {
                   4389:                        return(tmp);
                   4390:                }
                   4391:        }
1.1.1.28  root     4392:        
                   4393:        // check if COMMAND.COM is in the current directory
1.1.1.9   root     4394:        if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
                   4395:                if(_access(tmp, 0) == 0) {
                   4396:                        return(tmp);
                   4397:                }
                   4398:        }
1.1.1.28  root     4399:        
                   4400:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4401:        strcpy(path, env_path);
                   4402:        char *token = my_strtok(path, ";");
1.1.1.9   root     4403:        while(token != NULL) {
1.1.1.14  root     4404:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4405:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4406:                        if(_access(tmp, 0) == 0) {
                   4407:                                return(tmp);
                   4408:                        }
                   4409:                }
                   4410:                token = my_strtok(NULL, ";");
                   4411:        }
                   4412:        return(NULL);
                   4413: }
                   4414: 
1.1.1.14  root     4415: int msdos_drive_number(const char *path)
1.1       root     4416: {
                   4417:        char tmp[MAX_PATH], *name;
                   4418:        
                   4419:        GetFullPathName(path, MAX_PATH, tmp, &name);
                   4420:        if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4421:                return(tmp[0] - 'a');
                   4422:        } else {
                   4423:                return(tmp[0] - 'A');
                   4424:        }
                   4425: }
                   4426: 
                   4427: char *msdos_volume_label(char *path)
                   4428: {
                   4429:        static char tmp[MAX_PATH];
                   4430:        char volume[] = "A:\\";
                   4431:        
                   4432:        if(path[1] == ':') {
                   4433:                volume[0] = path[0];
                   4434:        } else {
                   4435:                volume[0] = 'A' + _getdrive() - 1;
                   4436:        }
                   4437:        if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
                   4438:                memset(tmp, 0, sizeof(tmp));
                   4439:        }
                   4440:        return(tmp);
                   4441: }
                   4442: 
                   4443: char *msdos_short_volume_label(char *label)
                   4444: {
                   4445:        static char tmp[(8 + 1 + 3) + 1];
                   4446:        char *src = label;
                   4447:        int remain = strlen(label);
                   4448:        char *dst_n = tmp;
                   4449:        char *dst_e = tmp + 9;
                   4450:        
                   4451:        strcpy(tmp, "        .   ");
                   4452:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4453:                if(msdos_lead_byte_check(*src)) {
                   4454:                        if(++i == 8) {
                   4455:                                break;
                   4456:                        }
                   4457:                        *dst_n++ = *src++;
                   4458:                        remain--;
                   4459:                }
                   4460:                *dst_n++ = *src++;
                   4461:                remain--;
                   4462:        }
                   4463:        if(remain > 0) {
                   4464:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4465:                        if(msdos_lead_byte_check(*src)) {
                   4466:                                if(++i == 3) {
                   4467:                                        break;
                   4468:                                }
                   4469:                                *dst_e++ = *src++;
                   4470:                                remain--;
                   4471:                        }
                   4472:                        *dst_e++ = *src++;
                   4473:                        remain--;
                   4474:                }
                   4475:                *dst_e = '\0';
                   4476:        } else {
                   4477:                *dst_n = '\0';
                   4478:        }
                   4479:        my_strupr(tmp);
                   4480:        return(tmp);
                   4481: }
                   4482: 
1.1.1.13  root     4483: errno_t msdos_maperr(unsigned long oserrno)
                   4484: {
                   4485:        _doserrno = oserrno;
1.1.1.14  root     4486:        switch(oserrno) {
1.1.1.13  root     4487:        case ERROR_FILE_NOT_FOUND:         // 2
                   4488:        case ERROR_PATH_NOT_FOUND:         // 3
                   4489:        case ERROR_INVALID_DRIVE:          // 15
                   4490:        case ERROR_NO_MORE_FILES:          // 18
                   4491:        case ERROR_BAD_NETPATH:            // 53
                   4492:        case ERROR_BAD_NET_NAME:           // 67
                   4493:        case ERROR_BAD_PATHNAME:           // 161
                   4494:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4495:                return ENOENT;
                   4496:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4497:                return EMFILE;
                   4498:        case ERROR_ACCESS_DENIED:          // 5
                   4499:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4500:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4501:        case ERROR_CANNOT_MAKE:            // 82
                   4502:        case ERROR_FAIL_I24:               // 83
                   4503:        case ERROR_DRIVE_LOCKED:           // 108
                   4504:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4505:        case ERROR_NOT_LOCKED:             // 158
                   4506:        case ERROR_LOCK_FAILED:            // 167
                   4507:                return EACCES;
                   4508:        case ERROR_INVALID_HANDLE:         // 6
                   4509:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4510:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4511:                return EBADF;
                   4512:        case ERROR_ARENA_TRASHED:          // 7
                   4513:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4514:        case ERROR_INVALID_BLOCK:          // 9
                   4515:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4516:                return ENOMEM;
                   4517:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4518:                return E2BIG;
                   4519:        case ERROR_BAD_FORMAT:             // 11
                   4520:                return ENOEXEC;
                   4521:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4522:                return EXDEV;
                   4523:        case ERROR_FILE_EXISTS:            // 80
                   4524:        case ERROR_ALREADY_EXISTS:         // 183
                   4525:                return EEXIST;
                   4526:        case ERROR_NO_PROC_SLOTS:          // 89
                   4527:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4528:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4529:                return EAGAIN;
                   4530:        case ERROR_BROKEN_PIPE:            // 109
                   4531:                return EPIPE;
                   4532:        case ERROR_DISK_FULL:              // 112
                   4533:                return ENOSPC;
                   4534:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4535:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4536:                return ECHILD;
                   4537:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4538:                return ENOTEMPTY;
                   4539:        }
1.1.1.14  root     4540:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4541:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4542:                return EACCES;
                   4543:        }
1.1.1.14  root     4544:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4545:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4546:                return ENOEXEC;
                   4547:        }
                   4548:        return EINVAL;
                   4549: }
                   4550: 
                   4551: int msdos_open(const char *filename, int oflag)
                   4552: {
1.1.1.14  root     4553:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.13  root     4554:                return _open(filename, oflag);
                   4555:        }
1.1.1.14  root     4556:        
                   4557:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4558:        DWORD disposition;
1.1.1.14  root     4559:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4560:        default:
1.1.1.13  root     4561:        case _O_EXCL:
                   4562:                disposition = OPEN_EXISTING;
                   4563:                break;
                   4564:        case _O_CREAT:
                   4565:                disposition = OPEN_ALWAYS;
                   4566:                break;
                   4567:        case _O_CREAT | _O_EXCL:
                   4568:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4569:                disposition = CREATE_NEW;
                   4570:                break;
                   4571:        case _O_TRUNC:
                   4572:        case _O_TRUNC | _O_EXCL:
                   4573:                disposition = TRUNCATE_EXISTING;
                   4574:                break;
                   4575:        case _O_CREAT | _O_TRUNC:
                   4576:                disposition = CREATE_ALWAYS;
                   4577:                break;
                   4578:        }
1.1.1.14  root     4579:        
1.1.1.13  root     4580:        HANDLE h = CreateFile(filename, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
                   4581:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4582:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4583:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4584:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4585:                // Retry without FILE_WRITE_ATTRIBUTES.
                   4586:                h = CreateFile(filename, GENERIC_READ,
                   4587:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4588:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4589:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4590:                        errno = msdos_maperr(GetLastError());
                   4591:                        return -1;
                   4592:                }
                   4593:        }
1.1.1.14  root     4594:        
1.1.1.13  root     4595:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     4596:        if(fd == -1) {
1.1.1.13  root     4597:                CloseHandle(h);
                   4598:        }
                   4599:        return fd;
                   4600: }
                   4601: 
1.1.1.37  root     4602: 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     4603: {
                   4604:        static int id = 0;
                   4605:        char full[MAX_PATH], *name;
                   4606:        
                   4607:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4608:                strcpy(file_handler[fd].path, full);
                   4609:        } else {
                   4610:                strcpy(file_handler[fd].path, path);
                   4611:        }
1.1.1.14  root     4612:        // isatty makes no distinction between CON & NUL
                   4613:        // GetFileSize fails on CON, succeeds on NUL
                   4614:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
                   4615:                info = 0x8084;
                   4616:                atty = 0;
                   4617:        } else if(!atty && info == 0x80d3) {
                   4618:                info = msdos_drive_number(".");
                   4619:        }
1.1       root     4620:        file_handler[fd].valid = 1;
                   4621:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37  root     4622:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     4623:        file_handler[fd].mode = mode;
                   4624:        file_handler[fd].info = info;
                   4625:        file_handler[fd].psp = psp_seg;
1.1.1.37  root     4626:        file_handler[fd].sio_port = sio_port;
                   4627:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     4628:        
                   4629:        // init system file table
                   4630:        if(fd < 20) {
                   4631:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   4632:                
                   4633:                memset(sft, 0, 0x3b);
                   4634:                
                   4635:                *(UINT16 *)(sft + 0x00) = 1;
                   4636:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
                   4637:                *(UINT8  *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
                   4638:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   4639:                
                   4640:                if(!(file_handler[fd].info & 0x80)) {
                   4641:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   4642:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   4643:                        
                   4644:                        FILETIME time, local;
                   4645:                        HANDLE hHandle;
                   4646:                        WORD dos_date = 0, dos_time = 0;
                   4647:                        DWORD file_size = 0;
                   4648:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   4649:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   4650:                                        FileTimeToLocalFileTime(&time, &local);
                   4651:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   4652:                                }
                   4653:                                file_size = GetFileSize(hHandle, NULL);
                   4654:                        }
                   4655:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   4656:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   4657:                        *(UINT32 *)(sft + 0x11) = file_size;
                   4658:                }
                   4659:                
                   4660:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   4661:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   4662:                my_strupr(fname);
                   4663:                my_strupr(ext);
                   4664:                memset(sft + 0x20, 0x20, 11);
                   4665:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   4666:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   4667:                
                   4668:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   4669:        }
1.1       root     4670: }
                   4671: 
1.1.1.37  root     4672: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg)
                   4673: {
                   4674:         msdos_file_handler_open(fd, path, atty, mode, info, psp_seg, 0, 0);
                   4675: }
                   4676: 
1.1       root     4677: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   4678: {
                   4679:        strcpy(file_handler[dst].path, file_handler[src].path);
                   4680:        file_handler[dst].valid = 1;
                   4681:        file_handler[dst].id = file_handler[src].id;
                   4682:        file_handler[dst].atty = file_handler[src].atty;
                   4683:        file_handler[dst].mode = file_handler[src].mode;
                   4684:        file_handler[dst].info = file_handler[src].info;
                   4685:        file_handler[dst].psp = psp_seg;
1.1.1.37  root     4686:        file_handler[dst].sio_port = file_handler[src].sio_port;
                   4687:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     4688: }
                   4689: 
1.1.1.20  root     4690: void msdos_file_handler_close(int fd)
1.1       root     4691: {
                   4692:        file_handler[fd].valid = 0;
1.1.1.21  root     4693:        
                   4694:        if(fd < 20) {
                   4695:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   4696:        }
1.1       root     4697: }
                   4698: 
1.1.1.14  root     4699: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     4700: {
1.1.1.14  root     4701:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   4702:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   4703:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     4704: }
                   4705: 
                   4706: // find file
                   4707: 
                   4708: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   4709: {
                   4710:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4711:                return(0);      // search directory only !!!
                   4712:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   4713:                return(0);
                   4714:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   4715:                return(0);
                   4716:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4717:                return(0);
                   4718:        } else if((attribute & required_mask) != required_mask) {
                   4719:                return(0);
                   4720:        } else {
                   4721:                return(1);
                   4722:        }
                   4723: }
                   4724: 
1.1.1.13  root     4725: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
                   4726: {
1.1.1.14  root     4727:        if(fd->cAlternateFileName[0]) {
1.1.1.42  root     4728:                return(1);
1.1.1.13  root     4729:        }
                   4730:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     4731:        if(len > 12) {
1.1.1.42  root     4732:                return(0);
1.1.1.13  root     4733:        }
                   4734:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     4735:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42  root     4736:                return(0);
1.1.1.13  root     4737:        }
1.1.1.42  root     4738:        return(1);
1.1.1.13  root     4739: }
                   4740: 
1.1       root     4741: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
                   4742: {
                   4743:        FILETIME local;
                   4744:        
                   4745:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   4746:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   4747:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   4748:        
                   4749:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   4750:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   4751:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   4752:        
                   4753:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   4754:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   4755:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   4756: }
                   4757: 
                   4758: // i/o
                   4759: 
                   4760: void msdos_stdio_reopen()
                   4761: {
                   4762:        if(!file_handler[0].valid) {
                   4763:                _dup2(DUP_STDIN, 0);
                   4764:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   4765:        }
                   4766:        if(!file_handler[1].valid) {
                   4767:                _dup2(DUP_STDOUT, 1);
                   4768:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   4769:        }
                   4770:        if(!file_handler[2].valid) {
                   4771:                _dup2(DUP_STDERR, 2);
                   4772:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   4773:        }
1.1.1.21  root     4774:        if(!file_handler[3].valid) {
                   4775:                _dup2(DUP_STDAUX, 3);
                   4776:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   4777:        }
                   4778:        if(!file_handler[4].valid) {
                   4779:                _dup2(DUP_STDPRN, 4);
1.1.1.37  root     4780:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
1.1.1.21  root     4781:        }
                   4782:        for(int i = 0; i < 5; i++) {
                   4783:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   4784:                        msdos_psp_set_file_table(i, i, current_psp);
                   4785:                }
                   4786:        }
1.1       root     4787: }
                   4788: 
1.1.1.37  root     4789: int msdos_read(int fd, void *buffer, unsigned int count)
                   4790: {
                   4791:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   4792:                // read from serial port
                   4793:                int read = 0;
                   4794:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   4795:                        UINT8 *buf = (UINT8 *)buffer;
                   4796:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   4797:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     4798:                        DWORD timeout = timeGetTime() + 1000;
                   4799:                        while(read < count) {
                   4800:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
                   4801:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
                   4802:                                        timeout = timeGetTime() + 1000;
                   4803:                                } else {
                   4804:                                        if(timeGetTime() > timeout) {
                   4805:                                                break;
                   4806:                                        }
                   4807:                                        Sleep(10);
1.1.1.37  root     4808:                                }
                   4809:                        }
                   4810:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   4811:                }
                   4812:                return(read);
                   4813:        }
                   4814:        return(_read(fd, buffer, count));
                   4815: }
                   4816: 
1.1       root     4817: int msdos_kbhit()
                   4818: {
                   4819:        msdos_stdio_reopen();
                   4820:        
1.1.1.20  root     4821:        process_t *process = msdos_process_info_get(current_psp);
                   4822:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4823:        
                   4824:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4825:                // stdin is redirected to file
1.1.1.20  root     4826:                return(eof(fd) == 0);
1.1       root     4827:        }
                   4828:        
                   4829:        // check keyboard status
1.1.1.35  root     4830:        if(key_recv != 0) {
1.1       root     4831:                return(1);
                   4832:        }
1.1.1.35  root     4833:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4834: #ifdef USE_SERVICE_THREAD
                   4835:                EnterCriticalSection(&key_buf_crit_sect);
                   4836: #endif
                   4837:                bool empty = key_buf_char->empty();
                   4838: #ifdef USE_SERVICE_THREAD
                   4839:                LeaveCriticalSection(&key_buf_crit_sect);
                   4840: #endif
                   4841:                if(!empty) return(1);
                   4842:        }
                   4843:        return(_kbhit());
1.1       root     4844: }
                   4845: 
                   4846: int msdos_getch_ex(int echo)
                   4847: {
                   4848:        static char prev = 0;
                   4849:        
                   4850:        msdos_stdio_reopen();
                   4851:        
1.1.1.20  root     4852:        process_t *process = msdos_process_info_get(current_psp);
                   4853:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4854:        
                   4855:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4856:                // stdin is redirected to file
                   4857: retry:
                   4858:                char data;
1.1.1.37  root     4859:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     4860:                        char tmp = data;
                   4861:                        if(data == 0x0a) {
                   4862:                                if(prev == 0x0d) {
                   4863:                                        goto retry; // CRLF -> skip LF
                   4864:                                } else {
                   4865:                                        data = 0x0d; // LF only -> CR
                   4866:                                }
                   4867:                        }
                   4868:                        prev = tmp;
                   4869:                        return(data);
                   4870:                }
                   4871:                return(EOF);
                   4872:        }
                   4873:        
                   4874:        // input from console
1.1.1.5   root     4875:        int key_char, key_scan;
1.1.1.33  root     4876:        if(key_recv != 0) {
1.1.1.5   root     4877:                key_char = (key_code >> 0) & 0xff;
                   4878:                key_scan = (key_code >> 8) & 0xff;
                   4879:                key_code >>= 16;
1.1.1.33  root     4880:                key_recv >>= 16;
1.1.1.5   root     4881:        } else {
1.1.1.35  root     4882:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_halted) {
                   4883:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4884: #ifdef USE_SERVICE_THREAD
                   4885:                                EnterCriticalSection(&key_buf_crit_sect);
                   4886: #endif
                   4887:                                bool empty = key_buf_char->empty();
                   4888: #ifdef USE_SERVICE_THREAD
                   4889:                                LeaveCriticalSection(&key_buf_crit_sect);
                   4890: #endif
                   4891:                                if(!empty) break;
                   4892:                        }
1.1.1.23  root     4893:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   4894:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   4895:                                if(_kbhit()) {
1.1.1.32  root     4896:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     4897: #ifdef USE_SERVICE_THREAD
                   4898:                                                EnterCriticalSection(&key_buf_crit_sect);
                   4899: #endif
1.1.1.32  root     4900:                                                key_buf_char->write(_getch());
1.1.1.35  root     4901:                                                key_buf_scan->write(0x00);
                   4902: #ifdef USE_SERVICE_THREAD
                   4903:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   4904: #endif
1.1.1.32  root     4905:                                        }
1.1.1.23  root     4906:                                } else {
                   4907:                                        Sleep(10);
                   4908:                                }
                   4909:                        } else {
                   4910:                                if(!update_key_buffer()) {
                   4911:                                        Sleep(10);
                   4912:                                }
1.1.1.14  root     4913:                        }
                   4914:                }
                   4915:                if(m_halted) {
1.1.1.33  root     4916:                        // insert CR to terminate input loops
1.1.1.14  root     4917:                        key_char = 0x0d;
                   4918:                        key_scan = 0;
1.1.1.32  root     4919:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     4920: #ifdef USE_SERVICE_THREAD
                   4921:                        EnterCriticalSection(&key_buf_crit_sect);
                   4922: #endif
1.1.1.14  root     4923:                        key_char = key_buf_char->read();
                   4924:                        key_scan = key_buf_scan->read();
1.1.1.41  root     4925:                        // write to bottom of key buffer
                   4926:                        mem[0x43c] = (UINT8)key_char;
                   4927:                        mem[0x43d] = (UINT8)key_scan;
1.1.1.35  root     4928: #ifdef USE_SERVICE_THREAD
                   4929:                        LeaveCriticalSection(&key_buf_crit_sect);
                   4930: #endif
1.1.1.5   root     4931:                }
1.1       root     4932:        }
                   4933:        if(echo && key_char) {
                   4934:                msdos_putch(key_char);
                   4935:        }
                   4936:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   4937: }
                   4938: 
                   4939: inline int msdos_getch()
                   4940: {
                   4941:        return(msdos_getch_ex(0));
                   4942: }
                   4943: 
                   4944: inline int msdos_getche()
                   4945: {
                   4946:        return(msdos_getch_ex(1));
                   4947: }
                   4948: 
                   4949: int msdos_write(int fd, const void *buffer, unsigned int count)
                   4950: {
1.1.1.37  root     4951:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   4952:                // write to serial port
1.1.1.38  root     4953:                int written = 0;
1.1.1.37  root     4954:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   4955:                        UINT8 *buf = (UINT8 *)buffer;
                   4956:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   4957:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     4958:                        DWORD timeout = timeGetTime() + 1000;
                   4959:                        while(written < count) {
                   4960:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
                   4961:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
                   4962:                                        timeout = timeGetTime() + 1000;
                   4963:                                } else {
                   4964:                                        if(timeGetTime() > timeout) {
                   4965:                                                break;
                   4966:                                        }
                   4967:                                        Sleep(10);
                   4968:                                }
1.1.1.37  root     4969:                        }
                   4970:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   4971:                }
1.1.1.38  root     4972:                return(written);
1.1.1.37  root     4973:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   4974:                // write to printer port
                   4975:                UINT8 *buf = (UINT8 *)buffer;
                   4976:                for(unsigned int i = 0; i < count; i++) {
                   4977: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   4978:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   4979:                }
                   4980:                return(count);
                   4981:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     4982:                // CR+LF -> LF
1.1.1.37  root     4983:                static int is_cr = 0;
1.1       root     4984:                UINT8 *buf = (UINT8 *)buffer;
                   4985:                for(unsigned int i = 0; i < count; i++) {
                   4986:                        UINT8 data = buf[i];
                   4987:                        if(is_cr) {
                   4988:                                if(data != 0x0a) {
                   4989:                                        UINT8 tmp = 0x0d;
                   4990:                                        _write(1, &tmp, 1);
                   4991:                                }
                   4992:                                _write(1, &data, 1);
                   4993:                                is_cr = 0;
                   4994:                        } else if(data == 0x0d) {
                   4995:                                is_cr = 1;
                   4996:                        } else {
                   4997:                                _write(1, &data, 1);
                   4998:                        }
                   4999:                }
                   5000:                return(count);
                   5001:        }
1.1.1.14  root     5002:        vram_flush();
1.1       root     5003:        return(_write(fd, buffer, count));
                   5004: }
                   5005: 
                   5006: void msdos_putch(UINT8 data)
1.1.1.35  root     5007: #ifdef USE_SERVICE_THREAD
                   5008: {
                   5009:        EnterCriticalSection(&putch_crit_sect);
                   5010:        msdos_putch_tmp(data);
                   5011:        LeaveCriticalSection(&putch_crit_sect);
                   5012: }
                   5013: void msdos_putch_tmp(UINT8 data)
                   5014: #endif
1.1       root     5015: {
1.1.1.34  root     5016:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5017:        SMALL_RECT rect;
                   5018:        COORD co;
1.1       root     5019:        static int p = 0;
                   5020:        static int is_kanji = 0;
                   5021:        static int is_esc = 0;
                   5022:        static int stored_x;
                   5023:        static int stored_y;
                   5024:        static WORD stored_a;
1.1.1.20  root     5025:        static char tmp[64], out[64];
1.1       root     5026:        
                   5027:        msdos_stdio_reopen();
                   5028:        
1.1.1.20  root     5029:        process_t *process = msdos_process_info_get(current_psp);
                   5030:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5031:        
                   5032:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5033:                // stdout is redirected to file
1.1.1.20  root     5034:                msdos_write(fd, &data, 1);
1.1       root     5035:                return;
                   5036:        }
1.1.1.23  root     5037:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     5038:        
                   5039:        // output to console
                   5040:        tmp[p++] = data;
                   5041:        
1.1.1.14  root     5042:        vram_flush();
                   5043:        
1.1       root     5044:        if(is_kanji) {
                   5045:                // kanji character
                   5046:                is_kanji = 0;
                   5047:        } else if(is_esc) {
                   5048:                // escape sequense
                   5049:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   5050:                        p = is_esc = 0;
                   5051:                } else if(tmp[1] == '=' && p == 4) {
                   5052:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     5053:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     5054:                        SetConsoleCursorPosition(hStdout, co);
                   5055:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5056:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     5057:                        cursor_moved = false;
                   5058:                        p = is_esc = 0;
                   5059:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
                   5060:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5061:                        co.X = csbi.dwCursorPosition.X;
                   5062:                        co.Y = csbi.dwCursorPosition.Y;
                   5063:                        WORD wAttributes = csbi.wAttributes;
                   5064:                        
                   5065:                        if(tmp[1] == 'D') {
                   5066:                                co.Y++;
                   5067:                        } else if(tmp[1] == 'E') {
                   5068:                                co.X = 0;
                   5069:                                co.Y++;
                   5070:                        } else if(tmp[1] == 'M') {
                   5071:                                co.Y--;
                   5072:                        } else if(tmp[1] == '*') {
1.1.1.14  root     5073:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5074:                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5075:                                co.X = 0;
                   5076:                                co.Y = csbi.srWindow.Top;
1.1       root     5077:                        } else if(tmp[1] == '[') {
                   5078:                                int param[256], params = 0;
                   5079:                                memset(param, 0, sizeof(param));
                   5080:                                for(int i = 2; i < p; i++) {
                   5081:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   5082:                                                param[params] *= 10;
                   5083:                                                param[params] += tmp[i] - '0';
                   5084:                                        } else {
                   5085:                                                params++;
                   5086:                                        }
                   5087:                                }
                   5088:                                if(data == 'A') {
1.1.1.14  root     5089:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     5090:                                } else if(data == 'B') {
1.1.1.14  root     5091:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     5092:                                } else if(data == 'C') {
1.1.1.14  root     5093:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     5094:                                } else if(data == 'D') {
1.1.1.14  root     5095:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     5096:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     5097:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   5098:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     5099:                                } else if(data == 'J') {
1.1.1.14  root     5100:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5101:                                        if(param[0] == 0) {
                   5102:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5103:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5104:                                                if(co.Y < csbi.srWindow.Bottom) {
                   5105:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5106:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5107:                                                }
                   5108:                                        } else if(param[0] == 1) {
1.1.1.14  root     5109:                                                if(co.Y > csbi.srWindow.Top) {
                   5110:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
                   5111:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5112:                                                }
                   5113:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5114:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5115:                                        } else if(param[0] == 2) {
1.1.1.14  root     5116:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5117:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5118:                                                co.X = co.Y = 0;
                   5119:                                        }
                   5120:                                } else if(data == 'K') {
1.1.1.14  root     5121:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5122:                                        if(param[0] == 0) {
                   5123:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5124:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5125:                                        } else if(param[0] == 1) {
                   5126:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5127:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5128:                                        } else if(param[0] == 2) {
                   5129:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5130:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5131:                                        }
                   5132:                                } else if(data == 'L') {
1.1.1.14  root     5133:                                        if(params == 0) {
                   5134:                                                param[0] = 1;
1.1       root     5135:                                        }
1.1.1.14  root     5136:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5137:                                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5138:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5139:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5140:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5141:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14  root     5142:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5143:                                        co.X = 0;
                   5144:                                } else if(data == 'M') {
1.1.1.14  root     5145:                                        if(params == 0) {
                   5146:                                                param[0] = 1;
                   5147:                                        }
                   5148:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5149:                                                clear_scr_buffer(csbi.wAttributes);
                   5150:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5151:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5152:                                        } else {
1.1.1.14  root     5153:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5154:                                                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5155:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5156:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5157:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5158:                                        }
                   5159:                                        co.X = 0;
                   5160:                                } else if(data == 'h') {
                   5161:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5162:                                                CONSOLE_CURSOR_INFO cur;
                   5163:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5164:                                                if(cur.bVisible) {
                   5165:                                                        cur.bVisible = FALSE;
1.1.1.14  root     5166: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5167:                                                }
                   5168:                                        }
                   5169:                                } else if(data == 'l') {
                   5170:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5171:                                                CONSOLE_CURSOR_INFO cur;
                   5172:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5173:                                                if(!cur.bVisible) {
                   5174:                                                        cur.bVisible = TRUE;
1.1.1.14  root     5175: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5176:                                                }
                   5177:                                        }
                   5178:                                } else if(data == 'm') {
                   5179:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5180:                                        int reverse = 0, hidden = 0;
                   5181:                                        for(int i = 0; i < params; i++) {
                   5182:                                                if(param[i] == 1) {
                   5183:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5184:                                                } else if(param[i] == 4) {
                   5185:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5186:                                                } else if(param[i] == 7) {
                   5187:                                                        reverse = 1;
                   5188:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5189:                                                        hidden = 1;
                   5190:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5191:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5192:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5193:                                                                param[i] -= 16;
                   5194:                                                        } else {
                   5195:                                                                param[i] -= 30;
                   5196:                                                        }
                   5197:                                                        if(param[i] & 1) {
                   5198:                                                                wAttributes |= FOREGROUND_RED;
                   5199:                                                        }
                   5200:                                                        if(param[i] & 2) {
                   5201:                                                                wAttributes |= FOREGROUND_GREEN;
                   5202:                                                        }
                   5203:                                                        if(param[i] & 4) {
                   5204:                                                                wAttributes |= FOREGROUND_BLUE;
                   5205:                                                        }
                   5206:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5207:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5208:                                                        if((param[i] - 40) & 1) {
                   5209:                                                                wAttributes |= BACKGROUND_RED;
                   5210:                                                        }
                   5211:                                                        if((param[i] - 40) & 2) {
                   5212:                                                                wAttributes |= BACKGROUND_GREEN;
                   5213:                                                        }
                   5214:                                                        if((param[i] - 40) & 4) {
                   5215:                                                                wAttributes |= BACKGROUND_BLUE;
                   5216:                                                        }
                   5217:                                                }
                   5218:                                        }
                   5219:                                        if(reverse) {
                   5220:                                                wAttributes &= ~0xff;
                   5221:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5222:                                        }
                   5223:                                        if(hidden) {
                   5224:                                                wAttributes &= ~0x0f;
                   5225:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5226:                                        }
                   5227:                                } else if(data == 'n') {
                   5228:                                        if(param[0] == 6) {
                   5229:                                                char tmp[16];
                   5230:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5231:                                                int len = strlen(tmp);
1.1.1.32  root     5232:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5233: #ifdef USE_SERVICE_THREAD
                   5234:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5235: #endif
1.1.1.32  root     5236:                                                        for(int i = 0; i < len; i++) {
                   5237:                                                                key_buf_char->write(tmp[i]);
                   5238:                                                                key_buf_scan->write(0x00);
                   5239:                                                        }
1.1.1.35  root     5240: #ifdef USE_SERVICE_THREAD
                   5241:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5242: #endif
1.1       root     5243:                                                }
                   5244:                                        }
                   5245:                                } else if(data == 's') {
                   5246:                                        stored_x = co.X;
                   5247:                                        stored_y = co.Y;
                   5248:                                        stored_a = wAttributes;
                   5249:                                } else if(data == 'u') {
                   5250:                                        co.X = stored_x;
                   5251:                                        co.Y = stored_y;
                   5252:                                        wAttributes = stored_a;
                   5253:                                }
                   5254:                        }
                   5255:                        if(co.X < 0) {
                   5256:                                co.X = 0;
                   5257:                        } else if(co.X >= csbi.dwSize.X) {
                   5258:                                co.X = csbi.dwSize.X - 1;
                   5259:                        }
1.1.1.14  root     5260:                        if(co.Y < csbi.srWindow.Top) {
                   5261:                                co.Y = csbi.srWindow.Top;
                   5262:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5263:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5264:                        }
                   5265:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5266:                                SetConsoleCursorPosition(hStdout, co);
                   5267:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5268:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5269:                                cursor_moved = false;
                   5270:                        }
                   5271:                        if(wAttributes != csbi.wAttributes) {
                   5272:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5273:                        }
                   5274:                        p = is_esc = 0;
                   5275:                }
                   5276:                return;
                   5277:        } else {
                   5278:                if(msdos_lead_byte_check(data)) {
                   5279:                        is_kanji = 1;
                   5280:                        return;
                   5281:                } else if(data == 0x1b) {
                   5282:                        is_esc = 1;
                   5283:                        return;
                   5284:                }
                   5285:        }
1.1.1.20  root     5286:        
                   5287:        DWORD q = 0, num;
                   5288:        is_kanji = 0;
                   5289:        for(int i = 0; i < p; i++) {
                   5290:                UINT8 c = tmp[i];
                   5291:                if(is_kanji) {
                   5292:                        is_kanji = 0;
                   5293:                } else if(msdos_lead_byte_check(data)) {
                   5294:                        is_kanji = 1;
                   5295:                } else if(msdos_ctrl_code_check(data)) {
                   5296:                        out[q++] = '^';
                   5297:                        c += 'A' - 1;
                   5298:                }
                   5299:                out[q++] = c;
                   5300:        }
1.1.1.34  root     5301:        if(q == 1 && out[0] == 0x08) {
                   5302:                // back space
                   5303:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5304:                if(csbi.dwCursorPosition.X > 0) {
                   5305:                        co.X = csbi.dwCursorPosition.X - 1;
                   5306:                        co.Y = csbi.dwCursorPosition.Y;
                   5307:                        SetConsoleCursorPosition(hStdout, co);
                   5308:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5309:                        co.X = csbi.dwSize.X - 1;
                   5310:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5311:                        SetConsoleCursorPosition(hStdout, co);
                   5312:                } else {
                   5313:                        WriteConsole(hStdout, out, q, &num, NULL); // to make sure
                   5314:                }
                   5315:        } else {
                   5316:                WriteConsole(hStdout, out, q, &num, NULL);
                   5317:        }
1.1       root     5318:        p = 0;
1.1.1.14  root     5319:        
1.1.1.15  root     5320:        if(!restore_console_on_exit) {
                   5321:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5322:                scr_top = csbi.srWindow.Top;
                   5323:        }
1.1       root     5324:        cursor_moved = true;
                   5325: }
                   5326: 
                   5327: int msdos_aux_in()
                   5328: {
1.1.1.21  root     5329:        msdos_stdio_reopen();
                   5330:        
1.1.1.20  root     5331:        process_t *process = msdos_process_info_get(current_psp);
                   5332:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5333:        
                   5334:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5335:                char data = 0;
1.1.1.37  root     5336:                msdos_read(fd, &data, 1);
1.1       root     5337:                return(data);
                   5338:        } else {
                   5339:                return(EOF);
                   5340:        }
                   5341: }
                   5342: 
                   5343: void msdos_aux_out(char data)
                   5344: {
1.1.1.21  root     5345:        msdos_stdio_reopen();
                   5346:        
1.1.1.20  root     5347:        process_t *process = msdos_process_info_get(current_psp);
                   5348:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5349:        
                   5350:        if(fd < process->max_files && file_handler[fd].valid) {
                   5351:                msdos_write(fd, &data, 1);
1.1       root     5352:        }
                   5353: }
                   5354: 
                   5355: void msdos_prn_out(char data)
                   5356: {
1.1.1.21  root     5357:        msdos_stdio_reopen();
                   5358:        
1.1.1.20  root     5359:        process_t *process = msdos_process_info_get(current_psp);
                   5360:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5361:        
                   5362:        if(fd < process->max_files && file_handler[fd].valid) {
                   5363:                msdos_write(fd, &data, 1);
1.1       root     5364:        }
                   5365: }
                   5366: 
                   5367: // memory control
                   5368: 
1.1.1.39  root     5369: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, char *prog_name)
1.1       root     5370: {
                   5371:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5372:        
                   5373:        mcb->mz = mz;
                   5374:        mcb->psp = psp;
1.1.1.30  root     5375:        mcb->paragraphs = paragraphs;
1.1.1.39  root     5376:        
                   5377:        if(prog_name != NULL) {
                   5378:                memset(mcb->prog_name, 0, 8);
                   5379:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
                   5380:        }
1.1       root     5381:        return(mcb);
                   5382: }
                   5383: 
1.1.1.39  root     5384: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs)
                   5385: {
                   5386:        return(msdos_mcb_create(mcb_seg, mz, psp, paragraphs, NULL));
                   5387: }
                   5388: 
1.1       root     5389: void msdos_mcb_check(mcb_t *mcb)
                   5390: {
                   5391:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5392:                #if 0
                   5393:                        // shutdown now !!!
                   5394:                        fatalerror("broken memory control block\n");
                   5395:                #else
                   5396:                        // return error code and continue
                   5397:                        throw(0x07); // broken memory control block
                   5398:                #endif
1.1       root     5399:        }
                   5400: }
                   5401: 
1.1.1.39  root     5402: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5403: {
                   5404:        int mcb_seg = seg - 1;
                   5405:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5406:        msdos_mcb_check(mcb);
                   5407:        
1.1.1.30  root     5408:        if(mcb->paragraphs > paragraphs) {
1.1       root     5409:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5410:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5411:                
                   5412:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5413:                mcb->mz = 'M';
1.1.1.30  root     5414:                mcb->paragraphs = paragraphs;
1.1       root     5415:        }
                   5416: }
                   5417: 
                   5418: void msdos_mem_merge(int seg)
                   5419: {
                   5420:        int mcb_seg = seg - 1;
                   5421:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5422:        msdos_mcb_check(mcb);
                   5423:        
                   5424:        while(1) {
                   5425:                if(mcb->mz == 'Z') {
                   5426:                        break;
                   5427:                }
1.1.1.30  root     5428:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5429:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5430:                msdos_mcb_check(next_mcb);
                   5431:                
                   5432:                if(next_mcb->psp != 0) {
                   5433:                        break;
                   5434:                }
                   5435:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5436:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5437:        }
                   5438: }
                   5439: 
1.1.1.8   root     5440: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5441: {
                   5442:        while(1) {
                   5443:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5444:                bool last_block;
1.1       root     5445:                
1.1.1.14  root     5446:                if(mcb->psp == 0) {
                   5447:                        msdos_mem_merge(mcb_seg + 1);
                   5448:                } else {
                   5449:                        msdos_mcb_check(mcb);
                   5450:                }
1.1.1.33  root     5451:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5452:                        // check if the next is dummy mcb to link to umb
                   5453:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5454:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5455:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5456:                }
                   5457:                if(!(new_process && !last_block)) {
1.1.1.30  root     5458:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5459:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5460:                                mcb->psp = current_psp;
                   5461:                                return(mcb_seg + 1);
                   5462:                        }
                   5463:                }
                   5464:                if(mcb->mz == 'Z') {
                   5465:                        break;
                   5466:                }
1.1.1.30  root     5467:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5468:        }
                   5469:        return(-1);
                   5470: }
                   5471: 
                   5472: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5473: {
                   5474:        int mcb_seg = seg - 1;
                   5475:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5476:        msdos_mcb_check(mcb);
1.1.1.30  root     5477:        int current_paragraphs = mcb->paragraphs;
1.1       root     5478:        
                   5479:        msdos_mem_merge(seg);
1.1.1.30  root     5480:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5481:                if(max_paragraphs) {
1.1.1.30  root     5482:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5483:                }
1.1       root     5484:                msdos_mem_split(seg, current_paragraphs);
                   5485:                return(-1);
                   5486:        }
                   5487:        msdos_mem_split(seg, paragraphs);
                   5488:        return(0);
                   5489: }
                   5490: 
                   5491: void msdos_mem_free(int seg)
                   5492: {
                   5493:        int mcb_seg = seg - 1;
                   5494:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5495:        msdos_mcb_check(mcb);
                   5496:        
                   5497:        mcb->psp = 0;
                   5498:        msdos_mem_merge(seg);
                   5499: }
                   5500: 
1.1.1.8   root     5501: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5502: {
                   5503:        int max_paragraphs = 0;
                   5504:        
                   5505:        while(1) {
                   5506:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5507:                bool last_block;
                   5508:                
1.1       root     5509:                msdos_mcb_check(mcb);
                   5510:                
1.1.1.33  root     5511:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5512:                        // check if the next is dummy mcb to link to umb
                   5513:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5514:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5515:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5516:                }
                   5517:                if(!(new_process && !last_block)) {
1.1.1.30  root     5518:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   5519:                                max_paragraphs = mcb->paragraphs;
1.1       root     5520:                        }
                   5521:                }
                   5522:                if(mcb->mz == 'Z') {
                   5523:                        break;
                   5524:                }
1.1.1.30  root     5525:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5526:        }
1.1.1.14  root     5527:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     5528: }
                   5529: 
1.1.1.8   root     5530: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   5531: {
                   5532:        int last_seg = -1;
                   5533:        
                   5534:        while(1) {
                   5535:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5536:                msdos_mcb_check(mcb);
                   5537:                
1.1.1.14  root     5538:                if(mcb->psp == psp) {
1.1.1.8   root     5539:                        last_seg = mcb_seg;
                   5540:                }
1.1.1.14  root     5541:                if(mcb->mz == 'Z') {
                   5542:                        break;
                   5543:                }
1.1.1.30  root     5544:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     5545:        }
                   5546:        return(last_seg);
                   5547: }
                   5548: 
1.1.1.19  root     5549: int msdos_mem_get_umb_linked()
                   5550: {
1.1.1.33  root     5551:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5552:        msdos_mcb_check(mcb);
1.1.1.19  root     5553:        
1.1.1.33  root     5554:        if(mcb->mz == 'M') {
                   5555:                return(-1);
1.1.1.19  root     5556:        }
                   5557:        return(0);
                   5558: }
                   5559: 
1.1.1.33  root     5560: void msdos_mem_link_umb()
1.1.1.19  root     5561: {
1.1.1.33  root     5562:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5563:        msdos_mcb_check(mcb);
1.1.1.19  root     5564:        
1.1.1.33  root     5565:        mcb->mz = 'M';
                   5566:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39  root     5567:        
                   5568:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     5569: }
                   5570: 
1.1.1.33  root     5571: void msdos_mem_unlink_umb()
1.1.1.19  root     5572: {
1.1.1.33  root     5573:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5574:        msdos_mcb_check(mcb);
1.1.1.19  root     5575:        
1.1.1.33  root     5576:        mcb->mz = 'Z';
                   5577:        mcb->paragraphs = 0;
1.1.1.39  root     5578:        
                   5579:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     5580: }
                   5581: 
1.1.1.29  root     5582: #ifdef SUPPORT_HMA
                   5583: 
                   5584: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   5585: {
                   5586:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5587:        
                   5588:        mcb->ms[0] = 'M';
                   5589:        mcb->ms[1] = 'S';
                   5590:        mcb->owner = owner;
                   5591:        mcb->size = size;
                   5592:        mcb->next = next;
                   5593:        return(mcb);
                   5594: }
                   5595: 
                   5596: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   5597: {
                   5598:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   5599: }
                   5600: 
                   5601: int msdos_hma_mem_split(int offset, int size)
                   5602: {
                   5603:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5604:        
                   5605:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5606:                return(-1);
                   5607:        }
                   5608:        if(mcb->size >= size + 0x10) {
                   5609:                int new_offset = offset + 0x10 + size;
                   5610:                int new_size = mcb->size - 0x10 - size;
                   5611:                
                   5612:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   5613:                mcb->size = size;
                   5614:                mcb->next = new_offset;
                   5615:                return(0);
                   5616:        }
                   5617:        return(-1);
                   5618: }
                   5619: 
                   5620: void msdos_hma_mem_merge(int offset)
                   5621: {
                   5622:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5623:        
                   5624:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5625:                return;
                   5626:        }
                   5627:        while(1) {
                   5628:                if(mcb->next == 0) {
                   5629:                        break;
                   5630:                }
                   5631:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   5632:                
                   5633:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   5634:                        return;
                   5635:                }
                   5636:                if(next_mcb->owner != 0) {
                   5637:                        break;
                   5638:                }
                   5639:                mcb->size += 0x10 + next_mcb->size;
                   5640:                mcb->next = next_mcb->next;
                   5641:        }
                   5642: }
                   5643: 
                   5644: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   5645: {
                   5646:        int offset = 0x10; // first mcb in HMA
                   5647:        
                   5648:        while(1) {
                   5649:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5650:                
                   5651:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5652:                        return(-1);
                   5653:                }
                   5654:                if(mcb->owner == 0) {
                   5655:                        msdos_hma_mem_merge(offset);
                   5656:                }
                   5657:                if(mcb->owner == 0 && mcb->size >= size) {
                   5658:                        msdos_hma_mem_split(offset, size);
                   5659:                        mcb->owner = owner;
                   5660:                        return(offset);
                   5661:                }
                   5662:                if(mcb->next == 0) {
                   5663:                        break;
                   5664:                }
                   5665:                offset = mcb->next;
                   5666:        }
                   5667:        return(-1);
                   5668: }
                   5669: 
                   5670: int msdos_hma_mem_realloc(int offset, int size)
                   5671: {
                   5672:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5673:        
                   5674:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5675:                return(-1);
                   5676:        }
                   5677:        if(mcb->size < size) {
                   5678:                return(-1);
                   5679:        }
                   5680:        msdos_hma_mem_split(offset, size);
                   5681:        return(0);
                   5682: }
                   5683: 
                   5684: void msdos_hma_mem_free(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:        mcb->owner = 0;
                   5692:        msdos_hma_mem_merge(offset);
                   5693: }
                   5694: 
                   5695: int msdos_hma_mem_get_free(int *available_offset)
                   5696: {
                   5697:        int offset = 0x10; // first mcb in HMA
                   5698:        int size = 0;
                   5699:        
                   5700:        while(1) {
                   5701:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5702:                
                   5703:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5704:                        return(0);
                   5705:                }
                   5706:                if(mcb->owner == 0 && size < mcb->size) {
                   5707:                        if(available_offset != NULL) {
                   5708:                                *available_offset = offset;
                   5709:                        }
                   5710:                        size = mcb->size;
                   5711:                }
                   5712:                if(mcb->next == 0) {
                   5713:                        break;
                   5714:                }
                   5715:                offset = mcb->next;
                   5716:        }
                   5717:        return(size);
                   5718: }
                   5719: 
                   5720: #endif
                   5721: 
1.1       root     5722: // environment
                   5723: 
                   5724: void msdos_env_set_argv(int env_seg, char *argv)
                   5725: {
                   5726:        char *dst = (char *)(mem + (env_seg << 4));
                   5727:        
                   5728:        while(1) {
                   5729:                if(dst[0] == 0) {
                   5730:                        break;
                   5731:                }
                   5732:                dst += strlen(dst) + 1;
                   5733:        }
                   5734:        *dst++ = 0; // end of environment
                   5735:        *dst++ = 1; // top of argv[0]
                   5736:        *dst++ = 0;
                   5737:        memcpy(dst, argv, strlen(argv));
                   5738:        dst += strlen(argv);
                   5739:        *dst++ = 0;
                   5740:        *dst++ = 0;
                   5741: }
                   5742: 
                   5743: char *msdos_env_get_argv(int env_seg)
                   5744: {
                   5745:        static char env[ENV_SIZE];
                   5746:        char *src = env;
                   5747:        
                   5748:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5749:        while(1) {
                   5750:                if(src[0] == 0) {
                   5751:                        if(src[1] == 1) {
                   5752:                                return(src + 3);
                   5753:                        }
                   5754:                        break;
                   5755:                }
                   5756:                src += strlen(src) + 1;
                   5757:        }
                   5758:        return(NULL);
                   5759: }
                   5760: 
                   5761: char *msdos_env_get(int env_seg, const char *name)
                   5762: {
                   5763:        static char env[ENV_SIZE];
                   5764:        char *src = env;
                   5765:        
                   5766:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5767:        while(1) {
                   5768:                if(src[0] == 0) {
                   5769:                        break;
                   5770:                }
                   5771:                int len = strlen(src);
                   5772:                char *n = my_strtok(src, "=");
                   5773:                char *v = src + strlen(n) + 1;
                   5774:                
                   5775:                if(_stricmp(name, n) == 0) {
                   5776:                        return(v);
                   5777:                }
                   5778:                src += len + 1;
                   5779:        }
                   5780:        return(NULL);
                   5781: }
                   5782: 
                   5783: void msdos_env_set(int env_seg, char *name, char *value)
                   5784: {
                   5785:        char env[ENV_SIZE];
                   5786:        char *src = env;
                   5787:        char *dst = (char *)(mem + (env_seg << 4));
                   5788:        char *argv = msdos_env_get_argv(env_seg);
                   5789:        int done = 0;
                   5790:        
                   5791:        memcpy(src, dst, ENV_SIZE);
                   5792:        memset(dst, 0, ENV_SIZE);
                   5793:        while(1) {
                   5794:                if(src[0] == 0) {
                   5795:                        break;
                   5796:                }
                   5797:                int len = strlen(src);
                   5798:                char *n = my_strtok(src, "=");
                   5799:                char *v = src + strlen(n) + 1;
                   5800:                char tmp[1024];
                   5801:                
                   5802:                if(_stricmp(name, n) == 0) {
                   5803:                        sprintf(tmp, "%s=%s", n, value);
                   5804:                        done = 1;
                   5805:                } else {
                   5806:                        sprintf(tmp, "%s=%s", n, v);
                   5807:                }
                   5808:                memcpy(dst, tmp, strlen(tmp));
                   5809:                dst += strlen(tmp) + 1;
                   5810:                src += len + 1;
                   5811:        }
                   5812:        if(!done) {
                   5813:                char tmp[1024];
                   5814:                
                   5815:                sprintf(tmp, "%s=%s", name, value);
                   5816:                memcpy(dst, tmp, strlen(tmp));
                   5817:                dst += strlen(tmp) + 1;
                   5818:        }
                   5819:        if(argv) {
                   5820:                *dst++ = 0; // end of environment
                   5821:                *dst++ = 1; // top of argv[0]
                   5822:                *dst++ = 0;
                   5823:                memcpy(dst, argv, strlen(argv));
                   5824:                dst += strlen(argv);
                   5825:                *dst++ = 0;
                   5826:                *dst++ = 0;
                   5827:        }
                   5828: }
                   5829: 
                   5830: // process
                   5831: 
1.1.1.8   root     5832: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     5833: {
                   5834:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5835:        
                   5836:        memset(psp, 0, PSP_SIZE);
                   5837:        psp->exit[0] = 0xcd;
                   5838:        psp->exit[1] = 0x20;
1.1.1.8   root     5839:        psp->first_mcb = mcb_seg;
1.1       root     5840:        psp->far_call = 0xea;
                   5841:        psp->cpm_entry.w.l = 0xfff1;    // int 21h, retf
                   5842:        psp->cpm_entry.w.h = 0xf000;
                   5843:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   5844:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   5845:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   5846:        psp->parent_psp = parent_psp;
1.1.1.20  root     5847:        if(parent_psp == (UINT16)-1) {
                   5848:                for(int i = 0; i < 20; i++) {
                   5849:                        if(file_handler[i].valid) {
                   5850:                                psp->file_table[i] = i;
                   5851:                        } else {
                   5852:                                psp->file_table[i] = 0xff;
                   5853:                        }
1.1       root     5854:                }
1.1.1.20  root     5855:        } else {
                   5856:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     5857:        }
                   5858:        psp->env_seg = env_seg;
                   5859:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     5860:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     5861:        psp->file_table_size = 20;
                   5862:        psp->file_table_ptr.w.l = 0x18;
                   5863:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     5864:        psp->service[0] = 0xcd;
                   5865:        psp->service[1] = 0x21;
                   5866:        psp->service[2] = 0xcb;
                   5867:        return(psp);
                   5868: }
                   5869: 
1.1.1.20  root     5870: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   5871: {
                   5872:        if(psp_seg && fd < 20) {
                   5873:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5874:                psp->file_table[fd] = value;
                   5875:        }
                   5876: }
                   5877: 
                   5878: int msdos_psp_get_file_table(int fd, int psp_seg)
                   5879: {
                   5880:        if(psp_seg && fd < 20) {
                   5881:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5882:                fd = psp->file_table[fd];
                   5883:        }
                   5884:        return fd;
                   5885: }
                   5886: 
1.1       root     5887: int msdos_process_exec(char *cmd, param_block_t *param, UINT8 al)
                   5888: {
                   5889:        // load command file
                   5890:        int fd = -1;
                   5891:        int dos_command = 0;
1.1.1.24  root     5892:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     5893:        char pipe_stdin_path[MAX_PATH] = {0};
                   5894:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39  root     5895:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     5896:        
                   5897:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   5898:        int opt_len = mem[opt_ofs];
                   5899:        memset(opt, 0, sizeof(opt));
                   5900:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   5901:        
1.1.1.14  root     5902:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   5903:                // this is a batch file, run command.com
                   5904:                char tmp[MAX_PATH];
                   5905:                if(opt_len != 0) {
                   5906:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   5907:                } else {
                   5908:                        sprintf(tmp, "/C %s", cmd);
                   5909:                }
                   5910:                strcpy(opt, tmp);
                   5911:                opt_len = strlen(opt);
                   5912:                mem[opt_ofs] = opt_len;
                   5913:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5914:                strcpy(command, comspec_path);
                   5915:                strcpy(name_tmp, "COMMAND.COM");
                   5916:        } else {
                   5917:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   5918:                        // redirect C:\COMMAND.COM to comspec_path
                   5919:                        strcpy(command, comspec_path);
                   5920:                } else {
                   5921:                        strcpy(command, cmd);
                   5922:                }
1.1.1.24  root     5923:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   5924:                        return(-1);
                   5925:                }
1.1.1.14  root     5926:                memset(name_tmp, 0, sizeof(name_tmp));
                   5927:                strcpy(name_tmp, name);
                   5928:                
                   5929:                // check command.com
1.1.1.38  root     5930:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   5931:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     5932:                        if(opt_len == 0) {
                   5933: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   5934:                                process_t *current_process = NULL;
                   5935:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   5936:                                        if(process[i].psp == current_psp) {
                   5937:                                                current_process = &process[i];
                   5938:                                                break;
                   5939:                                        }
                   5940:                                }
                   5941:                                if(current_process != NULL) {
                   5942:                                        param->cmd_line.dw = current_process->dta.dw;
                   5943:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   5944:                                        opt_len = mem[opt_ofs];
                   5945:                                        memset(opt, 0, sizeof(opt));
                   5946:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   5947:                                }
                   5948:                        }
                   5949:                        for(int i = 0; i < opt_len; i++) {
                   5950:                                if(opt[i] == ' ') {
                   5951:                                        continue;
                   5952:                                }
                   5953:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   5954:                                        for(int j = i + 3; j < opt_len; j++) {
                   5955:                                                if(opt[j] == ' ') {
                   5956:                                                        continue;
                   5957:                                                }
                   5958:                                                char *token = my_strtok(opt + j, " ");
                   5959:                                                
1.1.1.38  root     5960:                                                strcpy(command, token);
                   5961:                                                char tmp[MAX_PATH];
                   5962:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39  root     5963:                                                strcpy(opt, "");
                   5964:                                                for(int i = 0; i < strlen(tmp); i++) {
                   5965:                                                        if(tmp[i] != ' ') {
                   5966:                                                                strcpy(opt, tmp + i);
                   5967:                                                                break;
                   5968:                                                        }
                   5969:                                                }
                   5970:                                                strcpy(tmp, opt);
1.1.1.38  root     5971:                                                
                   5972:                                                if(al == 0x00) {
1.1.1.39  root     5973:                                                        #define GET_FILE_PATH() { \
                   5974:                                                                if(token[0] != '>' && token[0] != '<') { \
                   5975:                                                                        token++; \
                   5976:                                                                } \
                   5977:                                                                token++; \
                   5978:                                                                while(*token == ' ') { \
                   5979:                                                                        token++; \
                   5980:                                                                } \
                   5981:                                                                char *ptr = token; \
                   5982:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
                   5983:                                                                        ptr++; \
                   5984:                                                                } \
                   5985:                                                                *ptr = '\0'; \
                   5986:                                                        }
                   5987:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
                   5988:                                                                GET_FILE_PATH();
1.1.1.38  root     5989:                                                                strcpy(pipe_stdin_path, token);
                   5990:                                                                strcpy(opt, tmp);
                   5991:                                                        }
1.1.1.39  root     5992:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
                   5993:                                                                GET_FILE_PATH();
1.1.1.38  root     5994:                                                                strcpy(pipe_stdout_path, token);
                   5995:                                                                strcpy(opt, tmp);
                   5996:                                                        }
1.1.1.39  root     5997:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   5998:                                                                GET_FILE_PATH();
                   5999:                                                                strcpy(pipe_stderr_path, token);
                   6000:                                                                strcpy(opt, tmp);
                   6001:                                                        }
                   6002:                                                        #undef GET_FILE_PATH
                   6003:                                                        
                   6004:                                                        if((token = strstr(opt, "0<")) != NULL) {
                   6005:                                                                *token = '\0';
                   6006:                                                        }
                   6007:                                                        if((token = strstr(opt, "1>")) != NULL) {
                   6008:                                                                *token = '\0';
                   6009:                                                        }
                   6010:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6011:                                                                *token = '\0';
                   6012:                                                        }
1.1.1.38  root     6013:                                                        if((token = strstr(opt, "<")) != NULL) {
                   6014:                                                                *token = '\0';
                   6015:                                                        }
                   6016:                                                        if((token = strstr(opt, ">")) != NULL) {
                   6017:                                                                *token = '\0';
                   6018:                                                        }
1.1.1.14  root     6019:                                                }
1.1.1.39  root     6020:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
                   6021:                                                        opt[i] = '\0';
                   6022:                                                }
1.1.1.38  root     6023:                                                opt_len = strlen(opt);
                   6024:                                                mem[opt_ofs] = opt_len;
                   6025:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6026:                                                dos_command = 1;
1.1.1.14  root     6027:                                                break;
1.1       root     6028:                                        }
                   6029:                                }
1.1.1.14  root     6030:                                break;
1.1       root     6031:                        }
                   6032:                }
                   6033:        }
                   6034:        
                   6035:        // load command file
                   6036:        strcpy(path, command);
                   6037:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6038:                sprintf(path, "%s.COM", command);
                   6039:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6040:                        sprintf(path, "%s.EXE", command);
                   6041:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     6042:                                sprintf(path, "%s.BAT", command);
                   6043:                                if(_access(path, 0) == 0) {
                   6044:                                        // this is a batch file, run command.com
                   6045:                                        char tmp[MAX_PATH];
                   6046:                                        if(opt_len != 0) {
                   6047:                                                sprintf(tmp, "/C %s %s", path, opt);
                   6048:                                        } else {
                   6049:                                                sprintf(tmp, "/C %s", path);
                   6050:                                        }
                   6051:                                        strcpy(opt, tmp);
                   6052:                                        opt_len = strlen(opt);
                   6053:                                        mem[opt_ofs] = opt_len;
                   6054:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6055:                                        strcpy(path, comspec_path);
                   6056:                                        strcpy(name_tmp, "COMMAND.COM");
                   6057:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6058:                                } else {
                   6059:                                        // search path in parent environments
                   6060:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6061:                                        char *env = msdos_env_get(parent_psp->env_seg, "PATH");
                   6062:                                        if(env != NULL) {
                   6063:                                                char env_path[4096];
                   6064:                                                strcpy(env_path, env);
                   6065:                                                char *token = my_strtok(env_path, ";");
                   6066:                                                
                   6067:                                                while(token != NULL) {
                   6068:                                                        if(strlen(token) != 0) {
                   6069:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   6070:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6071:                                                                        break;
                   6072:                                                                }
                   6073:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   6074:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6075:                                                                        break;
                   6076:                                                                }
                   6077:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   6078:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6079:                                                                        break;
                   6080:                                                                }
                   6081:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   6082:                                                                if(_access(path, 0) == 0) {
                   6083:                                                                        // this is a batch file, run command.com
                   6084:                                                                        char tmp[MAX_PATH];
                   6085:                                                                        if(opt_len != 0) {
                   6086:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   6087:                                                                        } else {
                   6088:                                                                                sprintf(tmp, "/C %s", path);
                   6089:                                                                        }
                   6090:                                                                        strcpy(opt, tmp);
                   6091:                                                                        opt_len = strlen(opt);
                   6092:                                                                        mem[opt_ofs] = opt_len;
                   6093:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6094:                                                                        strcpy(path, comspec_path);
                   6095:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   6096:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6097:                                                                        break;
                   6098:                                                                }
1.1.1.8   root     6099:                                                        }
1.1.1.14  root     6100:                                                        token = my_strtok(NULL, ";");
1.1       root     6101:                                                }
                   6102:                                        }
                   6103:                                }
                   6104:                        }
                   6105:                }
                   6106:        }
                   6107:        if(fd == -1) {
1.1.1.38  root     6108:                // we can not find command.com in the path, so open comspec_path
                   6109:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6110:                        strcpy(command, comspec_path);
                   6111:                        strcpy(path, command);
                   6112:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6113:                }
                   6114:        }
                   6115:        if(fd == -1) {
1.1       root     6116:                if(dos_command) {
                   6117:                        // may be dos command
                   6118:                        char tmp[MAX_PATH];
                   6119:                        sprintf(tmp, "%s %s", command, opt);
                   6120:                        system(tmp);
                   6121:                        return(0);
                   6122:                } else {
                   6123:                        return(-1);
                   6124:                }
                   6125:        }
                   6126:        _read(fd, file_buffer, sizeof(file_buffer));
                   6127:        _close(fd);
                   6128:        
                   6129:        // copy environment
1.1.1.29  root     6130:        int umb_linked, env_seg, psp_seg;
1.1       root     6131:        
1.1.1.29  root     6132:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6133:                msdos_mem_unlink_umb();
                   6134:        }
1.1.1.8   root     6135:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6136:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6137:                        if(umb_linked != 0) {
                   6138:                                msdos_mem_link_umb();
                   6139:                        }
                   6140:                        return(-1);
                   6141:                }
1.1       root     6142:        }
                   6143:        if(param->env_seg == 0) {
                   6144:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6145:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6146:        } else {
                   6147:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6148:        }
                   6149:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6150:        
                   6151:        // check exe header
                   6152:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6153:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6154:        UINT16 cs, ss, ip, sp;
                   6155:        
                   6156:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6157:                // memory allocation
                   6158:                int header_size = header->header_size * 16;
                   6159:                int load_size = header->pages * 512 - header_size;
                   6160:                if(header_size + load_size < 512) {
                   6161:                        load_size = 512 - header_size;
                   6162:                }
                   6163:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6164:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6165:                        msdos_mem_free(env_seg);
                   6166:                        return(-1);
                   6167:                }
                   6168:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6169:                if(paragraphs > free_paragraphs) {
                   6170:                        paragraphs = free_paragraphs;
                   6171:                }
1.1.1.8   root     6172:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6173:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6174:                                if(umb_linked != 0) {
                   6175:                                        msdos_mem_link_umb();
                   6176:                                }
                   6177:                                msdos_mem_free(env_seg);
                   6178:                                return(-1);
                   6179:                        }
1.1       root     6180:                }
                   6181:                // relocation
                   6182:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6183:                for(int i = 0; i < header->relocations; i++) {
                   6184:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6185:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6186:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6187:                }
                   6188:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6189:                // segments
                   6190:                cs = header->init_cs + start_seg;
                   6191:                ss = header->init_ss + start_seg;
                   6192:                ip = header->init_ip;
                   6193:                sp = header->init_sp - 2; // for symdeb
                   6194:        } else {
                   6195:                // memory allocation
                   6196:                paragraphs = free_paragraphs;
1.1.1.8   root     6197:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6198:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6199:                                if(umb_linked != 0) {
                   6200:                                        msdos_mem_link_umb();
                   6201:                                }
                   6202:                                msdos_mem_free(env_seg);
                   6203:                                return(-1);
                   6204:                        }
1.1       root     6205:                }
                   6206:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6207:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6208:                // segments
                   6209:                cs = ss = psp_seg;
                   6210:                ip = 0x100;
                   6211:                sp = 0xfffe;
                   6212:        }
1.1.1.29  root     6213:        if(umb_linked != 0) {
                   6214:                msdos_mem_link_umb();
                   6215:        }
1.1       root     6216:        
                   6217:        // create psp
1.1.1.3   root     6218:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6219:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6220:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6221:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6222:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6223:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6224:        
                   6225:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6226:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6227:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6228:        
1.1.1.4   root     6229:        for(int i = 0; i < 8; i++) {
                   6230:                if(name_tmp[i] == '.') {
                   6231:                        mcb_psp->prog_name[i] = '\0';
                   6232:                        break;
                   6233:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6234:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6235:                        i++;
                   6236:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6237:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6238:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6239:                } else {
                   6240:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6241:                }
                   6242:        }
                   6243:        
1.1       root     6244:        // process info
                   6245:        process_t *process = msdos_process_info_create(psp_seg);
                   6246:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6247: #ifdef USE_DEBUGGER
                   6248:        strcpy(process->module_path, path);
                   6249: #endif
1.1       root     6250:        process->dta.w.l = 0x80;
                   6251:        process->dta.w.h = psp_seg;
                   6252:        process->switchar = '/';
                   6253:        process->max_files = 20;
                   6254:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6255:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6256:        process->parent_ds = SREG(DS);
1.1.1.31  root     6257:        process->parent_es = SREG(ES);
1.1       root     6258:        
                   6259:        current_psp = psp_seg;
1.1.1.23  root     6260:        msdos_sda_update(current_psp);
1.1       root     6261:        
                   6262:        if(al == 0x00) {
                   6263:                int_10h_feh_called = int_10h_ffh_called = false;
                   6264:                
1.1.1.38  root     6265:                // pipe
                   6266:                if(pipe_stdin_path[0] != '\0') {
                   6267:                        if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
                   6268:                                UINT16 info = msdos_drive_number(pipe_stdin_path);
                   6269:                                msdos_file_handler_open(fd, pipe_stdin_path, _isatty(fd), 0, info, current_psp);
                   6270:                                psp->file_table[0] = fd;
                   6271:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6272:                        }
                   6273:                }
                   6274:                if(pipe_stdout_path[0] != '\0') {
                   6275:                        if(_access(pipe_stdout_path, 0) == 0) {
                   6276:                                SetFileAttributes(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6277:                                DeleteFile(pipe_stdout_path);
                   6278:                        }
                   6279:                        if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT)) != -1) {
                   6280:                                UINT16 info = msdos_drive_number(pipe_stdout_path);
                   6281:                                msdos_file_handler_open(fd, pipe_stdout_path, _isatty(fd), 1, info, current_psp);
                   6282:                                psp->file_table[1] = fd;
                   6283:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6284:                        }
                   6285:                }
1.1.1.39  root     6286:                if(pipe_stderr_path[0] != '\0') {
                   6287:                        if(_access(pipe_stderr_path, 0) == 0) {
                   6288:                                SetFileAttributes(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
                   6289:                                DeleteFile(pipe_stderr_path);
                   6290:                        }
                   6291:                        if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT)) != -1) {
                   6292:                                UINT16 info = msdos_drive_number(pipe_stderr_path);
                   6293:                                msdos_file_handler_open(fd, pipe_stderr_path, _isatty(fd), 2, info, current_psp);
                   6294:                                psp->file_table[2] = fd;
                   6295:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6296:                        }
                   6297:                }
1.1.1.38  root     6298:                
1.1       root     6299:                // registers and segments
                   6300:                REG16(AX) = REG16(BX) = 0x00;
                   6301:                REG16(CX) = 0xff;
                   6302:                REG16(DX) = psp_seg;
                   6303:                REG16(SI) = ip;
                   6304:                REG16(DI) = sp;
                   6305:                REG16(SP) = sp;
1.1.1.3   root     6306:                SREG(DS) = SREG(ES) = psp_seg;
                   6307:                SREG(SS) = ss;
                   6308:                i386_load_segment_descriptor(DS);
                   6309:                i386_load_segment_descriptor(ES);
                   6310:                i386_load_segment_descriptor(SS);
1.1       root     6311:                
                   6312:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6313:                i386_jmp_far(cs, ip);
                   6314:        } else if(al == 0x01) {
                   6315:                // copy ss:sp and cs:ip to param block
                   6316:                param->sp = sp;
                   6317:                param->ss = ss;
                   6318:                param->ip = ip;
                   6319:                param->cs = cs;
1.1.1.31  root     6320:                
                   6321:                // the AX value to be passed to the child program is put on top of the child's stack
                   6322:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6323:        }
                   6324:        return(0);
                   6325: }
                   6326: 
                   6327: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6328: {
                   6329:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6330:        
                   6331:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6332:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6333:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6334:        
1.1.1.3   root     6335:        SREG(SS) = psp->stack.w.h;
                   6336:        i386_load_segment_descriptor(SS);
1.1       root     6337:        REG16(SP) = psp->stack.w.l;
                   6338:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6339:        
1.1.1.28  root     6340: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6341:        process_t *current_process = NULL;
                   6342:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6343:                if(process[i].psp == psp_seg) {
                   6344:                        current_process = &process[i];
                   6345:                        break;
                   6346:                }
                   6347:        }
                   6348:        if(current_process == NULL) {
                   6349:                throw(0x1f); // general failure
                   6350:        }
                   6351:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6352:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6353:        if(current_process->called_by_int2eh) {
                   6354:                REG16(AX) = ret;
                   6355:        }
                   6356:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6357:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6358:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6359:        i386_load_segment_descriptor(ES);
1.1       root     6360:        
                   6361:        if(mem_free) {
1.1.1.8   root     6362:                int mcb_seg;
                   6363:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6364:                        msdos_mem_free(mcb_seg + 1);
                   6365:                }
                   6366:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6367:                        msdos_mem_free(mcb_seg + 1);
                   6368:                }
1.1       root     6369:                
                   6370:                for(int i = 0; i < MAX_FILES; i++) {
                   6371:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6372:                                _close(i);
1.1.1.20  root     6373:                                msdos_file_handler_close(i);
                   6374:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6375:                        }
                   6376:                }
1.1.1.13  root     6377:                msdos_dta_info_free(psp_seg);
1.1       root     6378:        }
1.1.1.14  root     6379:        msdos_stdio_reopen();
1.1       root     6380:        
1.1.1.28  root     6381:        memset(current_process, 0, sizeof(process_t));
1.1       root     6382:        
                   6383:        current_psp = psp->parent_psp;
                   6384:        retval = ret;
1.1.1.23  root     6385:        msdos_sda_update(current_psp);
1.1       root     6386: }
                   6387: 
                   6388: // drive
                   6389: 
1.1.1.42  root     6390: int pcbios_update_drive_param(int drive_num, int force_update);
                   6391: 
1.1       root     6392: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6393: {
1.1.1.41  root     6394:        if(!(drive_num >= 0 && drive_num < 26)) {
                   6395:                return(0);
                   6396:        }
1.1.1.42  root     6397:        pcbios_update_drive_param(drive_num, force_update);
                   6398:        
                   6399:        drive_param_t *drive_param = &drive_params[drive_num];
1.1       root     6400:        *seg = DPB_TOP >> 4;
                   6401:        *ofs = sizeof(dpb_t) * drive_num;
                   6402:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6403:        
                   6404:        memset(dpb, 0, sizeof(dpb_t));
                   6405:        
1.1.1.41  root     6406:        dpb->drive_num = drive_num;
                   6407:        dpb->unit_num = drive_num;
1.1.1.42  root     6408:        
                   6409:        if(drive_param->valid) {
                   6410:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   6411:                
                   6412:                dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
                   6413:                dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
                   6414:                dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6415:                dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6416:                switch(geo->MediaType) {
                   6417:                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6418:                        dpb->media_type = 0xff;
                   6419:                        break;
                   6420:                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6421:                        dpb->media_type = 0xfe;
                   6422:                        break;
                   6423:                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6424:                        dpb->media_type = 0xfd;
                   6425:                        break;
                   6426:                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6427:                        dpb->media_type = 0xfc;
                   6428:                        break;
                   6429:                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6430:                case F3_1Pt2_512:
                   6431:                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6432:                case F5_720_512:
                   6433:                        dpb->media_type = 0xf9;
                   6434:                        break;
                   6435:                case FixedMedia:        // hard disk
                   6436:                case RemovableMedia:
                   6437:                case Unknown:
                   6438:                        dpb->media_type = 0xf8;
                   6439:                        break;
                   6440:                default:
                   6441:                        dpb->media_type = 0xf0;
                   6442:                        break;
                   6443:                }
                   6444:        }
1.1.1.41  root     6445:        dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
                   6446:        dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42  root     6447:        dpb->info_sector = 0xffff;
                   6448:        dpb->backup_boot_sector = 0xffff;
                   6449:        dpb->free_clusters = 0xffff;
                   6450:        dpb->free_search_cluster = 0xffffffff;
                   6451:        
                   6452:        return(drive_param->valid);
1.1       root     6453: }
                   6454: 
                   6455: // pc bios
                   6456: 
1.1.1.35  root     6457: #ifdef USE_SERVICE_THREAD
                   6458: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   6459: {
                   6460: #if defined(HAS_I386)
                   6461:        if(m_SF != 0) {
                   6462:                m_SF = 0;
                   6463:                mem[0xfffd0 + 0x15] = 0x79;     // jns -4
                   6464:        } else {
                   6465:                m_SF = 1;
                   6466:                mem[0xfffd0 + 0x15] = 0x78;     // js -4
                   6467:        }
                   6468: #else
                   6469:        if(m_SignVal < 0) {
                   6470:                m_SignVal = 0;
                   6471:                mem[0xfffd0 + 0x15] = 0x79;     // jns -4
                   6472:        } else {
                   6473:                m_SignVal = -1;
                   6474:                mem[0xfffd0 + 0x15] = 0x78;     // js -4
                   6475:        }
                   6476: #endif
                   6477:        i386_call_far(0xfffd, 0x0013);
                   6478:        in_service = true;
                   6479:        service_exit = false;
                   6480:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   6481: }
                   6482: 
                   6483: void finish_service_loop()
                   6484: {
                   6485:        if(in_service && service_exit) {
                   6486: #if defined(HAS_I386)
                   6487:                if(m_SF != 0) {
                   6488:                        m_SF = 0;
                   6489:                } else {
                   6490:                        m_SF = 1;
                   6491:                }
                   6492: #else
                   6493:                if(m_SignVal < 0) {
                   6494:                        m_SignVal = 0;
                   6495:                } else {
                   6496:                        m_SignVal = -1;
                   6497:                }
                   6498: #endif
                   6499:                in_service = false;
                   6500:        }
                   6501: }
                   6502: #endif
                   6503: 
1.1.1.19  root     6504: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   6505: {
                   6506:        static unsigned __int64 start_msec_since_midnight = 0;
                   6507:        static unsigned __int64 start_msec_since_hostboot = 0;
                   6508:        
                   6509:        if(start_msec_since_midnight == 0) {
                   6510:                SYSTEMTIME time;
                   6511:                GetLocalTime(&time);
                   6512:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   6513:                start_msec_since_hostboot = cur_msec;
                   6514:        }
                   6515:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   6516:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   6517:        return (UINT32)tick;
                   6518: }
                   6519: 
                   6520: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   6521: {
                   6522:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   6523:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   6524:        
                   6525:        if(prev_tick > next_tick) {
                   6526:                mem[0x470] = 1;
                   6527:        }
                   6528:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   6529: }
                   6530: 
1.1.1.14  root     6531: inline void pcbios_irq0()
                   6532: {
                   6533:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     6534:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     6535: }
                   6536: 
1.1.1.16  root     6537: int pcbios_get_text_vram_address(int page)
1.1       root     6538: {
                   6539:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6540:                return TEXT_VRAM_TOP;
1.1       root     6541:        } else {
1.1.1.14  root     6542:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     6543:        }
                   6544: }
                   6545: 
1.1.1.16  root     6546: int pcbios_get_shadow_buffer_address(int page)
1.1       root     6547: {
1.1.1.14  root     6548:        if(!int_10h_feh_called) {
1.1.1.16  root     6549:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     6550:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6551:                return SHADOW_BUF_TOP;
                   6552:        } else {
1.1.1.14  root     6553:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     6554:        }
                   6555: }
                   6556: 
1.1.1.16  root     6557: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     6558: {
1.1.1.16  root     6559:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     6560: }
                   6561: 
1.1.1.16  root     6562: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     6563: {
1.1.1.14  root     6564:        // clear the existing screen, not just the new one
                   6565:        int clr_height = max(height, scr_height);
                   6566:        
1.1.1.16  root     6567:        if(scr_width != width || scr_height != height) {
                   6568:                change_console_size(width, height);
1.1.1.14  root     6569:        }
                   6570:        mem[0x462] = 0;
                   6571:        *(UINT16 *)(mem + 0x44e) = 0;
                   6572:        
1.1.1.16  root     6573:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   6574:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   6575:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   6576:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1       root     6577:        
1.1.1.23  root     6578:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     6579:        if(clr_screen) {
1.1.1.14  root     6580:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   6581:                        mem[ofs++] = 0x20;
                   6582:                        mem[ofs++] = 0x07;
                   6583:                }
                   6584:                
1.1.1.35  root     6585: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6586:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6587: #endif
1.1.1.14  root     6588:                for(int y = 0; y < clr_height; y++) {
                   6589:                        for(int x = 0; x < scr_width; x++) {
                   6590:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6591:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     6592:                        }
                   6593:                }
                   6594:                SMALL_RECT rect;
1.1.1.14  root     6595:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   6596:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6597:                vram_length_char = vram_last_length_char = 0;
                   6598:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     6599: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6600:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6601: #endif
1.1       root     6602:        }
1.1.1.14  root     6603:        COORD co;
                   6604:        co.X = 0;
                   6605:        co.Y = scr_top;
                   6606:        SetConsoleCursorPosition(hStdout, co);
                   6607:        cursor_moved = true;
                   6608:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     6609: }
                   6610: 
1.1.1.36  root     6611: void pcbios_update_cursor_position()
                   6612: {
                   6613:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6614:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   6615:        if(!restore_console_on_exit) {
                   6616:                scr_top = csbi.srWindow.Top;
                   6617:        }
                   6618:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   6619:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   6620: }
                   6621: 
1.1.1.16  root     6622: inline void pcbios_int_10h_00h()
                   6623: {
                   6624:        switch(REG8(AL) & 0x7f) {
                   6625:        case 0x70: // v-text mode
                   6626:        case 0x71: // extended cga v-text mode
                   6627:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   6628:                break;
                   6629:        default:
                   6630:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   6631:                break;
                   6632:        }
                   6633:        if(REG8(AL) & 0x80) {
                   6634:                mem[0x487] |= 0x80;
                   6635:        } else {
                   6636:                mem[0x487] &= ~0x80;
                   6637:        }
                   6638:        mem[0x449] = REG8(AL) & 0x7f;
                   6639: }
                   6640: 
1.1       root     6641: inline void pcbios_int_10h_01h()
                   6642: {
1.1.1.13  root     6643:        mem[0x460] = REG8(CL);
                   6644:        mem[0x461] = REG8(CH);
1.1.1.14  root     6645:        
1.1.1.23  root     6646:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6647:        CONSOLE_CURSOR_INFO ci;
                   6648:        GetConsoleCursorInfo(hStdout, &ci);
                   6649: //     ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
                   6650: //     if(ci.bVisible) {
                   6651:                int lines = max(8, REG8(CL) + 1);
                   6652:                ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
                   6653: //     }
                   6654:        SetConsoleCursorInfo(hStdout, &ci);
1.1       root     6655: }
                   6656: 
                   6657: inline void pcbios_int_10h_02h()
                   6658: {
1.1.1.14  root     6659:        // continuously setting the cursor effectively stops it blinking
                   6660:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     6661:                COORD co;
                   6662:                co.X = REG8(DL);
1.1.1.14  root     6663:                co.Y = REG8(DH) + scr_top;
                   6664:                
                   6665:                // some programs hide the cursor by moving it off screen
                   6666:                static bool hidden = false;
1.1.1.23  root     6667:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6668:                CONSOLE_CURSOR_INFO ci;
                   6669:                GetConsoleCursorInfo(hStdout, &ci);
                   6670:                
                   6671:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   6672:                        if(ci.bVisible) {
                   6673:                                ci.bVisible = FALSE;
                   6674: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6675:                                hidden = true;
                   6676:                        }
                   6677:                } else if(hidden) {
                   6678:                        if(!ci.bVisible) {
                   6679:                                ci.bVisible = TRUE;
                   6680: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6681:                        }
                   6682:                        hidden = false;
                   6683:                }
1.1       root     6684:        }
1.1.1.14  root     6685:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   6686:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     6687: }
                   6688: 
                   6689: inline void pcbios_int_10h_03h()
                   6690: {
1.1.1.14  root     6691:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6692:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6693:        REG8(CL) = mem[0x460];
                   6694:        REG8(CH) = mem[0x461];
                   6695: }
                   6696: 
                   6697: inline void pcbios_int_10h_05h()
                   6698: {
1.1.1.14  root     6699:        if(REG8(AL) >= vram_pages) {
                   6700:                return;
                   6701:        }
                   6702:        if(mem[0x462] != REG8(AL)) {
                   6703:                vram_flush();
                   6704:                
1.1.1.23  root     6705:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6706:                SMALL_RECT rect;
1.1.1.14  root     6707:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6708:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6709:                
1.1.1.16  root     6710:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     6711:                        for(int x = 0; x < scr_width; x++) {
                   6712:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6713:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6714:                        }
                   6715:                }
1.1.1.16  root     6716:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     6717:                        for(int x = 0; x < scr_width; x++) {
                   6718:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   6719:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     6720:                        }
                   6721:                }
1.1.1.14  root     6722:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6723:                
                   6724:                COORD co;
1.1.1.14  root     6725:                co.X = mem[0x450 + REG8(AL) * 2];
                   6726:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   6727:                if(co.Y < scr_top + scr_height) {
                   6728:                        SetConsoleCursorPosition(hStdout, co);
                   6729:                }
1.1       root     6730:        }
1.1.1.14  root     6731:        mem[0x462] = REG8(AL);
                   6732:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   6733:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     6734:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     6735:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     6736:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     6737:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1       root     6738: }
                   6739: 
                   6740: inline void pcbios_int_10h_06h()
                   6741: {
1.1.1.14  root     6742:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6743:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6744:                return;
                   6745:        }
                   6746:        vram_flush();
                   6747:        
1.1.1.23  root     6748:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6749:        SMALL_RECT rect;
1.1.1.14  root     6750:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6751:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6752:        
                   6753:        int right = min(REG8(DL), scr_width - 1);
                   6754:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6755:        
                   6756:        if(REG8(AL) == 0) {
1.1.1.14  root     6757:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6758:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6759:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6760:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6761:                        }
                   6762:                }
                   6763:        } else {
1.1.1.14  root     6764:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     6765:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6766:                                if(y2 <= bottom) {
                   6767:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6768:                                } else {
1.1.1.14  root     6769:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6770:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6771:                                }
1.1.1.14  root     6772:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6773:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6774:                        }
                   6775:                }
                   6776:        }
1.1.1.14  root     6777:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6778: }
                   6779: 
                   6780: inline void pcbios_int_10h_07h()
                   6781: {
1.1.1.14  root     6782:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6783:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6784:                return;
                   6785:        }
                   6786:        vram_flush();
                   6787:        
1.1.1.23  root     6788:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6789:        SMALL_RECT rect;
1.1.1.14  root     6790:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6791:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6792:        
                   6793:        int right = min(REG8(DL), scr_width - 1);
                   6794:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6795:        
                   6796:        if(REG8(AL) == 0) {
1.1.1.14  root     6797:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6798:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6799:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6800:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6801:                        }
                   6802:                }
                   6803:        } else {
1.1.1.14  root     6804:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     6805:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6806:                                if(y2 >= REG8(CH)) {
                   6807:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6808:                                } else {
1.1.1.14  root     6809:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6810:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6811:                                }
1.1.1.14  root     6812:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6813:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6814:                        }
                   6815:                }
                   6816:        }
1.1.1.14  root     6817:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6818: }
                   6819: 
                   6820: inline void pcbios_int_10h_08h()
                   6821: {
                   6822:        COORD co;
                   6823:        DWORD num;
                   6824:        
1.1.1.14  root     6825:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6826:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6827:        
                   6828:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6829:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6830:                co.Y += scr_top;
                   6831:                vram_flush();
1.1       root     6832:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   6833:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   6834:                REG8(AL) = scr_char[0];
                   6835:                REG8(AH) = scr_attr[0];
                   6836:        } else {
1.1.1.16  root     6837:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     6838:        }
                   6839: }
                   6840: 
                   6841: inline void pcbios_int_10h_09h()
                   6842: {
                   6843:        COORD co;
                   6844:        
1.1.1.14  root     6845:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6846:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6847:        
1.1.1.16  root     6848:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   6849:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     6850:        
                   6851:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6852: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6853:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6854: #endif
1.1.1.16  root     6855:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6856:                while(dest < end) {
                   6857:                        write_text_vram_char(dest - vram, REG8(AL));
                   6858:                        mem[dest++] = REG8(AL);
                   6859:                        write_text_vram_attr(dest - vram, REG8(BL));
                   6860:                        mem[dest++] = REG8(BL);
1.1       root     6861:                }
1.1.1.35  root     6862: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6863:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6864: #endif
1.1       root     6865:        } else {
1.1.1.14  root     6866:                while(dest < end) {
1.1       root     6867:                        mem[dest++] = REG8(AL);
                   6868:                        mem[dest++] = REG8(BL);
                   6869:                }
                   6870:        }
                   6871: }
                   6872: 
                   6873: inline void pcbios_int_10h_0ah()
                   6874: {
                   6875:        COORD co;
                   6876:        
1.1.1.14  root     6877:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6878:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6879:        
1.1.1.16  root     6880:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   6881:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     6882:        
                   6883:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6884: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6885:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6886: #endif
1.1.1.16  root     6887:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6888:                while(dest < end) {
                   6889:                        write_text_vram_char(dest - vram, REG8(AL));
                   6890:                        mem[dest++] = REG8(AL);
                   6891:                        dest++;
1.1       root     6892:                }
1.1.1.35  root     6893: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6894:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6895: #endif
1.1       root     6896:        } else {
1.1.1.14  root     6897:                while(dest < end) {
1.1       root     6898:                        mem[dest++] = REG8(AL);
                   6899:                        dest++;
                   6900:                }
                   6901:        }
                   6902: }
                   6903: 
1.1.1.40  root     6904: HDC get_console_window_device_context()
                   6905: {
                   6906:        static HWND hwndFound = 0;
                   6907:        
                   6908:        if(hwndFound == 0) {
                   6909:                // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
                   6910:                char pszNewWindowTitle[1024];
                   6911:                char pszOldWindowTitle[1024];
                   6912:                
                   6913:                GetConsoleTitle(pszOldWindowTitle, 1024);
                   6914:                wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
                   6915:                SetConsoleTitle(pszNewWindowTitle);
                   6916:                Sleep(100);
                   6917:                hwndFound = FindWindow(NULL, pszNewWindowTitle);
                   6918:                SetConsoleTitle(pszOldWindowTitle);
                   6919:        }
                   6920:        return GetDC(hwndFound);
                   6921: }
                   6922: 
                   6923: inline void pcbios_int_10h_0ch()
                   6924: {
                   6925:        HDC hdc = get_console_window_device_context();
                   6926:        
                   6927:        if(hdc != NULL) {
                   6928:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
                   6929:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
                   6930:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
                   6931:                
                   6932:                if(REG8(AL) & 0x80) {
                   6933:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   6934:                        if(color != CLR_INVALID) {
                   6935:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
                   6936:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
                   6937:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
                   6938:                        }
                   6939:                }
                   6940:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
                   6941:        }
                   6942: }
                   6943: 
                   6944: inline void pcbios_int_10h_0dh()
                   6945: {
                   6946:        HDC hdc = get_console_window_device_context();
                   6947:        BYTE r = 0;
                   6948:        BYTE g = 0;
                   6949:        BYTE b = 0;
                   6950:        
                   6951:        if(hdc != NULL) {
                   6952:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   6953:                if(color != CLR_INVALID) {
                   6954:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
                   6955:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
                   6956:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
                   6957:                }
                   6958:        }
                   6959:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
                   6960: }
                   6961: 
1.1       root     6962: inline void pcbios_int_10h_0eh()
                   6963: {
1.1.1.14  root     6964:        DWORD num;
                   6965:        COORD co;
                   6966:        
                   6967:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6968:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6969:        
                   6970:        if(REG8(AL) == 7) {
                   6971:                //MessageBeep(-1);
                   6972:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   6973:                if(REG8(AL) == 10) {
                   6974:                        vram_flush();
                   6975:                }
1.1.1.23  root     6976:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     6977:                cursor_moved = true;
                   6978:        } else {
1.1.1.16  root     6979:                int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
1.1.1.14  root     6980:                if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6981: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6982:                        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6983: #endif
1.1.1.16  root     6984:                        int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6985:                        write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     6986: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6987:                        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6988: #endif
1.1.1.14  root     6989:                        
1.1.1.23  root     6990:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6991:                        if(++co.X == scr_width) {
                   6992:                                co.X = 0;
                   6993:                                if(++co.Y == scr_height) {
                   6994:                                        vram_flush();
                   6995:                                        WriteConsole(hStdout, "\n", 1, &num, NULL);
                   6996:                                        cursor_moved = true;
                   6997:                                }
                   6998:                        }
                   6999:                        if(!cursor_moved) {
                   7000:                                co.Y += scr_top;
                   7001:                                SetConsoleCursorPosition(hStdout, co);
                   7002:                                cursor_moved = true;
                   7003:                        }
                   7004:                }
                   7005:                mem[dest] = REG8(AL);
                   7006:        }
1.1       root     7007: }
                   7008: 
                   7009: inline void pcbios_int_10h_0fh()
                   7010: {
                   7011:        REG8(AL) = mem[0x449];
                   7012:        REG8(AH) = mem[0x44a];
                   7013:        REG8(BH) = mem[0x462];
                   7014: }
                   7015: 
1.1.1.14  root     7016: inline void pcbios_int_10h_11h()
                   7017: {
                   7018:        switch(REG8(AL)) {
1.1.1.16  root     7019:        case 0x01:
1.1.1.14  root     7020:        case 0x11:
1.1.1.16  root     7021:                pcbios_set_console_size(80, 28, true);
1.1.1.14  root     7022:                break;
1.1.1.16  root     7023:        case 0x02:
1.1.1.14  root     7024:        case 0x12:
1.1.1.16  root     7025:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     7026:                break;
1.1.1.16  root     7027:        case 0x04:
1.1.1.14  root     7028:        case 0x14:
1.1.1.16  root     7029:                pcbios_set_console_size(80, 25, true);
                   7030:                break;
                   7031:        case 0x18:
                   7032:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     7033:                break;
                   7034:        case 0x30:
                   7035:                SREG(ES) = 0;
                   7036:                i386_load_segment_descriptor(ES);
                   7037:                REG16(BP) = 0;
                   7038:                REG16(CX) = mem[0x485];
                   7039:                REG8(DL) = mem[0x484];
                   7040:                break;
                   7041:        }
                   7042: }
                   7043: 
                   7044: inline void pcbios_int_10h_12h()
                   7045: {
1.1.1.16  root     7046:        switch(REG8(BL)) {
                   7047:        case 0x10:
1.1.1.14  root     7048:                REG16(BX) = 0x0003;
                   7049:                REG16(CX) = 0x0009;
1.1.1.16  root     7050:                break;
1.1.1.14  root     7051:        }
                   7052: }
                   7053: 
1.1       root     7054: inline void pcbios_int_10h_13h()
                   7055: {
1.1.1.3   root     7056:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     7057:        COORD co;
                   7058:        DWORD num;
                   7059:        
                   7060:        co.X = REG8(DL);
1.1.1.14  root     7061:        co.Y = REG8(DH) + scr_top;
                   7062:        
                   7063:        vram_flush();
1.1       root     7064:        
                   7065:        switch(REG8(AL)) {
                   7066:        case 0x00:
                   7067:        case 0x01:
                   7068:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7069:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7070:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7071:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7072:                        SetConsoleCursorPosition(hStdout, co);
                   7073:                        
                   7074:                        if(csbi.wAttributes != REG8(BL)) {
                   7075:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   7076:                        }
1.1.1.14  root     7077:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   7078:                        
1.1       root     7079:                        if(csbi.wAttributes != REG8(BL)) {
                   7080:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7081:                        }
                   7082:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     7083:                                if(!restore_console_on_exit) {
                   7084:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7085:                                        scr_top = csbi.srWindow.Top;
                   7086:                                }
1.1.1.14  root     7087:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7088:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7089:                                SetConsoleCursorPosition(hStdout, co);
                   7090:                        } else {
                   7091:                                cursor_moved = true;
                   7092:                        }
                   7093:                } else {
1.1.1.3   root     7094:                        m_CF = 1;
1.1       root     7095:                }
                   7096:                break;
                   7097:        case 0x02:
                   7098:        case 0x03:
                   7099:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7100:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7101:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7102:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7103:                        SetConsoleCursorPosition(hStdout, co);
                   7104:                        
                   7105:                        WORD wAttributes = csbi.wAttributes;
                   7106:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7107:                                if(wAttributes != mem[ofs + 1]) {
                   7108:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7109:                                        wAttributes = mem[ofs + 1];
                   7110:                                }
1.1.1.14  root     7111:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7112:                        }
                   7113:                        if(csbi.wAttributes != wAttributes) {
                   7114:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7115:                        }
                   7116:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7117:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7118:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7119:                                SetConsoleCursorPosition(hStdout, co);
                   7120:                        } else {
                   7121:                                cursor_moved = true;
                   7122:                        }
                   7123:                } else {
1.1.1.3   root     7124:                        m_CF = 1;
1.1       root     7125:                }
                   7126:                break;
                   7127:        case 0x10:
                   7128:        case 0x11:
                   7129:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7130:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7131:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   7132:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7133:                        for(int i = 0; i < num; i++) {
                   7134:                                mem[ofs++] = scr_char[i];
                   7135:                                mem[ofs++] = scr_attr[i];
                   7136:                                if(REG8(AL) == 0x11) {
                   7137:                                        mem[ofs++] = 0;
                   7138:                                        mem[ofs++] = 0;
                   7139:                                }
                   7140:                        }
                   7141:                } else {
1.1.1.16  root     7142:                        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     7143:                                mem[ofs++] = mem[src++];
                   7144:                                mem[ofs++] = mem[src++];
                   7145:                                if(REG8(AL) == 0x11) {
                   7146:                                        mem[ofs++] = 0;
                   7147:                                        mem[ofs++] = 0;
                   7148:                                }
1.1.1.14  root     7149:                                if(++co.X == scr_width) {
                   7150:                                        if(++co.Y == scr_height) {
1.1       root     7151:                                                break;
                   7152:                                        }
                   7153:                                        co.X = 0;
                   7154:                                }
                   7155:                        }
                   7156:                }
                   7157:                break;
                   7158:        case 0x20:
                   7159:        case 0x21:
                   7160:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7161:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7162:                        int len = min(REG16(CX), scr_width * scr_height);
                   7163:                        for(int i = 0; i < len; i++) {
1.1       root     7164:                                scr_char[i] = mem[ofs++];
                   7165:                                scr_attr[i] = mem[ofs++];
                   7166:                                if(REG8(AL) == 0x21) {
                   7167:                                        ofs += 2;
                   7168:                                }
                   7169:                        }
1.1.1.14  root     7170:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7171:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7172:                } else {
1.1.1.16  root     7173:                        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     7174:                                mem[dest++] = mem[ofs++];
                   7175:                                mem[dest++] = mem[ofs++];
                   7176:                                if(REG8(AL) == 0x21) {
                   7177:                                        ofs += 2;
                   7178:                                }
1.1.1.14  root     7179:                                if(++co.X == scr_width) {
                   7180:                                        if(++co.Y == scr_height) {
1.1       root     7181:                                                break;
                   7182:                                        }
                   7183:                                        co.X = 0;
                   7184:                                }
                   7185:                        }
                   7186:                }
                   7187:                break;
                   7188:        default:
1.1.1.22  root     7189:                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     7190:                m_CF = 1;
1.1       root     7191:                break;
                   7192:        }
                   7193: }
                   7194: 
1.1.1.30  root     7195: inline void pcbios_int_10h_18h()
                   7196: {
                   7197:        switch(REG8(AL)) {
                   7198:        case 0x00:
                   7199:        case 0x01:
                   7200: //             REG8(AL) = 0x86;
                   7201:                REG8(AL) = 0x00;
                   7202:                break;
                   7203:        default:
                   7204:                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));
                   7205:                m_CF = 1;
                   7206:                break;
                   7207:        }
                   7208: }
                   7209: 
1.1.1.14  root     7210: inline void pcbios_int_10h_1ah()
                   7211: {
                   7212:        switch(REG8(AL)) {
                   7213:        case 0x00:
                   7214:                REG8(AL) = 0x1a;
                   7215:                REG8(BL) = 0x08;
                   7216:                REG8(BH) = 0x00;
                   7217:                break;
                   7218:        default:
1.1.1.22  root     7219:                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     7220:                m_CF = 1;
                   7221:                break;
                   7222:        }
                   7223: }
                   7224: 
1.1       root     7225: inline void pcbios_int_10h_1dh()
                   7226: {
                   7227:        switch(REG8(AL)) {
1.1.1.43  root     7228:        case 0x00:
                   7229:                // DOS/V Shift Status Line Control is not supported
                   7230:                m_CF = 1;
                   7231:                break;
1.1       root     7232:        case 0x01:
                   7233:                break;
                   7234:        case 0x02:
                   7235:                REG16(BX) = 0;
                   7236:                break;
                   7237:        default:
1.1.1.22  root     7238:                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));
                   7239:                m_CF = 1;
                   7240:                break;
                   7241:        }
                   7242: }
                   7243: 
                   7244: inline void pcbios_int_10h_4fh()
                   7245: {
                   7246:        switch(REG8(AL)) {
                   7247:        case 0x00:
                   7248:                REG8(AH) = 0x02; // not supported
                   7249:                break;
                   7250:        case 0x01:
                   7251:        case 0x02:
                   7252:        case 0x03:
                   7253:        case 0x04:
                   7254:        case 0x05:
                   7255:        case 0x06:
                   7256:        case 0x07:
                   7257:        case 0x08:
                   7258:        case 0x09:
                   7259:        case 0x0a:
                   7260:        case 0x0b:
                   7261:        case 0x0c:
                   7262:                REG8(AH) = 0x01; // failed
                   7263:                break;
                   7264:        default:
                   7265:                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     7266:                m_CF = 1;
1.1       root     7267:                break;
                   7268:        }
                   7269: }
                   7270: 
                   7271: inline void pcbios_int_10h_82h()
                   7272: {
                   7273:        static UINT8 mode = 0;
                   7274:        
                   7275:        switch(REG8(AL)) {
1.1.1.22  root     7276:        case 0x00:
1.1       root     7277:                if(REG8(BL) != 0xff) {
                   7278:                        mode = REG8(BL);
                   7279:                }
                   7280:                REG8(AL) = mode;
                   7281:                break;
                   7282:        default:
1.1.1.22  root     7283:                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     7284:                m_CF = 1;
1.1       root     7285:                break;
                   7286:        }
                   7287: }
                   7288: 
1.1.1.22  root     7289: inline void pcbios_int_10h_83h()
                   7290: {
                   7291:        static UINT8 mode = 0;
                   7292:        
                   7293:        switch(REG8(AL)) {
                   7294:        case 0x00:
                   7295:                REG16(AX) = 0; // offset???
                   7296:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7297:                i386_load_segment_descriptor(ES);
                   7298:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7299:                break;
                   7300:        default:
                   7301:                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));
                   7302:                m_CF = 1;
                   7303:                break;
                   7304:        }
                   7305: }
                   7306: 
                   7307: inline void pcbios_int_10h_90h()
                   7308: {
                   7309:        REG8(AL) = mem[0x449];
                   7310: }
                   7311: 
                   7312: inline void pcbios_int_10h_91h()
                   7313: {
                   7314:        REG8(AL) = 0x04; // VGA
                   7315: }
                   7316: 
                   7317: inline void pcbios_int_10h_efh()
                   7318: {
                   7319:        REG16(DX) = 0xffff;
                   7320: }
                   7321: 
1.1       root     7322: inline void pcbios_int_10h_feh()
                   7323: {
                   7324:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7325:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7326:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7327:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7328:        }
                   7329:        int_10h_feh_called = true;
                   7330: }
                   7331: 
                   7332: inline void pcbios_int_10h_ffh()
                   7333: {
                   7334:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7335:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7336:                COORD co;
                   7337:                DWORD num;
                   7338:                
1.1.1.14  root     7339:                vram_flush();
                   7340:                
                   7341:                co.X = (REG16(DI) >> 1) % scr_width;
                   7342:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7343:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7344:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7345:                int len;
                   7346:                for(len = 0; ofs < end; len++) {
                   7347:                        scr_char[len] = mem[ofs++];
                   7348:                        scr_attr[len] = mem[ofs++];
                   7349:                }
                   7350:                co.Y += scr_top;
                   7351:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7352:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7353:        }
                   7354:        int_10h_ffh_called = true;
                   7355: }
                   7356: 
1.1.1.42  root     7357: int pcbios_update_drive_param(int drive_num, int force_update)
                   7358: {
                   7359:        if(drive_num >= 0 && drive_num < 26) {
                   7360:                drive_param_t *drive_param = &drive_params[drive_num];
                   7361:                
                   7362:                if(force_update || !drive_param->initialized) {
                   7363:                        drive_param->valid = 0;
                   7364:                        char dev[64];
                   7365:                        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7366:                        
                   7367:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   7368:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7369:                                DWORD dwSize;
                   7370:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
                   7371:                                        drive_param->valid = 1;
                   7372:                                }
                   7373:                                CloseHandle(hFile);
                   7374:                        }
                   7375:                        drive_param->initialized = 1;
                   7376:                }
                   7377:                return(drive_param->valid);
                   7378:        }
                   7379:        return(0);
                   7380: }
                   7381: 
                   7382: inline void pcbios_int_13h_00h()
                   7383: {
                   7384:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7385:        
                   7386:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7387:                REG8(AH) = 0x00; // successful completion
                   7388:        } else {
                   7389:                if(REG8(DL) & 0x80) {
                   7390:                        REG8(AH) = 0x05; // reset failed (hard disk)
                   7391:                } else {
                   7392:                        REG8(AH) = 0x80; // timeout (not ready)
                   7393:                }
                   7394:                m_CF = 1;
                   7395:        }
                   7396: }
                   7397: 
                   7398: inline void pcbios_int_13h_02h()
                   7399: {
                   7400:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7401:        
                   7402:        if(REG8(AL) == 0) {
                   7403:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7404:                m_CF = 1;
                   7405:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7406:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7407:                m_CF = 1;
                   7408:        } else {
                   7409:                drive_param_t *drive_param = &drive_params[drive_num];
                   7410:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7411:                char dev[64];
                   7412:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7413:                
                   7414:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7415:                if(hFile == INVALID_HANDLE_VALUE) {
                   7416:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7417:                        m_CF = 1;
                   7418:                } else {
                   7419:                        UINT32 sector_num = REG8(AL);
                   7420:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7421:                        UINT32 head = REG8(DH);
                   7422:                        UINT32 sector = REG8(CL) & 0x3f;
                   7423:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7424:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7425:                        DWORD dwSize;
                   7426:                        
                   7427: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7428: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7429: //                             m_CF = 1;
                   7430: //                     } else 
                   7431:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7432:                                REG8(AH) = 0x04; // sector not found/read error
                   7433:                                m_CF = 1;
                   7434:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7435:                                REG8(AH) = 0x04; // sector not found/read error
                   7436:                                m_CF = 1;
                   7437:                        } else {
                   7438:                                REG8(AH) = 0x00; // successful completion
                   7439:                        }
                   7440:                        CloseHandle(hFile);
                   7441:                }
                   7442:        }
                   7443: }
                   7444: 
                   7445: inline void pcbios_int_13h_03h()
                   7446: {
                   7447:        // this operation may cause serious damage for drives, so support only floppy disk...
                   7448:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7449:        
                   7450:        if(REG8(AL) == 0) {
                   7451:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7452:                m_CF = 1;
                   7453:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7454:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7455:                m_CF = 1;
                   7456:        } else if(!drive_params[drive_num].is_fdd()) {
                   7457:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7458:                m_CF = 1;
                   7459:        } else {
                   7460:                drive_param_t *drive_param = &drive_params[drive_num];
                   7461:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7462:                char dev[64];
                   7463:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7464:                
                   7465:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7466:                if(hFile == INVALID_HANDLE_VALUE) {
                   7467:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7468:                        m_CF = 1;
                   7469:                } else {
                   7470:                        UINT32 sector_num = REG8(AL);
                   7471:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7472:                        UINT32 head = REG8(DH);
                   7473:                        UINT32 sector = REG8(CL) & 0x3f;
                   7474:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7475:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7476:                        DWORD dwSize;
                   7477:                        
                   7478: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7479: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7480: //                             m_CF = 1;
                   7481: //                     } else 
                   7482:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7483:                                REG8(AH) = 0x04; // sector not found/read error
                   7484:                                m_CF = 1;
                   7485:                        } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7486:                                REG8(AH) = 0x04; // sector not found/read error
                   7487:                                m_CF = 1;
                   7488:                        } else {
                   7489:                                REG8(AH) = 0x00; // successful completion
                   7490:                        }
                   7491:                        CloseHandle(hFile);
                   7492:                }
                   7493:        }
                   7494: }
                   7495: 
                   7496: inline void pcbios_int_13h_04h()
                   7497: {
                   7498:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7499:        
                   7500:        if(REG8(AL) == 0) {
                   7501:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7502:                m_CF = 1;
                   7503:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7504:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7505:                m_CF = 1;
                   7506:        } else {
                   7507:                drive_param_t *drive_param = &drive_params[drive_num];
                   7508:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7509:                char dev[64];
                   7510:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7511:                
                   7512:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7513:                if(hFile == INVALID_HANDLE_VALUE) {
                   7514:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7515:                        m_CF = 1;
                   7516:                } else {
                   7517:                        UINT32 sector_num = REG8(AL);
                   7518:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7519:                        UINT32 head = REG8(DH);
                   7520:                        UINT32 sector = REG8(CL) & 0x3f;
                   7521:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7522:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7523:                        DWORD dwSize;
                   7524:                        UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
                   7525:                        
                   7526: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7527: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7528: //                             m_CF = 1;
                   7529: //                     } else 
                   7530:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7531:                                REG8(AH) = 0x04; // sector not found/read error
                   7532:                                m_CF = 1;
                   7533:                        } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7534:                                REG8(AH) = 0x04; // sector not found/read error
                   7535:                                m_CF = 1;
                   7536:                        } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
                   7537:                                REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
                   7538:                                m_CF = 1;
                   7539:                        } else {
                   7540:                                REG8(AH) = 0x00; // successful completion
                   7541:                        }
                   7542:                        free(tmp_buffer);
                   7543:                        CloseHandle(hFile);
                   7544:                }
                   7545:        }
                   7546: }
                   7547: 
                   7548: inline void pcbios_int_13h_08h()
                   7549: {
                   7550:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7551:        
                   7552:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7553:                drive_param_t *drive_param = &drive_params[drive_num];
                   7554:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7555:                
                   7556:                REG16(AX) = 0x0000;
                   7557:                switch(geo->MediaType) {
                   7558:                case F5_360_512:
                   7559:                case F5_320_512:
                   7560:                case F5_320_1024:
                   7561:                case F5_180_512:
                   7562:                case F5_160_512:
                   7563:                        REG8(BL) = 0x01; // 320K/360K disk
                   7564:                        break;
                   7565:                case F5_1Pt2_512:
                   7566:                case F3_1Pt2_512:
                   7567:                case F3_1Pt23_1024:
                   7568:                case F5_1Pt23_1024:
                   7569:                        REG8(BL) = 0x02; // 1.2M disk
                   7570:                        break;
                   7571:                case F3_720_512:
                   7572:                case F3_640_512:
                   7573:                case F5_640_512:
                   7574:                case F5_720_512:
                   7575:                        REG8(BL) = 0x03; // 720K disk
                   7576:                        break;
                   7577:                case F3_1Pt44_512:
                   7578:                        REG8(BL) = 0x04; // 1.44M disk
                   7579:                        break;
                   7580:                case F3_2Pt88_512:
                   7581:                        REG8(BL) = 0x06; // 2.88M disk
                   7582:                        break;
                   7583:                case RemovableMedia:
                   7584:                        REG8(BL) = 0x10; // ATAPI Removable Media Device
                   7585:                        break;
                   7586:                default:
                   7587:                        REG8(BL) = 0x00; // unknown
                   7588:                        break;
                   7589:                }
                   7590:                if(REG8(DL) & 0x80) {
                   7591:                        switch(GetLogicalDrives() & 0x0c) {
                   7592:                        case 0x00: REG8(DL) = 0x00; break;
                   7593:                        case 0x04:
                   7594:                        case 0x08: REG8(DL) = 0x01; break;
                   7595:                        case 0x0c: REG8(DL) = 0x02; break;
                   7596:                        }
                   7597:                } else {
                   7598:                        switch(GetLogicalDrives() & 0x03) {
                   7599:                        case 0x00: REG8(DL) = 0x00; break;
                   7600:                        case 0x01:
                   7601:                        case 0x02: REG8(DL) = 0x01; break;
                   7602:                        case 0x03: REG8(DL) = 0x02; break;
                   7603:                        }
                   7604:                }
                   7605:                REG8(DH) = drive_param->head_num();
                   7606:                int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
                   7607:                int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
                   7608:                REG8(CH) = cyl & 0xff;
                   7609:                REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
                   7610:        } else {
                   7611:                REG8(AH) = 0x07;
                   7612:                m_CF = 1;
                   7613:        }
                   7614: }
                   7615: 
                   7616: inline void pcbios_int_13h_10h()
                   7617: {
                   7618:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7619:        
                   7620:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7621:                REG8(AH) = 0x00; // successful completion
                   7622:        } else {
                   7623:                if(REG8(DL) & 0x80) {
                   7624:                        REG8(AH) = 0xaa; // drive not ready (hard disk)
                   7625:                } else {
                   7626:                        REG8(AH) = 0x80; // timeout (not ready)
                   7627:                }
                   7628:                m_CF = 1;
                   7629:        }
                   7630: }
                   7631: 
                   7632: inline void pcbios_int_13h_15h()
                   7633: {
                   7634:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7635:        
                   7636:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7637:                if(REG8(DL) & 0x80) {
                   7638:                        REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
                   7639:                } else {
                   7640:                        REG8(AH) = 0x03; // hard disk
                   7641:                }
                   7642:        } else {
                   7643:                REG8(AH) = 0x00; // no such drive
                   7644:        }
                   7645: }
                   7646: 
1.1.1.43  root     7647: inline void pcbios_int_13h_41h()
                   7648: {
                   7649:        if(REG16(BX) == 0x55aa) {
                   7650:                // IBM/MS INT 13 Extensions is not installed
                   7651:                REG8(AH) = 0x01;
                   7652:                m_CF = 1;
                   7653:        } else {
                   7654:                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));
                   7655:                REG8(AH) = 0x01;
                   7656:                m_CF = 1;
                   7657:        }
                   7658: }
                   7659: 
1.1.1.25  root     7660: inline void pcbios_int_14h_00h()
                   7661: {
1.1.1.29  root     7662:        if(REG16(DX) < 4) {
1.1.1.25  root     7663:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   7664:                UINT8 selector = sio_read(REG16(DX), 3);
                   7665:                selector &= ~0x3f;
                   7666:                selector |= REG8(AL) & 0x1f;
                   7667:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   7668:                sio_write(REG16(DX), 3, selector | 0x80);
                   7669:                sio_write(REG16(DX), 0, divisor & 0xff);
                   7670:                sio_write(REG16(DX), 1, divisor >> 8);
                   7671:                sio_write(REG16(DX), 3, selector);
                   7672:                REG8(AH) = sio_read(REG16(DX), 5);
                   7673:                REG8(AL) = sio_read(REG16(DX), 6);
                   7674:        } else {
                   7675:                REG8(AH) = 0x80;
                   7676:        }
                   7677: }
                   7678: 
                   7679: inline void pcbios_int_14h_01h()
                   7680: {
1.1.1.29  root     7681:        if(REG16(DX) < 4) {
1.1.1.25  root     7682:                UINT8 selector = sio_read(REG16(DX), 3);
                   7683:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7684:                sio_write(REG16(DX), 0, REG8(AL));
                   7685:                sio_write(REG16(DX), 3, selector);
                   7686:                REG8(AH) = sio_read(REG16(DX), 5);
                   7687:        } else {
                   7688:                REG8(AH) = 0x80;
                   7689:        }
                   7690: }
                   7691: 
                   7692: inline void pcbios_int_14h_02h()
                   7693: {
1.1.1.29  root     7694:        if(REG16(DX) < 4) {
1.1.1.25  root     7695:                UINT8 selector = sio_read(REG16(DX), 3);
                   7696:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7697:                REG8(AL) = sio_read(REG16(DX), 0);
                   7698:                sio_write(REG16(DX), 3, selector);
                   7699:                REG8(AH) = sio_read(REG16(DX), 5);
                   7700:        } else {
                   7701:                REG8(AH) = 0x80;
                   7702:        }
                   7703: }
                   7704: 
                   7705: inline void pcbios_int_14h_03h()
                   7706: {
1.1.1.29  root     7707:        if(REG16(DX) < 4) {
1.1.1.25  root     7708:                REG8(AH) = sio_read(REG16(DX), 5);
                   7709:                REG8(AL) = sio_read(REG16(DX), 6);
                   7710:        } else {
                   7711:                REG8(AH) = 0x80;
                   7712:        }
                   7713: }
                   7714: 
                   7715: inline void pcbios_int_14h_04h()
                   7716: {
1.1.1.29  root     7717:        if(REG16(DX) < 4) {
1.1.1.25  root     7718:                UINT8 selector = sio_read(REG16(DX), 3);
                   7719:                if(REG8(CH) <= 0x03) {
                   7720:                        selector = (selector & ~0x03) | REG8(CH);
                   7721:                }
                   7722:                if(REG8(BL) == 0x00) {
                   7723:                        selector &= ~0x04;
                   7724:                } else if(REG8(BL) == 0x01) {
                   7725:                        selector |= 0x04;
                   7726:                }
                   7727:                if(REG8(BH) == 0x00) {
                   7728:                        selector = (selector & ~0x38) | 0x00;
                   7729:                } else if(REG8(BH) == 0x01) {
                   7730:                        selector = (selector & ~0x38) | 0x08;
                   7731:                } else if(REG8(BH) == 0x02) {
                   7732:                        selector = (selector & ~0x38) | 0x18;
                   7733:                } else if(REG8(BH) == 0x03) {
                   7734:                        selector = (selector & ~0x38) | 0x28;
                   7735:                } else if(REG8(BH) == 0x04) {
                   7736:                        selector = (selector & ~0x38) | 0x38;
                   7737:                }
                   7738:                if(REG8(AL) == 0x00) {
                   7739:                        selector |= 0x40;
                   7740:                } else if(REG8(AL) == 0x01) {
                   7741:                        selector &= ~0x40;
                   7742:                }
                   7743:                if(REG8(CL) <= 0x0b) {
                   7744:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   7745:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   7746:                        sio_write(REG16(DX), 3, selector | 0x80);
                   7747:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   7748:                        sio_write(REG16(DX), 1, divisor >> 8);
                   7749:                }
                   7750:                sio_write(REG16(DX), 3, selector);
                   7751:                REG8(AH) = sio_read(REG16(DX), 5);
                   7752:                REG8(AL) = sio_read(REG16(DX), 6);
                   7753:        } else {
                   7754:                REG8(AH) = 0x80;
                   7755:        }
                   7756: }
                   7757: 
                   7758: inline void pcbios_int_14h_05h()
                   7759: {
1.1.1.29  root     7760:        if(REG16(DX) < 4) {
1.1.1.25  root     7761:                if(REG8(AL) == 0x00) {
                   7762:                        REG8(BL) = sio_read(REG16(DX), 4);
                   7763:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7764:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7765:                } else if(REG8(AL) == 0x01) {
                   7766:                        sio_write(REG16(DX), 4, REG8(BL));
                   7767:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7768:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7769:                } else {
                   7770:                        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));
                   7771:                }
                   7772:        } else {
                   7773:                REG8(AH) = 0x80;
                   7774:        }
                   7775: }
                   7776: 
1.1.1.14  root     7777: inline void pcbios_int_15h_10h()
                   7778: {
1.1.1.22  root     7779:        switch(REG8(AL)) {
                   7780:        case 0x00:
1.1.1.14  root     7781:                Sleep(10);
1.1.1.35  root     7782:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     7783:                break;
                   7784:        default:
                   7785:                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     7786:                REG8(AH) = 0x86;
                   7787:                m_CF = 1;
                   7788:        }
                   7789: }
                   7790: 
1.1       root     7791: inline void pcbios_int_15h_23h()
                   7792: {
                   7793:        switch(REG8(AL)) {
1.1.1.22  root     7794:        case 0x00:
1.1.1.8   root     7795:                REG8(CL) = cmos_read(0x2d);
                   7796:                REG8(CH) = cmos_read(0x2e);
1.1       root     7797:                break;
1.1.1.22  root     7798:        case 0x01:
1.1.1.8   root     7799:                cmos_write(0x2d, REG8(CL));
                   7800:                cmos_write(0x2e, REG8(CH));
1.1       root     7801:                break;
                   7802:        default:
1.1.1.22  root     7803:                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     7804:                REG8(AH) = 0x86;
1.1.1.3   root     7805:                m_CF = 1;
1.1       root     7806:                break;
                   7807:        }
                   7808: }
                   7809: 
                   7810: inline void pcbios_int_15h_24h()
                   7811: {
                   7812:        switch(REG8(AL)) {
1.1.1.22  root     7813:        case 0x00:
1.1.1.3   root     7814:                i386_set_a20_line(0);
1.1       root     7815:                REG8(AH) = 0;
                   7816:                break;
1.1.1.22  root     7817:        case 0x01:
1.1.1.3   root     7818:                i386_set_a20_line(1);
1.1       root     7819:                REG8(AH) = 0;
                   7820:                break;
1.1.1.22  root     7821:        case 0x02:
1.1       root     7822:                REG8(AH) = 0;
1.1.1.3   root     7823:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     7824:                REG16(CX) = 0;
                   7825:                break;
1.1.1.22  root     7826:        case 0x03:
1.1       root     7827:                REG16(AX) = 0;
                   7828:                REG16(BX) = 0;
                   7829:                break;
1.1.1.22  root     7830:        default:
                   7831:                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));
                   7832:                REG8(AH) = 0x86;
                   7833:                m_CF = 1;
                   7834:                break;
1.1       root     7835:        }
                   7836: }
                   7837: 
                   7838: inline void pcbios_int_15h_49h()
                   7839: {
1.1.1.27  root     7840:        REG8(AH) = 0x00;
                   7841:        REG8(BL) = 0x00; // DOS/V
1.1       root     7842: }
                   7843: 
1.1.1.22  root     7844: inline void pcbios_int_15h_50h()
                   7845: {
                   7846:        switch(REG8(AL)) {
                   7847:        case 0x00:
                   7848:        case 0x01:
                   7849:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   7850:                        REG8(AH) = 0x01; // invalid font type in bh
                   7851:                        m_CF = 1;
1.1.1.27  root     7852:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     7853:                        REG8(AH) = 0x02; // bl not zero
                   7854:                        m_CF = 1;
                   7855:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   7856:                        REG8(AH) = 0x04; // invalid code page
                   7857:                        m_CF = 1;
1.1.1.27  root     7858:                } else if(REG8(AL) == 0x01) {
                   7859:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     7860:                        m_CF = 1;
1.1.1.27  root     7861:                } else {
                   7862:                        // dummy font read routine is at fffd:000d
                   7863:                        SREG(ES) = 0xfffd;
                   7864:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     7865:                        REG16(BX) = 0x000d;
1.1.1.27  root     7866:                        REG8(AH) = 0x00; // success
1.1.1.22  root     7867:                }
                   7868:                break;
                   7869:        default:
                   7870:                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));
                   7871:                REG8(AH) = 0x86;
                   7872:                m_CF = 1;
                   7873:                break;
                   7874:        }
                   7875: }
                   7876: 
1.1.1.30  root     7877: inline void pcbios_int_15h_53h()
                   7878: {
                   7879:        switch(REG8(AL)) {
                   7880:        case 0x00:
                   7881:                // APM is not installed
                   7882:                REG8(AH) = 0x86;
                   7883:                m_CF = 1;
                   7884:                break;
                   7885:        default:
                   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));
                   7887:                REG8(AH) = 0x86;
                   7888:                m_CF = 1;
                   7889:                break;
                   7890:        }
                   7891: }
                   7892: 
1.1.1.43  root     7893: inline void pcbios_int_15h_84h()
                   7894: {
                   7895:        // joystick support (from DOSBox)
                   7896:        switch(REG16(DX)) {
                   7897:        case 0x00:
                   7898:                REG16(AX) = 0x00f0;
                   7899:                REG16(DX) = 0x0201;
                   7900:                m_CF = 1;
                   7901:                break;
                   7902:        case 0x01:
                   7903:                REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   7904:                m_CF = 1;
                   7905:                break;
                   7906:        default:
                   7907:                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));
                   7908:                REG8(AH) = 0x86;
                   7909:                m_CF = 1;
                   7910:                break;
                   7911:        }
                   7912: }
1.1.1.35  root     7913: 
                   7914: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     7915: {
                   7916:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     7917:        UINT32 msec = usec / 1000;
                   7918:        
1.1.1.35  root     7919:        while(msec && !m_halted) {
1.1.1.14  root     7920:                UINT32 tmp = min(msec, 100);
                   7921:                if(msec - tmp < 10) {
                   7922:                        tmp = msec;
                   7923:                }
                   7924:                Sleep(tmp);
                   7925:                msec -= tmp;
                   7926:        }
1.1.1.35  root     7927:        
                   7928: #ifdef USE_SERVICE_THREAD
                   7929:        service_exit = true;
                   7930: #endif
                   7931:        return(0);
                   7932: }
                   7933: 
                   7934: inline void pcbios_int_15h_86h()
                   7935: {
                   7936:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   7937: #ifdef USE_SERVICE_THREAD
                   7938:                start_service_loop(pcbios_int_15h_86h_thread);
                   7939: #else
                   7940:                pcbios_int_15h_86h_thread(NULL);
                   7941:                REQUEST_HARDWRE_UPDATE();
                   7942: #endif
                   7943:        }
1.1       root     7944: }
                   7945: 
                   7946: inline void pcbios_int_15h_87h()
                   7947: {
                   7948:        // copy extended memory (from DOSBox)
                   7949:        int len = REG16(CX) * 2;
1.1.1.3   root     7950:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     7951:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   7952:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   7953:        memcpy(mem + dst, mem + src, len);
                   7954:        REG16(AX) = 0x00;
                   7955: }
                   7956: 
                   7957: inline void pcbios_int_15h_88h()
                   7958: {
1.1.1.17  root     7959:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     7960: }
                   7961: 
                   7962: inline void pcbios_int_15h_89h()
                   7963: {
1.1.1.21  root     7964: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     7965:        // switch to protected mode (from DOSBox)
                   7966:        write_io_byte(0x20, 0x10);
                   7967:        write_io_byte(0x21, REG8(BH));
                   7968:        write_io_byte(0x21, 0x00);
                   7969:        write_io_byte(0xa0, 0x10);
                   7970:        write_io_byte(0xa1, REG8(BL));
                   7971:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     7972:        i386_set_a20_line(1);
                   7973:        int ofs = SREG_BASE(ES) + REG16(SI);
                   7974:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   7975:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   7976:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   7977:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   7978: #if defined(HAS_I386)
                   7979:        m_cr[0] |= 1;
                   7980: #else
                   7981:        m_msw |= 1;
                   7982: #endif
                   7983:        SREG(DS) = 0x18;
                   7984:        SREG(ES) = 0x20;
                   7985:        SREG(SS) = 0x28;
                   7986:        i386_load_segment_descriptor(DS);
                   7987:        i386_load_segment_descriptor(ES);
                   7988:        i386_load_segment_descriptor(SS);
1.1.1.21  root     7989:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     7990:        REG16(SP) += 6;
1.1.1.3   root     7991: #if defined(HAS_I386)
1.1.1.21  root     7992:        UINT32 flags = get_flags();
                   7993:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   7994:        set_flags(flags);
1.1.1.3   root     7995: #else
1.1.1.21  root     7996:        UINT32 flags = CompressFlags();
                   7997:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   7998:        ExpandFlags(flags);
1.1.1.3   root     7999: #endif
1.1       root     8000:        REG16(AX) = 0x00;
1.1.1.21  root     8001:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     8002: #else
1.1.1.21  root     8003:        // i86/i186/v30: protected mode is not supported
1.1       root     8004:        REG8(AH) = 0x86;
1.1.1.3   root     8005:        m_CF = 1;
1.1       root     8006: #endif
                   8007: }
                   8008: 
1.1.1.21  root     8009: inline void pcbios_int_15h_8ah()
                   8010: {
                   8011:        UINT32 size = MAX_MEM - 0x100000;
                   8012:        REG16(AX) = size & 0xffff;
                   8013:        REG16(DX) = size >> 16;
                   8014: }
                   8015: 
1.1.1.3   root     8016: #if defined(HAS_I386)
1.1       root     8017: inline void pcbios_int_15h_c9h()
                   8018: {
                   8019:        REG8(AH) = 0x00;
                   8020:        REG8(CH) = cpu_type;
                   8021:        REG8(CL) = cpu_step;
                   8022: }
1.1.1.3   root     8023: #endif
1.1       root     8024: 
                   8025: inline void pcbios_int_15h_cah()
                   8026: {
                   8027:        switch(REG8(AL)) {
1.1.1.22  root     8028:        case 0x00:
1.1       root     8029:                if(REG8(BL) > 0x3f) {
                   8030:                        REG8(AH) = 0x03;
1.1.1.3   root     8031:                        m_CF = 1;
1.1       root     8032:                } else if(REG8(BL) < 0x0e) {
                   8033:                        REG8(AH) = 0x04;
1.1.1.3   root     8034:                        m_CF = 1;
1.1       root     8035:                } else {
1.1.1.8   root     8036:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     8037:                }
                   8038:                break;
1.1.1.22  root     8039:        case 0x01:
1.1       root     8040:                if(REG8(BL) > 0x3f) {
                   8041:                        REG8(AH) = 0x03;
1.1.1.3   root     8042:                        m_CF = 1;
1.1       root     8043:                } else if(REG8(BL) < 0x0e) {
                   8044:                        REG8(AH) = 0x04;
1.1.1.3   root     8045:                        m_CF = 1;
1.1       root     8046:                } else {
1.1.1.8   root     8047:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     8048:                }
                   8049:                break;
                   8050:        default:
1.1.1.22  root     8051:                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     8052:                REG8(AH) = 0x86;
1.1.1.3   root     8053:                m_CF = 1;
1.1       root     8054:                break;
                   8055:        }
                   8056: }
                   8057: 
1.1.1.22  root     8058: inline void pcbios_int_15h_e8h()
1.1.1.17  root     8059: {
1.1.1.22  root     8060:        switch(REG8(AL)) {
                   8061: #if defined(HAS_I386)
                   8062:        case 0x01:
                   8063:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   8064:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8065:                break;
1.1.1.17  root     8066: #endif
1.1.1.22  root     8067:        default:
                   8068:                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));
                   8069:                REG8(AH) = 0x86;
                   8070:                m_CF = 1;
                   8071:                break;
                   8072:        }
                   8073: }
1.1.1.17  root     8074: 
1.1.1.33  root     8075: void pcbios_update_key_code(bool wait)
1.1       root     8076: {
1.1.1.32  root     8077:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8078: #ifdef USE_SERVICE_THREAD
                   8079:                EnterCriticalSection(&key_buf_crit_sect);
                   8080: #endif
                   8081:                bool empty = key_buf_char->empty();
                   8082: #ifdef USE_SERVICE_THREAD
                   8083:                LeaveCriticalSection(&key_buf_crit_sect);
                   8084: #endif
                   8085:                if(empty) {
1.1.1.32  root     8086:                        if(!update_key_buffer()) {
1.1.1.33  root     8087:                                if(wait) {
1.1.1.32  root     8088:                                        Sleep(10);
                   8089:                                } else {
                   8090:                                        maybe_idle();
                   8091:                                }
1.1.1.14  root     8092:                        }
                   8093:                }
1.1.1.34  root     8094:        }
                   8095:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8096: #ifdef USE_SERVICE_THREAD
                   8097:                EnterCriticalSection(&key_buf_crit_sect);
                   8098: #endif
1.1.1.32  root     8099:                if(key_buf_char->count() != 0) {
1.1.1.41  root     8100:                        int key_char = key_buf_char->read();
                   8101:                        int key_scan = key_buf_scan->read();
                   8102:                        key_code  = key_char << 0;
                   8103:                        key_code |= key_scan << 8;
1.1.1.35  root     8104:                        key_recv  = 0x0000ffff;
1.1.1.41  root     8105:                        // write to bottom of key buffer
                   8106:                        mem[0x43c] = (UINT8)key_char;
                   8107:                        mem[0x43d] = (UINT8)key_scan;
1.1.1.32  root     8108:                }
                   8109:                if(key_buf_char->count() != 0) {
1.1.1.41  root     8110:                        int key_char = key_buf_char->read();
                   8111:                        int key_scan = key_buf_scan->read();
                   8112:                        key_code |= key_char << 16;
                   8113:                        key_code |= key_scan << 24;
1.1.1.33  root     8114:                        key_recv |= 0xffff0000;
1.1.1.41  root     8115:                        // write to bottom of key buffer
                   8116:                        mem[0x43c] = (UINT8)key_char;
                   8117:                        mem[0x43d] = (UINT8)key_scan;
1.1.1.32  root     8118:                }
1.1.1.35  root     8119: #ifdef USE_SERVICE_THREAD
                   8120:                LeaveCriticalSection(&key_buf_crit_sect);
                   8121: #endif
1.1       root     8122:        }
                   8123: }
                   8124: 
1.1.1.35  root     8125: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     8126: {
1.1.1.33  root     8127:        while(key_recv == 0 && !m_halted) {
                   8128:                pcbios_update_key_code(true);
1.1       root     8129:        }
1.1.1.33  root     8130:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8131:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   8132:                        if(REG8(AH) == 0x10) {
                   8133:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   8134:                        } else {
                   8135:                                key_code = ((key_code >> 16) & 0xff00);
                   8136:                        }
                   8137:                        key_recv >>= 16;
1.1       root     8138:                }
                   8139:        }
                   8140:        REG16(AX) = key_code & 0xffff;
                   8141:        key_code >>= 16;
1.1.1.33  root     8142:        key_recv >>= 16;
1.1.1.35  root     8143:        
                   8144: #ifdef USE_SERVICE_THREAD
                   8145:        service_exit = true;
                   8146: #endif
                   8147:        return(0);
                   8148: }
                   8149: 
                   8150: inline void pcbios_int_16h_00h()
                   8151: {
                   8152: #ifdef USE_SERVICE_THREAD
                   8153:        start_service_loop(pcbios_int_16h_00h_thread);
                   8154: #else
                   8155:        pcbios_int_16h_00h_thread(NULL);
                   8156:        REQUEST_HARDWRE_UPDATE();
                   8157: #endif
1.1       root     8158: }
                   8159: 
                   8160: inline void pcbios_int_16h_01h()
                   8161: {
1.1.1.33  root     8162:        if(key_recv == 0) {
                   8163:                pcbios_update_key_code(false);
1.1.1.5   root     8164:        }
1.1.1.33  root     8165:        if(key_recv != 0) {
                   8166:                UINT32 key_code_tmp = key_code;
                   8167:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8168:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   8169:                                if(REG8(AH) == 0x11) {
                   8170:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   8171:                                } else {
                   8172:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   8173:                                }
                   8174:                        }
1.1       root     8175:                }
1.1.1.5   root     8176:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     8177: #if defined(HAS_I386)
1.1.1.33  root     8178:                m_ZF = 0;
                   8179: #else
                   8180:                m_ZeroVal = 1;
                   8181: #endif
                   8182:        } else {
                   8183: #if defined(HAS_I386)
                   8184:                m_ZF = 1;
1.1.1.3   root     8185: #else
1.1.1.33  root     8186:                m_ZeroVal = 0;
1.1.1.3   root     8187: #endif
1.1.1.33  root     8188:        }
1.1       root     8189: }
                   8190: 
                   8191: inline void pcbios_int_16h_02h()
                   8192: {
                   8193:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   8194:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   8195:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   8196:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   8197:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   8198:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   8199:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   8200:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   8201: }
                   8202: 
                   8203: inline void pcbios_int_16h_03h()
                   8204: {
                   8205:        static UINT16 status = 0;
                   8206:        
                   8207:        switch(REG8(AL)) {
                   8208:        case 0x05:
                   8209:                status = REG16(BX);
                   8210:                break;
                   8211:        case 0x06:
                   8212:                REG16(BX) = status;
                   8213:                break;
                   8214:        default:
1.1.1.3   root     8215:                m_CF = 1;
1.1       root     8216:                break;
                   8217:        }
                   8218: }
                   8219: 
                   8220: inline void pcbios_int_16h_05h()
                   8221: {
1.1.1.32  root     8222:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8223: #ifdef USE_SERVICE_THREAD
                   8224:                EnterCriticalSection(&key_buf_crit_sect);
                   8225: #endif
1.1.1.32  root     8226:                key_buf_char->write(REG8(CL));
                   8227:                key_buf_scan->write(REG8(CH));
1.1.1.35  root     8228: #ifdef USE_SERVICE_THREAD
                   8229:                LeaveCriticalSection(&key_buf_crit_sect);
                   8230: #endif
1.1.1.32  root     8231:        }
1.1       root     8232:        REG8(AL) = 0x00;
                   8233: }
                   8234: 
                   8235: inline void pcbios_int_16h_12h()
                   8236: {
                   8237:        pcbios_int_16h_02h();
                   8238:        
                   8239:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   8240:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   8241:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   8242:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   8243:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   8244:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   8245:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   8246:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   8247: }
                   8248: 
                   8249: inline void pcbios_int_16h_13h()
                   8250: {
                   8251:        static UINT16 status = 0;
                   8252:        
                   8253:        switch(REG8(AL)) {
                   8254:        case 0x00:
                   8255:                status = REG16(DX);
                   8256:                break;
                   8257:        case 0x01:
                   8258:                REG16(DX) = status;
                   8259:                break;
                   8260:        default:
1.1.1.22  root     8261:                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     8262:                m_CF = 1;
1.1       root     8263:                break;
                   8264:        }
                   8265: }
                   8266: 
                   8267: inline void pcbios_int_16h_14h()
                   8268: {
                   8269:        static UINT8 status = 0;
                   8270:        
                   8271:        switch(REG8(AL)) {
                   8272:        case 0x00:
                   8273:        case 0x01:
                   8274:                status = REG8(AL);
                   8275:                break;
                   8276:        case 0x02:
                   8277:                REG8(AL) = status;
                   8278:                break;
                   8279:        default:
1.1.1.22  root     8280:                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     8281:                m_CF = 1;
1.1       root     8282:                break;
                   8283:        }
                   8284: }
                   8285: 
1.1.1.24  root     8286: inline void pcbios_int_16h_55h()
                   8287: {
                   8288:        switch(REG8(AL)) {
                   8289:        case 0x00:
                   8290:                // keyboard tsr is not present
                   8291:                break;
                   8292:        case 0xfe:
                   8293:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   8294:                break;
                   8295:        case 0xff:
                   8296:                break;
                   8297:        default:
                   8298:                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));
                   8299:                m_CF = 1;
                   8300:                break;
                   8301:        }
                   8302: }
                   8303: 
1.1.1.30  root     8304: inline void pcbios_int_16h_6fh()
                   8305: {
                   8306:        switch(REG8(AL)) {
                   8307:        case 0x00:
                   8308:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   8309:                break;
                   8310:        default:
                   8311:                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));
                   8312:                m_CF = 1;
                   8313:                break;
                   8314:        }
                   8315: }
                   8316: 
1.1.1.37  root     8317: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   8318: {
                   8319:        UINT8 hi = jis >> 8;
                   8320:        UINT8 lo = jis & 0xff;
                   8321:        
                   8322:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   8323:        hi = (hi - 0x21) / 2 + 0x81;
                   8324:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   8325:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   8326:        
                   8327:        return((hi << 8) + lo);
                   8328: }
                   8329: 
                   8330: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   8331: {
                   8332:        UINT8 hi = sjis >> 8;
                   8333:        UINT8 lo = sjis & 0xff;
                   8334:        
                   8335:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   8336:                return(0x2121);
                   8337:        }
                   8338:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   8339:                return(0x2121);
                   8340:        }
                   8341:        if(hi >= 0xf0 && hi <= 0xf3) {
                   8342:                // gaiji
                   8343:                if(lo >= 0x40 && lo <= 0x7e) {
                   8344:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   8345:                }
                   8346:                if(lo >= 0x80 && lo <= 0x9e) {
                   8347:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   8348:                }
                   8349:                if(lo >= 0x9f && lo <= 0xfc) {
                   8350:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   8351:                }
                   8352:        }
                   8353:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   8354:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   8355:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   8356:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   8357:        
                   8358:        return((hi << 8) + lo);
                   8359: }
                   8360: 
1.1.1.38  root     8361: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   8362: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   8363: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     8364: 
                   8365: void pcbios_printer_out(int c, UINT8 data)
                   8366: {
                   8367:        if(pio[c].conv_mode) {
                   8368:                if(pio[c].sjis_hi != 0) {
                   8369:                        if(!pio[c].jis_mode) {
                   8370:                                printer_out(c, 0x1c);
                   8371:                                printer_out(c, 0x26);
                   8372:                                pio[c].jis_mode = true;
                   8373:                        }
                   8374:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   8375:                        printer_out(c, jis >> 8);
                   8376:                        printer_out(c, jis & 0xff);
                   8377:                        pio[c].sjis_hi = 0;
                   8378:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   8379:                        printer_out(c, data);
                   8380:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8381:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8382:                        }
                   8383:                        pio[c].esc_len++;
                   8384:                        
                   8385:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8386:                        case 0x33: // 1Bh 33h XX
                   8387:                        case 0x4a: // 1Bh 4Ah XX
                   8388:                        case 0x4e: // 1Bh 4Eh XX
                   8389:                        case 0x51: // 1Bh 51h XX
                   8390:                        case 0x55: // 1Bh 55h XX
                   8391:                        case 0x6c: // 1Bh 6Ch XX
                   8392:                        case 0x71: // 1Bh 71h XX
                   8393:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     8394:                                if(pio[c].esc_len == 3) {
                   8395:                                        pio[c].esc_buf[0] = 0x00;
                   8396:                                }
                   8397:                                break;
1.1.1.38  root     8398:                        case 0x24: // 1Bh 24h XX XX
                   8399:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     8400:                                if(pio[c].esc_len == 4) {
                   8401:                                        pio[c].esc_buf[0] = 0x00;
                   8402:                                }
                   8403:                                break;
1.1.1.38  root     8404:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     8405:                                if(pio[c].esc_len >= 3) {
                   8406:                                        switch(pio[c].esc_buf[2]) {
                   8407:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   8408:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   8409:                                                        pio[c].esc_buf[0] = 0x00;
                   8410:                                                }
                   8411:                                                break;
                   8412:                                        case 32: case 33: case 38: case 39: case 40:
                   8413:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   8414:                                                        pio[c].esc_buf[0] = 0x00;
                   8415:                                                }
                   8416:                                                break;
1.1.1.38  root     8417:                                        case 71: case 72: case 73:
                   8418:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   8419:                                                        pio[c].esc_buf[0] = 0x00;
                   8420:                                                }
                   8421:                                                break;
1.1.1.37  root     8422:                                        default:
                   8423:                                                pio[c].esc_buf[0] = 0x00;
                   8424:                                                break;
                   8425:                                        }
                   8426:                                }
                   8427:                                break;
1.1.1.38  root     8428:                        case 0x40: // 1Bh 40h
1.1.1.37  root     8429:                                if(pio[c].jis_mode) {
                   8430:                                        printer_out(c, 0x1c);
                   8431:                                        printer_out(c, 0x2e);
                   8432:                                        pio[c].jis_mode = false;
                   8433:                                }
                   8434:                                pio[c].esc_buf[0] = 0x00;
                   8435:                                break;
1.1.1.38  root     8436:                        case 0x42: // 1Bh 42h data 00h
                   8437:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8438:                                if(pio[c].esc_len >= 3 && data == 0) {
                   8439:                                        pio[c].esc_buf[0] = 0x00;
                   8440:                                }
                   8441:                                break;
1.1.1.38  root     8442:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     8443:                                if(pio[c].esc_len >= 3 && data != 0) {
                   8444:                                        pio[c].esc_buf[0] = 0x00;
                   8445:                                }
                   8446:                                break;
1.1.1.38  root     8447:                        default: // 1Bh XX
1.1.1.37  root     8448:                                pio[c].esc_buf[0] = 0x00;
                   8449:                                break;
                   8450:                        }
                   8451:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   8452:                        printer_out(c, data);
                   8453:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8454:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8455:                        }
                   8456:                        pio[c].esc_len++;
                   8457:                        
                   8458:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8459:                        case 0x21: // 1Ch 21h XX
                   8460:                        case 0x2d: // 1Ch 2Dh XX
                   8461:                        case 0x57: // 1Ch 57h XX
                   8462:                        case 0x6b: // 1Ch 6Bh XX
                   8463:                        case 0x72: // 1Ch 72h XX
                   8464:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     8465:                                if(pio[c].esc_len == 3) {
                   8466:                                        pio[c].esc_buf[0] = 0x00;
                   8467:                                }
                   8468:                                break;
1.1.1.38  root     8469:                        case 0x26: // 1Ch 26h
1.1.1.37  root     8470:                                pio[c].jis_mode = true;
                   8471:                                pio[c].esc_buf[0] = 0x00;
                   8472:                                break;
1.1.1.38  root     8473:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     8474:                                pio[c].jis_mode = false;
                   8475:                                pio[c].esc_buf[0] = 0x00;
                   8476:                                break;
1.1.1.38  root     8477:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     8478:                                if(pio[c].esc_len == 76) {
                   8479:                                        pio[c].esc_buf[0] = 0x00;
                   8480:                                }
                   8481:                                break;
1.1.1.38  root     8482:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8483:                                if(pio[c].esc_len == 6) {
                   8484:                                        pio[c].esc_buf[0] = 0x00;
                   8485:                                }
                   8486:                                break;
1.1.1.38  root     8487:                        case 0x53: // 1Ch 53h XX XX
                   8488:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     8489:                                if(pio[c].esc_len == 4) {
                   8490:                                        pio[c].esc_buf[0] = 0x00;
                   8491:                                }
                   8492:                                break;
1.1.1.38  root     8493:                        default: // 1Ch XX
1.1.1.37  root     8494:                                pio[c].esc_buf[0] = 0x00;
                   8495:                                break;
                   8496:                        }
                   8497:                } else if(data == 0x1b || data == 0x1c) {
                   8498:                        printer_out(c, data);
                   8499:                        pio[c].esc_buf[0] = data;
                   8500:                        pio[c].esc_len = 1;
                   8501:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   8502:                        pio[c].sjis_hi = data;
                   8503:                } else {
                   8504:                        if(pio[c].jis_mode) {
                   8505:                                printer_out(c, 0x1c);
                   8506:                                printer_out(c, 0x2e);
                   8507:                                pio[c].jis_mode = false;
                   8508:                        }
                   8509:                        printer_out(c, data);
                   8510:                }
                   8511:        } else {
                   8512:                if(pio[c].jis_mode) {
                   8513:                        printer_out(c, 0x1c);
                   8514:                        printer_out(c, 0x2e);
                   8515:                        pio[c].jis_mode = false;
                   8516:                }
                   8517:                printer_out(c, data);
                   8518:        }
                   8519: }
                   8520: 
                   8521: inline void pcbios_int_17h_00h()
                   8522: {
                   8523:        if(REG16(DX) < 3) {
                   8524:                pcbios_printer_out(REG16(DX), REG8(AL));
                   8525:                REG8(AH) = 0xd0;
                   8526:        }
                   8527: }
                   8528: 
                   8529: inline void pcbios_int_17h_01h()
                   8530: {
                   8531:        if(REG16(DX) < 3) {
                   8532:                REG8(AH) = 0xd0;
                   8533:        }
                   8534: }
                   8535: 
                   8536: inline void pcbios_int_17h_02h()
                   8537: {
                   8538:        if(REG16(DX) < 3) {
                   8539:                REG8(AH) = 0xd0;
                   8540:        }
                   8541: }
                   8542: 
                   8543: inline void pcbios_int_17h_03h()
                   8544: {
                   8545:        switch(REG8(AL)) {
                   8546:        case 0x00:
                   8547:                if(REG16(DX) < 3) {
                   8548:                        if(pio[REG16(DX)].jis_mode) {
                   8549:                                printer_out(REG16(DX), 0x1c);
                   8550:                                printer_out(REG16(DX), 0x2e);
                   8551:                                pio[REG16(DX)].jis_mode = false;
                   8552:                        }
                   8553:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   8554:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   8555:                        }
                   8556:                        REG16(CX) = 0x0000;
                   8557:                        REG8(AH) = 0xd0;
                   8558:                }
                   8559:                break;
                   8560:        default:
                   8561:                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));
                   8562:                break;
                   8563:        }
                   8564: }
                   8565: 
                   8566: inline void pcbios_int_17h_50h()
                   8567: {
                   8568:        switch(REG8(AL)) {
                   8569:        case 0x00:
                   8570:                if(REG16(DX) < 3) {
                   8571:                        if(REG16(BX) = 0x0001) {
                   8572:                                pio[REG16(DX)].conv_mode = false;
                   8573:                                REG8(AL) = 0x00;
                   8574:                        } else if(REG16(BX) = 0x0051) {
                   8575:                                pio[REG16(DX)].conv_mode = true;
                   8576:                                REG8(AL) = 0x00;
                   8577:                        } else {
                   8578:                                REG8(AL) = 0x01;
                   8579:                        }
                   8580:                } else {
                   8581:                        REG8(AL) = 0x02;
                   8582:                }
                   8583:                break;
                   8584:        case 0x01:
                   8585:                if(REG16(DX) < 3) {
                   8586:                        if(pio[REG16(DX)].conv_mode) {
                   8587:                                REG16(BX) = 0x0051;
                   8588:                        } else {
                   8589:                                REG16(BX) = 0x0001;
                   8590:                        }
                   8591:                        REG8(AL) = 0x00;
                   8592:                } else {
                   8593:                        REG8(AL) = 0x02;
                   8594:                }
                   8595:                break;
                   8596:        default:
                   8597:                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));
                   8598:                break;
                   8599:        }
                   8600: }
                   8601: 
                   8602: inline void pcbios_int_17h_51h()
                   8603: {
                   8604:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   8605:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   8606:        } else {
                   8607:                REG16(DX) = 0x0000;
                   8608:        }
                   8609: }
                   8610: 
                   8611: inline void pcbios_int_17h_52h()
                   8612: {
                   8613:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   8614:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   8615:        } else {
                   8616:                REG16(DX) = 0x0000;
                   8617:        }
                   8618: }
                   8619: 
                   8620: inline void pcbios_int_17h_84h()
                   8621: {
                   8622:        if(REG16(DX) < 3) {
                   8623:                if(pio[REG16(DX)].jis_mode) {
                   8624:                        printer_out(REG16(DX), 0x1c);
                   8625:                        printer_out(REG16(DX), 0x2e);
                   8626:                        pio[REG16(DX)].jis_mode = false;
                   8627:                }
                   8628:                printer_out(REG16(DX), REG8(AL));
                   8629:                REG8(AH) = 0xd0;
                   8630:        }
                   8631: }
                   8632: 
                   8633: inline void pcbios_int_17h_85h()
                   8634: {
                   8635:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   8636: }
                   8637: 
1.1       root     8638: inline void pcbios_int_1ah_00h()
                   8639: {
1.1.1.19  root     8640:        pcbios_update_daily_timer_counter(timeGetTime());
                   8641:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   8642:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   8643:        REG8(AL) = mem[0x470];
                   8644:        mem[0x470] = 0;
1.1       root     8645: }
                   8646: 
                   8647: inline int to_bcd(int t)
                   8648: {
                   8649:        int u = (t % 100) / 10;
                   8650:        return (u << 4) | (t % 10);
                   8651: }
                   8652: 
                   8653: inline void pcbios_int_1ah_02h()
                   8654: {
                   8655:        SYSTEMTIME time;
                   8656:        
                   8657:        GetLocalTime(&time);
                   8658:        REG8(CH) = to_bcd(time.wHour);
                   8659:        REG8(CL) = to_bcd(time.wMinute);
                   8660:        REG8(DH) = to_bcd(time.wSecond);
                   8661:        REG8(DL) = 0x00;
                   8662: }
                   8663: 
                   8664: inline void pcbios_int_1ah_04h()
                   8665: {
                   8666:        SYSTEMTIME time;
                   8667:        
                   8668:        GetLocalTime(&time);
                   8669:        REG8(CH) = to_bcd(time.wYear / 100);
                   8670:        REG8(CL) = to_bcd(time.wYear);
                   8671:        REG8(DH) = to_bcd(time.wMonth);
                   8672:        REG8(DL) = to_bcd(time.wDay);
                   8673: }
                   8674: 
                   8675: inline void pcbios_int_1ah_0ah()
                   8676: {
                   8677:        SYSTEMTIME time;
                   8678:        FILETIME file_time;
                   8679:        WORD dos_date, dos_time;
                   8680:        
                   8681:        GetLocalTime(&time);
                   8682:        SystemTimeToFileTime(&time, &file_time);
                   8683:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   8684:        REG16(CX) = dos_date;
                   8685: }
                   8686: 
                   8687: // msdos system call
                   8688: 
1.1.1.43  root     8689: inline void msdos_int_21h_56h(int lfn);
                   8690: 
1.1       root     8691: inline void msdos_int_21h_00h()
                   8692: {
1.1.1.3   root     8693:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     8694: }
                   8695: 
1.1.1.35  root     8696: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     8697: {
                   8698:        REG8(AL) = msdos_getche();
1.1.1.33  root     8699:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8700:        
1.1.1.35  root     8701: #ifdef USE_SERVICE_THREAD
                   8702:        service_exit = true;
                   8703: #endif
                   8704:        return(0);
                   8705: }
                   8706: 
                   8707: inline void msdos_int_21h_01h()
                   8708: {
                   8709: #ifdef USE_SERVICE_THREAD
                   8710:        start_service_loop(msdos_int_21h_01h_thread);
                   8711: #else
                   8712:        msdos_int_21h_01h_thread(NULL);
                   8713:        REQUEST_HARDWRE_UPDATE();
                   8714: #endif
1.1       root     8715: }
                   8716: 
                   8717: inline void msdos_int_21h_02h()
                   8718: {
1.1.1.33  root     8719:        UINT8 data = REG8(DL);
                   8720:        msdos_putch(data);
                   8721:        REG8(AL) = data;
                   8722:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8723: }
                   8724: 
                   8725: inline void msdos_int_21h_03h()
                   8726: {
                   8727:        REG8(AL) = msdos_aux_in();
                   8728: }
                   8729: 
                   8730: inline void msdos_int_21h_04h()
                   8731: {
                   8732:        msdos_aux_out(REG8(DL));
                   8733: }
                   8734: 
                   8735: inline void msdos_int_21h_05h()
                   8736: {
                   8737:        msdos_prn_out(REG8(DL));
                   8738: }
                   8739: 
                   8740: inline void msdos_int_21h_06h()
                   8741: {
                   8742:        if(REG8(DL) == 0xff) {
                   8743:                if(msdos_kbhit()) {
                   8744:                        REG8(AL) = msdos_getch();
1.1.1.3   root     8745: #if defined(HAS_I386)
                   8746:                        m_ZF = 0;
                   8747: #else
                   8748:                        m_ZeroVal = 1;
                   8749: #endif
1.1       root     8750:                } else {
                   8751:                        REG8(AL) = 0;
1.1.1.3   root     8752: #if defined(HAS_I386)
                   8753:                        m_ZF = 1;
                   8754: #else
                   8755:                        m_ZeroVal = 0;
                   8756: #endif
1.1.1.14  root     8757:                        maybe_idle();
1.1       root     8758:                }
                   8759:        } else {
1.1.1.33  root     8760:                UINT8 data = REG8(DL);
                   8761:                msdos_putch(data);
                   8762:                REG8(AL) = data;
1.1       root     8763:        }
                   8764: }
                   8765: 
1.1.1.35  root     8766: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     8767: {
                   8768:        REG8(AL) = msdos_getch();
1.1.1.26  root     8769:        
1.1.1.35  root     8770: #ifdef USE_SERVICE_THREAD
                   8771:        service_exit = true;
                   8772: #endif
                   8773:        return(0);
1.1       root     8774: }
                   8775: 
1.1.1.35  root     8776: inline void msdos_int_21h_07h()
                   8777: {
                   8778: #ifdef USE_SERVICE_THREAD
                   8779:        start_service_loop(msdos_int_21h_07h_thread);
                   8780: #else
                   8781:        msdos_int_21h_07h_thread(NULL);
                   8782:        REQUEST_HARDWRE_UPDATE();
                   8783: #endif
                   8784: }
                   8785: 
                   8786: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     8787: {
                   8788:        REG8(AL) = msdos_getch();
1.1.1.33  root     8789:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8790:        
1.1.1.35  root     8791: #ifdef USE_SERVICE_THREAD
                   8792:        service_exit = true;
                   8793: #endif
                   8794:        return(0);
                   8795: }
                   8796: 
                   8797: inline void msdos_int_21h_08h()
                   8798: {
                   8799: #ifdef USE_SERVICE_THREAD
                   8800:        start_service_loop(msdos_int_21h_08h_thread);
                   8801: #else
                   8802:        msdos_int_21h_08h_thread(NULL);
                   8803:        REQUEST_HARDWRE_UPDATE();
                   8804: #endif
1.1       root     8805: }
                   8806: 
                   8807: inline void msdos_int_21h_09h()
                   8808: {
1.1.1.21  root     8809:        msdos_stdio_reopen();
                   8810:        
1.1.1.20  root     8811:        process_t *process = msdos_process_info_get(current_psp);
                   8812:        int fd = msdos_psp_get_file_table(1, current_psp);
                   8813:        
1.1.1.14  root     8814:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   8815:        int len = 0;
1.1       root     8816:        
1.1.1.14  root     8817:        while(str[len] != '$' && len < 0x10000) {
                   8818:                len++;
                   8819:        }
1.1.1.20  root     8820:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     8821:                // stdout is redirected to file
1.1.1.20  root     8822:                msdos_write(fd, str, len);
1.1       root     8823:        } else {
                   8824:                for(int i = 0; i < len; i++) {
1.1.1.14  root     8825:                        msdos_putch(str[i]);
1.1       root     8826:                }
                   8827:        }
1.1.1.33  root     8828:        REG8(AL) = '$';
                   8829:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8830: }
                   8831: 
1.1.1.35  root     8832: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     8833: {
1.1.1.3   root     8834:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     8835:        int max = mem[ofs] - 1;
                   8836:        UINT8 *buf = mem + ofs + 2;
                   8837:        int chr, p = 0;
                   8838:        
                   8839:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     8840:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     8841:                        p = 0;
1.1.1.33  root     8842:                        msdos_putch(0x03);
                   8843:                        msdos_putch(0x0d);
                   8844:                        msdos_putch(0x0a);
1.1.1.26  root     8845:                        break;
1.1.1.33  root     8846:                } else if(ctrl_break_pressed) {
                   8847:                        // skip this byte
1.1.1.26  root     8848:                } else if(chr == 0x00) {
1.1       root     8849:                        // skip 2nd byte
                   8850:                        msdos_getch();
                   8851:                } else if(chr == 0x08) {
                   8852:                        // back space
                   8853:                        if(p > 0) {
                   8854:                                p--;
1.1.1.20  root     8855:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     8856:                                        msdos_putch(0x08);
                   8857:                                        msdos_putch(0x08);
                   8858:                                        msdos_putch(0x20);
                   8859:                                        msdos_putch(0x20);
                   8860:                                        msdos_putch(0x08);
                   8861:                                        msdos_putch(0x08);
1.1.1.36  root     8862:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   8863:                                        p--;
                   8864:                                        msdos_putch(0x08);
                   8865:                                        msdos_putch(0x08);
                   8866:                                        msdos_putch(0x20);
                   8867:                                        msdos_putch(0x20);
                   8868:                                        msdos_putch(0x08);
                   8869:                                        msdos_putch(0x08);
1.1.1.34  root     8870:                                } else {
                   8871:                                        msdos_putch(0x08);
                   8872:                                        msdos_putch(0x20);
                   8873:                                        msdos_putch(0x08);
                   8874:                                }
                   8875:                        }
                   8876:                } else if(chr == 0x1b) {
                   8877:                        // escape
                   8878:                        while(p > 0) {
                   8879:                                p--;
                   8880:                                if(msdos_ctrl_code_check(buf[p])) {
                   8881:                                        msdos_putch(0x08);
                   8882:                                        msdos_putch(0x08);
                   8883:                                        msdos_putch(0x20);
                   8884:                                        msdos_putch(0x20);
                   8885:                                        msdos_putch(0x08);
                   8886:                                        msdos_putch(0x08);
1.1.1.20  root     8887:                                } else {
1.1.1.34  root     8888:                                        msdos_putch(0x08);
                   8889:                                        msdos_putch(0x20);
                   8890:                                        msdos_putch(0x08);
1.1.1.20  root     8891:                                }
1.1       root     8892:                        }
                   8893:                } else if(p < max) {
                   8894:                        buf[p++] = chr;
                   8895:                        msdos_putch(chr);
                   8896:                }
                   8897:        }
                   8898:        buf[p] = 0x0d;
                   8899:        mem[ofs + 1] = p;
1.1.1.33  root     8900:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8901:        
1.1.1.35  root     8902: #ifdef USE_SERVICE_THREAD
                   8903:        service_exit = true;
                   8904: #endif
                   8905:        return(0);
                   8906: }
                   8907: 
                   8908: inline void msdos_int_21h_0ah()
                   8909: {
                   8910:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   8911: #ifdef USE_SERVICE_THREAD
                   8912:                start_service_loop(msdos_int_21h_0ah_thread);
                   8913: #else
                   8914:                msdos_int_21h_0ah_thread(NULL);
                   8915:                REQUEST_HARDWRE_UPDATE();
                   8916: #endif
                   8917:        }
1.1       root     8918: }
                   8919: 
                   8920: inline void msdos_int_21h_0bh()
                   8921: {
                   8922:        if(msdos_kbhit()) {
                   8923:                REG8(AL) = 0xff;
                   8924:        } else {
                   8925:                REG8(AL) = 0x00;
1.1.1.14  root     8926:                maybe_idle();
1.1       root     8927:        }
1.1.1.33  root     8928:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8929: }
                   8930: 
                   8931: inline void msdos_int_21h_0ch()
                   8932: {
                   8933:        // clear key buffer
1.1.1.21  root     8934:        msdos_stdio_reopen();
                   8935:        
1.1.1.20  root     8936:        process_t *process = msdos_process_info_get(current_psp);
                   8937:        int fd = msdos_psp_get_file_table(0, current_psp);
                   8938:        
                   8939:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     8940:                // stdin is redirected to file
                   8941:        } else {
                   8942:                while(msdos_kbhit()) {
                   8943:                        msdos_getch();
                   8944:                }
                   8945:        }
                   8946:        
                   8947:        switch(REG8(AL)) {
                   8948:        case 0x01:
                   8949:                msdos_int_21h_01h();
                   8950:                break;
                   8951:        case 0x06:
                   8952:                msdos_int_21h_06h();
                   8953:                break;
                   8954:        case 0x07:
                   8955:                msdos_int_21h_07h();
                   8956:                break;
                   8957:        case 0x08:
                   8958:                msdos_int_21h_08h();
                   8959:                break;
                   8960:        case 0x0a:
                   8961:                msdos_int_21h_0ah();
                   8962:                break;
                   8963:        default:
1.1.1.22  root     8964: //             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));
                   8965: //             REG16(AX) = 0x01;
                   8966: //             m_CF = 1;
1.1       root     8967:                break;
                   8968:        }
                   8969: }
                   8970: 
                   8971: inline void msdos_int_21h_0dh()
                   8972: {
                   8973: }
                   8974: 
                   8975: inline void msdos_int_21h_0eh()
                   8976: {
                   8977:        if(REG8(DL) < 26) {
                   8978:                _chdrive(REG8(DL) + 1);
                   8979:                msdos_cds_update(REG8(DL));
1.1.1.23  root     8980:                msdos_sda_update(current_psp);
1.1       root     8981:        }
                   8982:        REG8(AL) = 26; // zdrive
                   8983: }
                   8984: 
1.1.1.14  root     8985: inline void msdos_int_21h_0fh()
                   8986: {
                   8987:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8988:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8989:        char *path = msdos_fcb_path(fcb);
                   8990:        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     8991:        
1.1.1.14  root     8992:        if(hFile == INVALID_HANDLE_VALUE) {
                   8993:                REG8(AL) = 0xff;
                   8994:        } else {
                   8995:                REG8(AL) = 0;
                   8996:                fcb->current_block = 0;
                   8997:                fcb->record_size = 128;
                   8998:                fcb->file_size = GetFileSize(hFile, NULL);
                   8999:                fcb->handle = hFile;
                   9000:                fcb->cur_record = 0;
                   9001:        }
                   9002: }
                   9003: 
                   9004: inline void msdos_int_21h_10h()
                   9005: {
                   9006:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9007:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9008:        
                   9009:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9010: }
                   9011: 
1.1       root     9012: inline void msdos_int_21h_11h()
                   9013: {
1.1.1.3   root     9014:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9015:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9016:        
                   9017:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9018:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9019:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9020:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9021:        char *path = msdos_fcb_path(fcb);
                   9022:        WIN32_FIND_DATA fd;
                   9023:        
1.1.1.13  root     9024:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9025:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9026:                FindClose(dtainfo->find_handle);
                   9027:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9028:        }
                   9029:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9030:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9031:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9032:        
1.1.1.14  root     9033:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9034:                dtainfo->allowable_mask &= ~8;
1.1       root     9035:        }
1.1.1.14  root     9036:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   9037:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9038:                      !msdos_find_file_has_8dot3name(&fd)) {
                   9039:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9040:                                FindClose(dtainfo->find_handle);
                   9041:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9042:                                break;
                   9043:                        }
                   9044:                }
                   9045:        }
1.1.1.13  root     9046:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9047:                if(ext_fcb->flag == 0xff) {
                   9048:                        ext_find->flag = 0xff;
                   9049:                        memset(ext_find->reserved, 0, 5);
                   9050:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9051:                }
                   9052:                find->drive = _getdrive();
1.1.1.13  root     9053:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9054:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9055:                find->nt_res = 0;
                   9056:                msdos_find_file_conv_local_time(&fd);
                   9057:                find->create_time_ms = 0;
                   9058:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9059:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9060:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9061:                find->cluster_hi = find->cluster_lo = 0;
                   9062:                find->file_size = fd.nFileSizeLow;
                   9063:                REG8(AL) = 0x00;
1.1.1.14  root     9064:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9065:                if(ext_fcb->flag == 0xff) {
                   9066:                        ext_find->flag = 0xff;
                   9067:                        memset(ext_find->reserved, 0, 5);
                   9068:                        ext_find->attribute = 8;
                   9069:                }
                   9070:                find->drive = _getdrive();
                   9071:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9072:                find->attribute = 8;
                   9073:                find->nt_res = 0;
                   9074:                msdos_find_file_conv_local_time(&fd);
                   9075:                find->create_time_ms = 0;
                   9076:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9077:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9078:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9079:                find->cluster_hi = find->cluster_lo = 0;
                   9080:                find->file_size = 0;
1.1.1.14  root     9081:                dtainfo->allowable_mask &= ~8;
1.1       root     9082:                REG8(AL) = 0x00;
                   9083:        } else {
                   9084:                REG8(AL) = 0xff;
                   9085:        }
                   9086: }
                   9087: 
                   9088: inline void msdos_int_21h_12h()
                   9089: {
1.1.1.3   root     9090:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     9091: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9092:        
                   9093:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9094:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9095:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9096:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9097:        WIN32_FIND_DATA fd;
                   9098:        
1.1.1.13  root     9099:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9100:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9101:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     9102:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9103:                              !msdos_find_file_has_8dot3name(&fd)) {
                   9104:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9105:                                        FindClose(dtainfo->find_handle);
                   9106:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9107:                                        break;
                   9108:                                }
                   9109:                        }
                   9110:                } else {
1.1.1.13  root     9111:                        FindClose(dtainfo->find_handle);
                   9112:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9113:                }
                   9114:        }
1.1.1.13  root     9115:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9116:                if(ext_fcb->flag == 0xff) {
                   9117:                        ext_find->flag = 0xff;
                   9118:                        memset(ext_find->reserved, 0, 5);
                   9119:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9120:                }
                   9121:                find->drive = _getdrive();
1.1.1.13  root     9122:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9123:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9124:                find->nt_res = 0;
                   9125:                msdos_find_file_conv_local_time(&fd);
                   9126:                find->create_time_ms = 0;
                   9127:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9128:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9129:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9130:                find->cluster_hi = find->cluster_lo = 0;
                   9131:                find->file_size = fd.nFileSizeLow;
                   9132:                REG8(AL) = 0x00;
1.1.1.14  root     9133:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9134:                if(ext_fcb->flag == 0xff) {
                   9135:                        ext_find->flag = 0xff;
                   9136:                        memset(ext_find->reserved, 0, 5);
                   9137:                        ext_find->attribute = 8;
                   9138:                }
                   9139:                find->drive = _getdrive();
                   9140:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9141:                find->attribute = 8;
                   9142:                find->nt_res = 0;
                   9143:                msdos_find_file_conv_local_time(&fd);
                   9144:                find->create_time_ms = 0;
                   9145:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9146:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9147:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9148:                find->cluster_hi = find->cluster_lo = 0;
                   9149:                find->file_size = 0;
1.1.1.14  root     9150:                dtainfo->allowable_mask &= ~8;
1.1       root     9151:                REG8(AL) = 0x00;
                   9152:        } else {
                   9153:                REG8(AL) = 0xff;
                   9154:        }
                   9155: }
                   9156: 
                   9157: inline void msdos_int_21h_13h()
                   9158: {
1.1.1.3   root     9159:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     9160:                REG8(AL) = 0xff;
                   9161:        } else {
                   9162:                REG8(AL) = 0x00;
                   9163:        }
                   9164: }
                   9165: 
1.1.1.16  root     9166: inline void msdos_int_21h_14h()
                   9167: {
                   9168:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9169:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9170:        process_t *process = msdos_process_info_get(current_psp);
                   9171:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9172:        DWORD num = 0;
                   9173:        
                   9174:        memset(mem + dta_laddr, 0, fcb->record_size);
                   9175:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9176:                REG8(AL) = 1;
                   9177:        } else {
                   9178:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9179:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9180:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9181:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9182:        }
                   9183: }
                   9184: 
                   9185: inline void msdos_int_21h_15h()
1.1.1.14  root     9186: {
                   9187:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9188:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     9189:        process_t *process = msdos_process_info_get(current_psp);
                   9190:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9191:        DWORD num = 0;
1.1.1.14  root     9192:        
1.1.1.16  root     9193:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9194:                REG8(AL) = 1;
                   9195:        } else {
                   9196:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9197:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9198:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9199:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9200:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9201:        }
                   9202: }
                   9203: 
                   9204: inline void msdos_int_21h_16h()
                   9205: {
                   9206:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9207:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.14  root     9208:        char *path = msdos_fcb_path(fcb);
                   9209:        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     9210:        
1.1.1.14  root     9211:        if(hFile == INVALID_HANDLE_VALUE) {
                   9212:                REG8(AL) = 0xff;
                   9213:        } else {
                   9214:                REG8(AL) = 0;
                   9215:                fcb->current_block = 0;
                   9216:                fcb->record_size = 128;
                   9217:                fcb->file_size = 0;
                   9218:                fcb->handle = hFile;
                   9219:                fcb->cur_record = 0;
                   9220:        }
                   9221: }
                   9222: 
1.1.1.16  root     9223: inline void msdos_int_21h_17h()
                   9224: {
                   9225:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9226:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
                   9227:        char *path_src = msdos_fcb_path(fcb_src);
                   9228:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   9229:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
                   9230:        char *path_dst = msdos_fcb_path(fcb_dst);
                   9231:        
                   9232:        if(rename(path_src, path_dst)) {
                   9233:                REG8(AL) = 0xff;
                   9234:        } else {
                   9235:                REG8(AL) = 0;
                   9236:        }
                   9237: }
                   9238: 
1.1       root     9239: inline void msdos_int_21h_18h()
                   9240: {
                   9241:        REG8(AL) = 0x00;
                   9242: }
                   9243: 
                   9244: inline void msdos_int_21h_19h()
                   9245: {
                   9246:        REG8(AL) = _getdrive() - 1;
                   9247: }
                   9248: 
                   9249: inline void msdos_int_21h_1ah()
                   9250: {
                   9251:        process_t *process = msdos_process_info_get(current_psp);
                   9252:        
                   9253:        process->dta.w.l = REG16(DX);
1.1.1.3   root     9254:        process->dta.w.h = SREG(DS);
1.1.1.23  root     9255:        msdos_sda_update(current_psp);
1.1       root     9256: }
                   9257: 
                   9258: inline void msdos_int_21h_1bh()
                   9259: {
                   9260:        int drive_num = _getdrive() - 1;
                   9261:        UINT16 seg, ofs;
                   9262:        
                   9263:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9264:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9265:                REG8(AL) = dpb->highest_sector_num + 1;
                   9266:                REG16(CX) = dpb->bytes_per_sector;
                   9267:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9268:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9269:        } else {
                   9270:                REG8(AL) = 0xff;
1.1.1.3   root     9271:                m_CF = 1;
1.1       root     9272:        }
                   9273: 
                   9274: }
                   9275: 
                   9276: inline void msdos_int_21h_1ch()
                   9277: {
1.1.1.41  root     9278:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     9279:        UINT16 seg, ofs;
                   9280:        
                   9281:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9282:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9283:                REG8(AL) = dpb->highest_sector_num + 1;
                   9284:                REG16(CX) = dpb->bytes_per_sector;
                   9285:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9286:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9287:        } else {
                   9288:                REG8(AL) = 0xff;
1.1.1.3   root     9289:                m_CF = 1;
1.1       root     9290:        }
                   9291: 
                   9292: }
                   9293: 
                   9294: inline void msdos_int_21h_1dh()
                   9295: {
                   9296:        REG8(AL) = 0;
                   9297: }
                   9298: 
                   9299: inline void msdos_int_21h_1eh()
                   9300: {
                   9301:        REG8(AL) = 0;
                   9302: }
                   9303: 
                   9304: inline void msdos_int_21h_1fh()
                   9305: {
                   9306:        int drive_num = _getdrive() - 1;
                   9307:        UINT16 seg, ofs;
                   9308:        
                   9309:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9310:                REG8(AL) = 0;
1.1.1.3   root     9311:                SREG(DS) = seg;
                   9312:                i386_load_segment_descriptor(DS);
1.1       root     9313:                REG16(BX) = ofs;
                   9314:        } else {
                   9315:                REG8(AL) = 0xff;
1.1.1.3   root     9316:                m_CF = 1;
1.1       root     9317:        }
                   9318: }
                   9319: 
                   9320: inline void msdos_int_21h_20h()
                   9321: {
                   9322:        REG8(AL) = 0;
                   9323: }
                   9324: 
1.1.1.14  root     9325: inline void msdos_int_21h_21h()
                   9326: {
                   9327:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9328:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9329:        
                   9330:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9331:                REG8(AL) = 1;
                   9332:        } else {
                   9333:                process_t *process = msdos_process_info_get(current_psp);
                   9334:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9335:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     9336:                DWORD num = 0;
1.1.1.14  root     9337:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9338:                        REG8(AL) = 1;
                   9339:                } else {
                   9340:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9341:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9342:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     9343:                }
                   9344:        }
                   9345: }
                   9346: 
                   9347: inline void msdos_int_21h_22h()
                   9348: {
                   9349:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9350:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9351:        
                   9352:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9353:                REG8(AL) = 0xff;
                   9354:        } else {
                   9355:                process_t *process = msdos_process_info_get(current_psp);
                   9356:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     9357:                DWORD num = 0;
1.1.1.14  root     9358:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   9359:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9360:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9361:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9362:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     9363:        }
                   9364: }
                   9365: 
1.1.1.16  root     9366: inline void msdos_int_21h_23h()
                   9367: {
                   9368:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9369:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9370:        char *path = msdos_fcb_path(fcb);
                   9371:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9372:        
                   9373:        if(hFile == INVALID_HANDLE_VALUE) {
                   9374:                REG8(AL) = 0xff;
                   9375:        } else {
                   9376:                UINT32 size = GetFileSize(hFile, NULL);
                   9377:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   9378:                REG8(AL) = 0;
                   9379:        }
                   9380: }
                   9381: 
                   9382: inline void msdos_int_21h_24h()
                   9383: {
                   9384:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9385:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9386:        
                   9387:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   9388: }
                   9389: 
1.1       root     9390: inline void msdos_int_21h_25h()
                   9391: {
                   9392:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     9393:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     9394: }
                   9395: 
                   9396: inline void msdos_int_21h_26h()
                   9397: {
                   9398:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   9399:        
                   9400:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   9401:        psp->first_mcb = REG16(DX) + 16;
                   9402:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   9403:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   9404:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   9405:        psp->parent_psp = 0;
                   9406: }
                   9407: 
1.1.1.16  root     9408: inline void msdos_int_21h_27h()
                   9409: {
                   9410:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9411:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9412:        
                   9413:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9414:                REG8(AL) = 1;
                   9415:        } else {
                   9416:                process_t *process = msdos_process_info_get(current_psp);
                   9417:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9418:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   9419:                DWORD num = 0;
                   9420:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   9421:                        REG8(AL) = 1;
                   9422:                } else {
                   9423:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9424:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9425:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9426:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9427:                }
                   9428:        }
                   9429: }
                   9430: 
                   9431: inline void msdos_int_21h_28h()
                   9432: {
                   9433:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9434:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9435:        
                   9436:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9437:                REG8(AL) = 0xff;
                   9438:        } else {
                   9439:                process_t *process = msdos_process_info_get(current_psp);
                   9440:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9441:                DWORD num = 0;
                   9442:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   9443:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9444:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9445:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9446:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9447:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9448:        }
                   9449: }
                   9450: 
1.1       root     9451: inline void msdos_int_21h_29h()
                   9452: {
1.1.1.20  root     9453:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   9454:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     9455:        UINT8 drv = 0;
                   9456:        char sep_chars[] = ":.;,=+";
                   9457:        char end_chars[] = "\\<>|/\"[]";
                   9458:        char spc_chars[] = " \t";
                   9459:        
1.1.1.20  root     9460:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   9461:        buffer[1023] = 0;
                   9462:        memset(name, 0x20, sizeof(name));
                   9463:        memset(ext, 0x20, sizeof(ext));
                   9464:        
1.1       root     9465:        if(REG8(AL) & 1) {
1.1.1.20  root     9466:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   9467:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     9468:                        ofs++;
                   9469:                }
                   9470:        }
1.1.1.20  root     9471:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     9472:        
1.1.1.24  root     9473:        if(buffer[ofs + 1] == ':') {
                   9474:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   9475:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     9476:                        ofs += 2;
1.1.1.24  root     9477:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9478:                                ofs++;
                   9479:                        }
                   9480:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   9481:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     9482:                        ofs += 2;
1.1.1.24  root     9483:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9484:                                ofs++;
                   9485:                        }
1.1       root     9486:                }
                   9487:        }
1.1.1.20  root     9488:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9489:                UINT8 c = buffer[ofs];
                   9490:                if(is_kanji) {
                   9491:                        is_kanji = 0;
                   9492:                } else if(msdos_lead_byte_check(c)) {
                   9493:                        is_kanji = 1;
                   9494:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9495:                        break;
                   9496:                } else if(c >= 'a' && c <= 'z') {
                   9497:                        c -= 0x20;
                   9498:                }
                   9499:                ofs++;
                   9500:                name[i] = c;
                   9501:        }
1.1.1.20  root     9502:        if(buffer[ofs] == '.') {
1.1       root     9503:                ofs++;
1.1.1.20  root     9504:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9505:                        UINT8 c = buffer[ofs];
                   9506:                        if(is_kanji) {
                   9507:                                is_kanji = 0;
                   9508:                        } else if(msdos_lead_byte_check(c)) {
                   9509:                                is_kanji = 1;
                   9510:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9511:                                break;
                   9512:                        } else if(c >= 'a' && c <= 'z') {
                   9513:                                c -= 0x20;
                   9514:                        }
                   9515:                        ofs++;
                   9516:                        ext[i] = c;
                   9517:                }
                   9518:        }
1.1.1.20  root     9519:        int si = REG16(SI) + ofs;
1.1.1.3   root     9520:        int ds = SREG(DS);
1.1       root     9521:        while(si > 0xffff) {
                   9522:                si -= 0x10;
                   9523:                ds++;
                   9524:        }
                   9525:        REG16(SI) = si;
1.1.1.3   root     9526:        SREG(DS) = ds;
                   9527:        i386_load_segment_descriptor(DS);
1.1       root     9528:        
1.1.1.3   root     9529:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     9530:        if(!(REG8(AL) & 2) || drv != 0) {
                   9531:                fcb[0] = drv;
                   9532:        }
                   9533:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   9534:                memcpy(fcb + 1, name, 8);
                   9535:        }
                   9536:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   9537:                memcpy(fcb + 9, ext, 3);
                   9538:        }
                   9539:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     9540:                if(fcb[i] == '*') {
                   9541:                        found_star = 1;
                   9542:                }
                   9543:                if(found_star) {
                   9544:                        fcb[i] = '?';
                   9545:                }
                   9546:        }
1.1.1.20  root     9547:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     9548:                if(fcb[i] == '*') {
                   9549:                        found_star = 1;
                   9550:                }
                   9551:                if(found_star) {
                   9552:                        fcb[i] = '?';
                   9553:                }
                   9554:        }
                   9555:        
1.1.1.44! root     9556:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     9557:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   9558:                        REG8(AL) = 0x01;
1.1.1.20  root     9559:                } else {
                   9560:                        REG8(AL) = 0x00;
1.1       root     9561:                }
                   9562:        } else {
                   9563:                REG8(AL) = 0xff;
                   9564:        }
                   9565: }
                   9566: 
                   9567: inline void msdos_int_21h_2ah()
                   9568: {
                   9569:        SYSTEMTIME sTime;
                   9570:        
                   9571:        GetLocalTime(&sTime);
                   9572:        REG16(CX) = sTime.wYear;
                   9573:        REG8(DH) = (UINT8)sTime.wMonth;
                   9574:        REG8(DL) = (UINT8)sTime.wDay;
                   9575:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   9576: }
                   9577: 
                   9578: inline void msdos_int_21h_2bh()
                   9579: {
1.1.1.14  root     9580:        REG8(AL) = 0xff;
1.1       root     9581: }
                   9582: 
                   9583: inline void msdos_int_21h_2ch()
                   9584: {
                   9585:        SYSTEMTIME sTime;
                   9586:        
                   9587:        GetLocalTime(&sTime);
                   9588:        REG8(CH) = (UINT8)sTime.wHour;
                   9589:        REG8(CL) = (UINT8)sTime.wMinute;
                   9590:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     9591:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     9592: }
                   9593: 
                   9594: inline void msdos_int_21h_2dh()
                   9595: {
                   9596:        REG8(AL) = 0x00;
                   9597: }
                   9598: 
                   9599: inline void msdos_int_21h_2eh()
                   9600: {
                   9601:        process_t *process = msdos_process_info_get(current_psp);
                   9602:        
                   9603:        process->verify = REG8(AL);
                   9604: }
                   9605: 
                   9606: inline void msdos_int_21h_2fh()
                   9607: {
                   9608:        process_t *process = msdos_process_info_get(current_psp);
                   9609:        
                   9610:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     9611:        SREG(ES) = process->dta.w.h;
                   9612:        i386_load_segment_descriptor(ES);
1.1       root     9613: }
                   9614: 
                   9615: inline void msdos_int_21h_30h()
                   9616: {
                   9617:        // Version Flag / OEM
1.1.1.27  root     9618:        if(REG8(AL) == 0x01) {
1.1.1.29  root     9619: #ifdef SUPPORT_HMA
                   9620:                REG16(BX) = 0x0000;
                   9621: #else
                   9622:                REG16(BX) = 0x1000; // DOS is in HMA
                   9623: #endif
1.1       root     9624:        } else {
1.1.1.27  root     9625:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   9626:                // but this is not correct on Windows 98 SE
                   9627: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   9628:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     9629:        }
1.1.1.27  root     9630:        REG16(CX) = 0x0000;
1.1.1.30  root     9631:        REG8(AL) = dos_major_version;   // 7
                   9632:        REG8(AH) = dos_minor_version;   // 10
1.1       root     9633: }
                   9634: 
                   9635: inline void msdos_int_21h_31h()
                   9636: {
1.1.1.29  root     9637:        try {
                   9638:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9639:        } catch(...) {
                   9640:                // recover the broken mcb
                   9641:                int mcb_seg = current_psp - 1;
                   9642:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     9643:                
1.1.1.29  root     9644:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     9645:                        mcb->mz = 'M';
                   9646:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   9647:                        
1.1.1.29  root     9648:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     9649:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     9650:                        } else {
1.1.1.39  root     9651:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     9652:                        }
                   9653:                } else {
                   9654:                        mcb->mz = 'Z';
1.1.1.30  root     9655:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     9656:                }
                   9657:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9658:        }
1.1       root     9659:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   9660: }
                   9661: 
                   9662: inline void msdos_int_21h_32h()
                   9663: {
                   9664:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   9665:        UINT16 seg, ofs;
                   9666:        
                   9667:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9668:                REG8(AL) = 0;
1.1.1.3   root     9669:                SREG(DS) = seg;
                   9670:                i386_load_segment_descriptor(DS);
1.1       root     9671:                REG16(BX) = ofs;
                   9672:        } else {
                   9673:                REG8(AL) = 0xff;
1.1.1.3   root     9674:                m_CF = 1;
1.1       root     9675:        }
                   9676: }
                   9677: 
                   9678: inline void msdos_int_21h_33h()
                   9679: {
                   9680:        char path[MAX_PATH];
                   9681:        
                   9682:        switch(REG8(AL)) {
                   9683:        case 0x00:
1.1.1.33  root     9684:                REG8(DL) = ctrl_break_checking;
1.1       root     9685:                break;
                   9686:        case 0x01:
1.1.1.33  root     9687:                ctrl_break_checking = REG8(DL);
                   9688:                break;
                   9689:        case 0x02:
                   9690:                {
                   9691:                        UINT8 old = ctrl_break_checking;
                   9692:                        ctrl_break_checking = REG8(DL);
                   9693:                        REG8(DL) = old;
                   9694:                }
                   9695:                break;
                   9696:        case 0x03:
                   9697:        case 0x04:
                   9698:                // DOS 4.0+ - Unused
1.1       root     9699:                break;
                   9700:        case 0x05:
                   9701:                GetSystemDirectory(path, MAX_PATH);
                   9702:                if(path[0] >= 'a' && path[0] <= 'z') {
                   9703:                        REG8(DL) = path[0] - 'a' + 1;
                   9704:                } else {
                   9705:                        REG8(DL) = path[0] - 'A' + 1;
                   9706:                }
                   9707:                break;
                   9708:        case 0x06:
1.1.1.2   root     9709:                // MS-DOS version (7.10)
1.1       root     9710:                REG8(BL) = 7;
1.1.1.2   root     9711:                REG8(BH) = 10;
1.1       root     9712:                REG8(DL) = 0;
1.1.1.29  root     9713: #ifdef SUPPORT_HMA
                   9714:                REG8(DH) = 0x00;
                   9715: #else
                   9716:                REG8(DH) = 0x10; // DOS is in HMA
                   9717: #endif
1.1       root     9718:                break;
1.1.1.6   root     9719:        case 0x07:
                   9720:                if(REG8(DL) == 0) {
                   9721:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   9722:                } else if(REG8(DL) == 1) {
                   9723:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   9724:                }
                   9725:                break;
1.1       root     9726:        default:
1.1.1.22  root     9727:                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     9728:                REG16(AX) = 0x01;
1.1.1.3   root     9729:                m_CF = 1;
1.1       root     9730:                break;
                   9731:        }
                   9732: }
                   9733: 
1.1.1.23  root     9734: inline void msdos_int_21h_34h()
                   9735: {
                   9736:        SREG(ES) = SDA_TOP >> 4;
                   9737:        i386_load_segment_descriptor(ES);
                   9738:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   9739: }
                   9740: 
1.1       root     9741: inline void msdos_int_21h_35h()
                   9742: {
                   9743:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     9744:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   9745:        i386_load_segment_descriptor(ES);
1.1       root     9746: }
                   9747: 
                   9748: inline void msdos_int_21h_36h()
                   9749: {
                   9750:        struct _diskfree_t df = {0};
                   9751:        
                   9752:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   9753:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   9754:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     9755:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   9756:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     9757:        } else {
                   9758:                REG16(AX) = 0xffff;
                   9759:        }
                   9760: }
                   9761: 
                   9762: inline void msdos_int_21h_37h()
                   9763: {
1.1.1.22  root     9764:        static UINT8 dev_flag = 0xff;
1.1       root     9765:        
                   9766:        switch(REG8(AL)) {
                   9767:        case 0x00:
1.1.1.22  root     9768:                {
                   9769:                        process_t *process = msdos_process_info_get(current_psp);
                   9770:                        REG8(AL) = 0x00;
                   9771:                        REG8(DL) = process->switchar;
                   9772:                }
1.1       root     9773:                break;
                   9774:        case 0x01:
1.1.1.22  root     9775:                {
                   9776:                        process_t *process = msdos_process_info_get(current_psp);
                   9777:                        REG8(AL) = 0x00;
                   9778:                        process->switchar = REG8(DL);
1.1.1.23  root     9779:                        msdos_sda_update(current_psp);
1.1.1.22  root     9780:                }
                   9781:                break;
                   9782:        case 0x02:
                   9783:                REG8(DL) = dev_flag;
                   9784:                break;
                   9785:        case 0x03:
                   9786:                dev_flag = REG8(DL);
                   9787:                break;
                   9788:        case 0xd0:
                   9789:        case 0xd1:
                   9790:        case 0xd2:
                   9791:        case 0xd3:
                   9792:        case 0xd4:
                   9793:        case 0xd5:
                   9794:        case 0xd6:
                   9795:        case 0xd7:
                   9796:        case 0xdc:
                   9797:        case 0xdd:
                   9798:        case 0xde:
                   9799:        case 0xdf:
                   9800:                // diet ???
                   9801:                REG16(AX) = 1;
1.1       root     9802:                break;
                   9803:        default:
1.1.1.22  root     9804:                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     9805:                REG16(AX) = 1;
                   9806:                break;
                   9807:        }
                   9808: }
                   9809: 
1.1.1.42  root     9810: int get_country_info(country_info_t *ci, LCID locale)
1.1.1.17  root     9811: {
                   9812:        char LCdata[80];
                   9813:        
1.1.1.19  root     9814:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.42  root     9815:        GetLocaleInfo(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     9816:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.42  root     9817:        GetLocaleInfo(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     9818:        ci->currency_format = *LCdata - '0';
1.1.1.42  root     9819:        GetLocaleInfo(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     9820:        ci->date_format = *LCdata - '0';
1.1.1.42  root     9821:        GetLocaleInfo(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     9822:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.42  root     9823:        GetLocaleInfo(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     9824:        *ci->date_sep = *LCdata;
1.1.1.42  root     9825:        GetLocaleInfo(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     9826:        *ci->dec_sep = *LCdata;
1.1.1.42  root     9827:        GetLocaleInfo(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     9828:        *ci->list_sep = *LCdata;
1.1.1.42  root     9829:        GetLocaleInfo(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     9830:        *ci->thou_sep = *LCdata;
1.1.1.42  root     9831:        GetLocaleInfo(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     9832:        *ci->time_sep = *LCdata;
1.1.1.42  root     9833:        GetLocaleInfo(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     9834:        if(strchr(LCdata, 'H') != NULL) {
                   9835:                ci->time_format = 1;
                   9836:        }
1.1.1.27  root     9837:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffd:000a
1.1.1.24  root     9838:        ci->case_map.w.h = 0xfffd;
1.1.1.42  root     9839:        GetLocaleInfo(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     9840:        return atoi(LCdata);
                   9841: }
                   9842: 
1.1.1.42  root     9843: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   9844: {
                   9845:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   9846: }
                   9847: 
                   9848: int get_country_info(country_info_t *ci)
                   9849: {
                   9850:        return get_country_info(ci, LOCALE_USER_DEFAULT);
                   9851: }
                   9852: 
1.1.1.43  root     9853: void set_country_info(country_info_t *ci, int size)
                   9854: {
                   9855:        char LCdata[80];
                   9856:        
                   9857:        if(size >= 0x00 + 2) {
                   9858:                memset(LCdata, 0, sizeof(LCdata));
                   9859:                *LCdata = '0' + ci->date_format;
                   9860:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
                   9861:        }
                   9862:        if(size >= 0x02 + 5) {
                   9863:                memset(LCdata, 0, sizeof(LCdata));
                   9864:                memcpy(LCdata, &ci->currency_symbol, 4);
                   9865:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
                   9866:        }
                   9867:        if(size >= 0x07 + 2) {
                   9868:                memset(LCdata, 0, sizeof(LCdata));
                   9869:                *LCdata = *ci->thou_sep;
                   9870:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
                   9871:        }
                   9872:        if(size >= 0x09 + 2) {
                   9873:                memset(LCdata, 0, sizeof(LCdata));
                   9874:                *LCdata = *ci->dec_sep;
                   9875:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
                   9876:        }
                   9877:        if(size >= 0x0b + 2) {
                   9878:                memset(LCdata, 0, sizeof(LCdata));
                   9879:                *LCdata = *ci->date_sep;
                   9880:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
                   9881:        }
                   9882:        if(size >= 0x0d + 2) {
                   9883:                memset(LCdata, 0, sizeof(LCdata));
                   9884:                *LCdata = *ci->time_sep;
                   9885:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
                   9886:        }
                   9887:        if(size >= 0x0f + 1) {
                   9888:                memset(LCdata, 0, sizeof(LCdata));
                   9889:                *LCdata = '0' + ci->currency_format;
                   9890:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
                   9891:        }
                   9892:        if(size >= 0x10 + 1) {
                   9893:                sprintf(LCdata, "%d", ci->currency_dec_digits);
                   9894:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
                   9895:        }
                   9896:        if(size >= 0x11 + 1) {
                   9897:                // FIXME: is time format always H/h:mm:ss ???
                   9898:                if(ci->time_format & 1) {
                   9899:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   9900:                } else {
                   9901:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   9902:                }
                   9903:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
                   9904:        }
                   9905:        if(size >= 0x12 + 4) {
                   9906:                // 12h  DWORD   address of case map routine
                   9907:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   9908:        }
                   9909:        if(size >= 0x16 + 2) {
                   9910:                memset(LCdata, 0, sizeof(LCdata));
                   9911:                *LCdata = *ci->list_sep;
                   9912:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
                   9913:        }
                   9914: }
                   9915: 
1.1.1.42  root     9916: #ifndef SUBLANG_SWAHILI
                   9917:        #define SUBLANG_SWAHILI 0x01
                   9918: #endif
                   9919: #ifndef SUBLANG_TSWANA_BOTSWANA
                   9920:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   9921: #endif
                   9922: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   9923:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   9924: #endif
                   9925: #ifndef LANG_BANGLA
                   9926:        #define LANG_BANGLA 0x45
                   9927: #endif
                   9928: #ifndef SUBLANG_BANGLA_BANGLADESH
                   9929:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   9930: #endif
                   9931: 
                   9932: static const struct {
                   9933:        int code;
                   9934:        USHORT usPrimaryLanguage;
                   9935:        USHORT usSubLanguage;
                   9936: } country_table[] = {
                   9937:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   9938:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   9939:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   9940:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   9941:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   9942:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   9943: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   9944: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   9945: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   9946: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   9947: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   9948:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   9949:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   9950: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   9951:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   9952: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   9953:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   9954:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   9955:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   9956:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   9957:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   9958:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   9959:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   9960: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   9961: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   9962:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   9963:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   9964:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   9965:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   9966:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   9967: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   9968: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   9969: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   9970:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   9971: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   9972: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   9973: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   9974: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   9975:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   9976:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   9977:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   9978: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   9979:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   9980:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   9981:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   9982:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   9983:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   9984:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   9985:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   9986:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   9987:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   9988:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   9989:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   9990:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   9991:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   9992: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   9993:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   9994:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   9995:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   9996:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   9997:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   9998:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   9999:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10000:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10001:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10002:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10003: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10004:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10005: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10006:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10007:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10008:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10009:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10010:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10011:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10012:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10013:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10014: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10015:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10016: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10017: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10018:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   10019: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   10020:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   10021:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   10022:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   10023:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   10024:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   10025:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   10026:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   10027: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   10028: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   10029:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   10030: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   10031:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   10032:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   10033:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   10034:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   10035: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   10036: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   10037: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   10038:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   10039:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   10040:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   10041:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   10042:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   10043:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   10044:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   10045:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   10046:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   10047:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   10048:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   10049:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   10050:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   10051:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   10052:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   10053:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   10054:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   10055:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   10056:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   10057:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   10058:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     10059: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     10060:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   10061: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   10062:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   10063:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   10064:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   10065:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   10066:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   10067:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   10068:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   10069:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   10070:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   10071:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   10072:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   10073:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   10074:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   10075:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   10076:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   10077:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   10078:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   10079:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   10080:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   10081:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   10082:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   10083:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   10084:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   10085:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   10086:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   10087: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   10088:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   10089:        {-1, 0, 0},
                   10090: };
                   10091: 
1.1.1.14  root     10092: inline void msdos_int_21h_38h()
                   10093: {
                   10094:        switch(REG8(AL)) {
                   10095:        case 0x00:
1.1.1.19  root     10096:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     10097:                break;
                   10098:        default:
1.1.1.42  root     10099:                for(int i = 0;; i++) {
                   10100:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   10101:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   10102:                                break;
                   10103:                        } else if(country_table[i].code == -1) {
                   10104: //                             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));
                   10105: //                             REG16(AX) = 2;
                   10106: //                             m_CF = 1;
                   10107:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   10108:                                break;
                   10109:                        }
                   10110:                }
1.1.1.14  root     10111:                break;
                   10112:        }
                   10113: }
                   10114: 
1.1       root     10115: inline void msdos_int_21h_39h(int lfn)
                   10116: {
1.1.1.3   root     10117:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10118:                REG16(AX) = errno;
1.1.1.3   root     10119:                m_CF = 1;
1.1       root     10120:        }
                   10121: }
                   10122: 
                   10123: inline void msdos_int_21h_3ah(int lfn)
                   10124: {
1.1.1.3   root     10125:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10126:                REG16(AX) = errno;
1.1.1.3   root     10127:                m_CF = 1;
1.1       root     10128:        }
                   10129: }
                   10130: 
                   10131: inline void msdos_int_21h_3bh(int lfn)
                   10132: {
1.1.1.44! root     10133:        char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
        !          10134:        
        !          10135:        if(_chdir(path)) {
1.1.1.17  root     10136:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     10137:                m_CF = 1;
1.1.1.44! root     10138:        } else {
        !          10139:                int drv = _getdrive() - 1;
        !          10140:                if(path[1] == ':') {
        !          10141:                        if(path[0] >= 'A' && path[0] <= 'Z') {
        !          10142:                                drv = path[0] - 'A';
        !          10143:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
        !          10144:                                drv = path[0] - 'a';
        !          10145:                        }
        !          10146:                }
        !          10147:                msdos_cds_update(drv, path);
1.1       root     10148:        }
                   10149: }
                   10150: 
                   10151: inline void msdos_int_21h_3ch()
                   10152: {
1.1.1.3   root     10153:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10154:        int attr = GetFileAttributes(path);
1.1.1.37  root     10155:        int fd = -1;
1.1.1.11  root     10156:        UINT16 info;
1.1.1.37  root     10157:        int sio_port = 0;
                   10158:        int lpt_port = 0;
1.1       root     10159:        
1.1.1.11  root     10160:        if(msdos_is_con_path(path)) {
                   10161:                fd = _open("CON", _O_WRONLY | _O_BINARY);
                   10162:                info = 0x80d3;
1.1.1.37  root     10163:        } else if((sio_port = msdos_is_comm_path(path)) != 0) {
1.1.1.38  root     10164:                fd = _open("NUL", _O_RDWR | _O_BINARY);
1.1.1.14  root     10165:                info = 0x80d3;
1.1.1.37  root     10166:                msdos_set_comm_params(sio_port, path);
                   10167:        } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
                   10168:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   10169:                info = 0xa8c0;
1.1.1.29  root     10170:        } else if(msdos_is_device_path(path)) {
1.1.1.20  root     10171:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   10172:                info = 0x80d3;
1.1       root     10173:        } else {
                   10174:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     10175:                info = msdos_drive_number(path);
1.1       root     10176:        }
                   10177:        if(fd != -1) {
                   10178:                if(attr == -1) {
                   10179:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   10180:                }
                   10181:                SetFileAttributes(path, attr);
                   10182:                REG16(AX) = fd;
1.1.1.37  root     10183:                msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     10184:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10185:        } else {
                   10186:                REG16(AX) = errno;
1.1.1.3   root     10187:                m_CF = 1;
1.1       root     10188:        }
                   10189: }
                   10190: 
                   10191: inline void msdos_int_21h_3dh()
                   10192: {
1.1.1.3   root     10193:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10194:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     10195:        int fd = -1;
1.1.1.11  root     10196:        UINT16 info;
1.1.1.37  root     10197:        int sio_port = 0;
                   10198:        int lpt_port = 0;
1.1       root     10199:        
                   10200:        if(mode < 0x03) {
1.1.1.11  root     10201:                if(msdos_is_con_path(path)) {
1.1.1.13  root     10202:                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     10203:                        info = 0x80d3;
1.1.1.37  root     10204:                } else if((sio_port = msdos_is_comm_path(path)) != 0) {
                   10205:                        fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14  root     10206:                        info = 0x80d3;
1.1.1.37  root     10207:                        msdos_set_comm_params(sio_port, path);
                   10208:                } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
                   10209:                        fd = _open("NUL", file_mode[mode].mode);
                   10210:                        info = 0xa8c0;
1.1.1.29  root     10211:                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     10212:                        fd = msdos_open("NUL", file_mode[mode].mode);
                   10213:                        info = 0x80d3;
1.1.1.11  root     10214:                } else {
1.1.1.13  root     10215:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     10216:                        info = msdos_drive_number(path);
                   10217:                }
1.1       root     10218:                if(fd != -1) {
                   10219:                        REG16(AX) = fd;
1.1.1.37  root     10220:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     10221:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10222:                } else {
                   10223:                        REG16(AX) = errno;
1.1.1.3   root     10224:                        m_CF = 1;
1.1       root     10225:                }
                   10226:        } else {
                   10227:                REG16(AX) = 0x0c;
1.1.1.3   root     10228:                m_CF = 1;
1.1       root     10229:        }
                   10230: }
                   10231: 
                   10232: inline void msdos_int_21h_3eh()
                   10233: {
                   10234:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10235:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10236:        
1.1.1.20  root     10237:        if(fd < process->max_files && file_handler[fd].valid) {
                   10238:                _close(fd);
                   10239:                msdos_file_handler_close(fd);
                   10240:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     10241:        } else {
                   10242:                REG16(AX) = 0x06;
1.1.1.3   root     10243:                m_CF = 1;
1.1       root     10244:        }
                   10245: }
                   10246: 
1.1.1.35  root     10247: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   10248: {
                   10249:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   10250:        int max = REG16(CX);
                   10251:        int p = 0;
                   10252:        
                   10253:        while(max > p) {
                   10254:                int chr = msdos_getch();
                   10255:                
                   10256:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   10257:                        p = 0;
                   10258:                        buf[p++] = 0x0d;
                   10259:                        if(max > p) {
                   10260:                                buf[p++] = 0x0a;
                   10261:                        }
                   10262:                        msdos_putch(0x03);
                   10263:                        msdos_putch(0x0d);
                   10264:                        msdos_putch(0x0a);
                   10265:                        break;
                   10266:                } else if(ctrl_break_pressed) {
                   10267:                        // skip this byte
                   10268:                } else if(chr == 0x00) {
                   10269:                        // skip 2nd byte
                   10270:                        msdos_getch();
                   10271:                } else if(chr == 0x0d) {
                   10272:                        // carriage return
                   10273:                        buf[p++] = 0x0d;
                   10274:                        if(max > p) {
                   10275:                                buf[p++] = 0x0a;
                   10276:                        }
                   10277:                        msdos_putch('\n');
                   10278:                        break;
                   10279:                } else if(chr == 0x08) {
                   10280:                        // back space
                   10281:                        if(p > 0) {
                   10282:                                p--;
                   10283:                                if(msdos_ctrl_code_check(buf[p])) {
                   10284:                                        msdos_putch(0x08);
                   10285:                                        msdos_putch(0x08);
                   10286:                                        msdos_putch(0x20);
                   10287:                                        msdos_putch(0x20);
                   10288:                                        msdos_putch(0x08);
                   10289:                                        msdos_putch(0x08);
1.1.1.36  root     10290:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   10291:                                        p--;
                   10292:                                        msdos_putch(0x08);
                   10293:                                        msdos_putch(0x08);
                   10294:                                        msdos_putch(0x20);
                   10295:                                        msdos_putch(0x20);
                   10296:                                        msdos_putch(0x08);
                   10297:                                        msdos_putch(0x08);
1.1.1.35  root     10298:                                } else {
                   10299:                                        msdos_putch(0x08);
                   10300:                                        msdos_putch(0x20);
                   10301:                                        msdos_putch(0x08);
                   10302:                                }
                   10303:                        }
                   10304:                } else if(chr == 0x1b) {
                   10305:                        // escape
                   10306:                        while(p > 0) {
                   10307:                                p--;
                   10308:                                if(msdos_ctrl_code_check(buf[p])) {
                   10309:                                        msdos_putch(0x08);
                   10310:                                        msdos_putch(0x08);
                   10311:                                        msdos_putch(0x20);
                   10312:                                        msdos_putch(0x20);
                   10313:                                        msdos_putch(0x08);
                   10314:                                        msdos_putch(0x08);
                   10315:                                } else {
                   10316:                                        msdos_putch(0x08);
                   10317:                                        msdos_putch(0x20);
                   10318:                                        msdos_putch(0x08);
                   10319:                                }
                   10320:                        }
                   10321:                } else {
                   10322:                        buf[p++] = chr;
                   10323:                        msdos_putch(chr);
                   10324:                }
                   10325:        }
                   10326:        REG16(AX) = p;
                   10327:        
                   10328: #ifdef USE_SERVICE_THREAD
                   10329:        service_exit = true;
                   10330: #endif
                   10331:        return(0);
                   10332: }
                   10333: 
1.1       root     10334: inline void msdos_int_21h_3fh()
                   10335: {
                   10336:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10337:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10338:        
1.1.1.20  root     10339:        if(fd < process->max_files && file_handler[fd].valid) {
                   10340:                if(file_mode[file_handler[fd].mode].in) {
                   10341:                        if(file_handler[fd].atty) {
1.1       root     10342:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     10343:                                if(REG16(CX) != 0) {
                   10344: #ifdef USE_SERVICE_THREAD
                   10345:                                        start_service_loop(msdos_int_21h_3fh_thread);
                   10346: #else
                   10347:                                        msdos_int_21h_3fh_thread(NULL);
                   10348:                                        REQUEST_HARDWRE_UPDATE();
                   10349: #endif
                   10350:                                } else {
                   10351:                                        REG16(AX) = 0;
1.1       root     10352:                                }
                   10353:                        } else {
1.1.1.37  root     10354:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10355:                        }
                   10356:                } else {
                   10357:                        REG16(AX) = 0x05;
1.1.1.3   root     10358:                        m_CF = 1;
1.1       root     10359:                }
                   10360:        } else {
                   10361:                REG16(AX) = 0x06;
1.1.1.3   root     10362:                m_CF = 1;
1.1       root     10363:        }
                   10364: }
                   10365: 
                   10366: inline void msdos_int_21h_40h()
                   10367: {
                   10368:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10369:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10370:        
1.1.1.20  root     10371:        if(fd < process->max_files && file_handler[fd].valid) {
                   10372:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     10373:                        if(REG16(CX)) {
1.1.1.20  root     10374:                                if(file_handler[fd].atty) {
1.1       root     10375:                                        // BX is stdout/stderr or is redirected to stdout
                   10376:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     10377:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     10378:                                        }
                   10379:                                        REG16(AX) = REG16(CX);
                   10380:                                } else {
1.1.1.20  root     10381:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10382:                                }
                   10383:                        } else {
1.1.1.20  root     10384:                                UINT32 pos = _tell(fd);
                   10385:                                _lseek(fd, 0, SEEK_END);
                   10386:                                UINT32 size = _tell(fd);
1.1.1.12  root     10387:                                if(pos < size) {
1.1.1.20  root     10388:                                        _lseek(fd, pos, SEEK_SET);
                   10389:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     10390:                                } else {
                   10391:                                        for(UINT32 i = size; i < pos; i++) {
                   10392:                                                UINT8 tmp = 0;
1.1.1.23  root     10393:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     10394:                                        }
1.1.1.20  root     10395:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     10396:                                }
1.1.1.23  root     10397:                                REG16(AX) = 0;
1.1       root     10398:                        }
                   10399:                } else {
                   10400:                        REG16(AX) = 0x05;
1.1.1.3   root     10401:                        m_CF = 1;
1.1       root     10402:                }
                   10403:        } else {
                   10404:                REG16(AX) = 0x06;
1.1.1.3   root     10405:                m_CF = 1;
1.1       root     10406:        }
                   10407: }
                   10408: 
                   10409: inline void msdos_int_21h_41h(int lfn)
                   10410: {
1.1.1.3   root     10411:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10412:                REG16(AX) = errno;
1.1.1.3   root     10413:                m_CF = 1;
1.1       root     10414:        }
                   10415: }
                   10416: 
                   10417: inline void msdos_int_21h_42h()
                   10418: {
                   10419:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10420:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10421:        
1.1.1.20  root     10422:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     10423:                if(REG8(AL) < 0x03) {
1.1.1.35  root     10424:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     10425:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   10426:                        UINT32 pos = _tell(fd);
1.1       root     10427:                        REG16(AX) = pos & 0xffff;
                   10428:                        REG16(DX) = (pos >> 16);
                   10429:                } else {
                   10430:                        REG16(AX) = 0x01;
1.1.1.3   root     10431:                        m_CF = 1;
1.1       root     10432:                }
                   10433:        } else {
                   10434:                REG16(AX) = 0x06;
1.1.1.3   root     10435:                m_CF = 1;
1.1       root     10436:        }
                   10437: }
                   10438: 
                   10439: inline void msdos_int_21h_43h(int lfn)
                   10440: {
1.1.1.3   root     10441:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     10442:        int attr;
                   10443:        
1.1.1.14  root     10444:        if(!lfn && REG8(AL) > 2) {
                   10445:                REG16(AX) = 0x01;
                   10446:                m_CF = 1;
                   10447:                return;
                   10448:        }
                   10449:        switch(REG8(lfn ? BL : AL)) {
1.1       root     10450:        case 0x00:
                   10451:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     10452:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   10453:                } else {
                   10454:                        REG16(AX) = (UINT16)GetLastError();
                   10455:                        m_CF = 1;
                   10456:                }
                   10457:                break;
                   10458:        case 0x01:
                   10459:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   10460:                        REG16(AX) = (UINT16)GetLastError();
                   10461:                        m_CF = 1;
                   10462:                }
                   10463:                break;
                   10464:        case 0x02:
                   10465:                {
                   10466:                        DWORD size = GetCompressedFileSize(path, NULL);
                   10467:                        if(size != INVALID_FILE_SIZE) {
                   10468:                                if(size != 0 && size == GetFileSize(path, NULL)) {
                   10469:                                        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   10470:                                        // this isn't correct if the file is in the NTFS MFT
                   10471:                                        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   10472:                                                size = ((size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   10473:                                        }
                   10474:                                }
                   10475:                                REG16(AX) = LOWORD(size);
                   10476:                                REG16(DX) = HIWORD(size);
                   10477:                        } else {
                   10478:                                REG16(AX) = (UINT16)GetLastError();
                   10479:                                m_CF = 1;
1.1       root     10480:                        }
1.1.1.14  root     10481:                }
                   10482:                break;
                   10483:        case 0x03:
                   10484:        case 0x05:
                   10485:        case 0x07:
                   10486:                {
                   10487:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10488:                        if(hFile != INVALID_HANDLE_VALUE) {
                   10489:                                FILETIME local, time;
                   10490:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   10491:                                if(REG8(BL) == 7) {
                   10492:                                        ULARGE_INTEGER hund;
                   10493:                                        hund.LowPart = local.dwLowDateTime;
                   10494:                                        hund.HighPart = local.dwHighDateTime;
                   10495:                                        hund.QuadPart += REG16(SI) * 100000;
                   10496:                                        local.dwLowDateTime = hund.LowPart;
                   10497:                                        local.dwHighDateTime = hund.HighPart;
                   10498:                                }
                   10499:                                LocalFileTimeToFileTime(&local, &time);
                   10500:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   10501:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   10502:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   10503:                                        REG16(AX) = (UINT16)GetLastError();
                   10504:                                        m_CF = 1;
                   10505:                                }
                   10506:                                CloseHandle(hFile);
                   10507:                        } else {
                   10508:                                REG16(AX) = (UINT16)GetLastError();
                   10509:                                m_CF = 1;
1.1       root     10510:                        }
1.1.1.14  root     10511:                }
                   10512:                break;
                   10513:        case 0x04:
                   10514:        case 0x06:
                   10515:        case 0x08:
                   10516:                {
                   10517:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   10518:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   10519:                                FILETIME *time, local;
                   10520:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   10521:                                                   0x06 ? &fad.ftLastAccessTime :
                   10522:                                                          &fad.ftCreationTime;
                   10523:                                FileTimeToLocalFileTime(time, &local);
                   10524:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   10525:                                if(REG8(BL) == 0x08) {
                   10526:                                        ULARGE_INTEGER hund;
                   10527:                                        hund.LowPart = local.dwLowDateTime;
                   10528:                                        hund.HighPart = local.dwHighDateTime;
                   10529:                                        hund.QuadPart /= 100000;
                   10530:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   10531:                                }
                   10532:                        } else {
                   10533:                                REG16(AX) = (UINT16)GetLastError();
                   10534:                                m_CF = 1;
1.1       root     10535:                        }
1.1.1.14  root     10536:                }
                   10537:                break;
1.1.1.43  root     10538:        case 0xff:
                   10539:                if(REG16(BP) == 0x5053) {
                   10540:                        if(REG8(CL) == 0x39) {
                   10541:                                msdos_int_21h_39h(1);
                   10542:                                break;
                   10543:                        } else if(REG8(CL) == 0x56) {
                   10544:                                msdos_int_21h_56h(1);
                   10545:                                break;
                   10546:                        }
                   10547:                }
1.1.1.14  root     10548:        default:
1.1.1.22  root     10549:                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     10550:                REG16(AX) = 0x01;
                   10551:                m_CF = 1;
                   10552:                break;
                   10553:        }
                   10554: }
                   10555: 
                   10556: inline void msdos_int_21h_44h()
                   10557: {
1.1.1.22  root     10558:        static UINT16 iteration_count = 0;
                   10559:        
1.1.1.44! root     10560:        process_t *process;
        !          10561:        int fd, drv;
1.1.1.14  root     10562:        
                   10563:        switch(REG8(AL)) {
                   10564:        case 0x00:
                   10565:        case 0x01:
                   10566:        case 0x02:
                   10567:        case 0x03:
                   10568:        case 0x04:
                   10569:        case 0x05:
                   10570:        case 0x06:
                   10571:        case 0x07:
1.1.1.44! root     10572:                process = msdos_process_info_get(current_psp);
        !          10573:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     10574:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   10575:                        REG16(AX) = 0x06;
                   10576:                        m_CF = 1;
                   10577:                        return;
1.1.1.14  root     10578:                }
                   10579:                break;
                   10580:        case 0x08:
                   10581:        case 0x09:
1.1.1.44! root     10582:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
        !          10583:                if(!msdos_is_valid_drive(drv)) {
        !          10584:                        // invalid drive
1.1.1.14  root     10585:                        REG16(AX) = 0x0f;
                   10586:                        m_CF = 1;
                   10587:                        return;
1.1       root     10588:                }
                   10589:                break;
                   10590:        }
                   10591:        switch(REG8(AL)) {
                   10592:        case 0x00: // get ioctrl data
1.1.1.20  root     10593:                REG16(DX) = file_handler[fd].info;
1.1       root     10594:                break;
                   10595:        case 0x01: // set ioctrl data
1.1.1.20  root     10596:                file_handler[fd].info |= REG8(DL);
1.1       root     10597:                break;
                   10598:        case 0x02: // recv from character device
                   10599:        case 0x03: // send to character device
                   10600:        case 0x04: // recv from block device
                   10601:        case 0x05: // send to block device
                   10602:                REG16(AX) = 0x05;
1.1.1.3   root     10603:                m_CF = 1;
1.1       root     10604:                break;
                   10605:        case 0x06: // get read status
1.1.1.20  root     10606:                if(file_mode[file_handler[fd].mode].in) {
                   10607:                        if(file_handler[fd].atty) {
1.1.1.14  root     10608:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     10609:                        } else {
1.1.1.20  root     10610:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     10611:                        }
1.1.1.14  root     10612:                } else {
                   10613:                        REG8(AL) = 0x00;
1.1       root     10614:                }
                   10615:                break;
                   10616:        case 0x07: // get write status
1.1.1.20  root     10617:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     10618:                        REG8(AL) = 0xff;
                   10619:                } else {
                   10620:                        REG8(AL) = 0x00;
1.1       root     10621:                }
                   10622:                break;
                   10623:        case 0x08: // check removable drive
1.1.1.44! root     10624:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     10625:                        // removable drive
                   10626:                        REG16(AX) = 0x00;
1.1       root     10627:                } else {
1.1.1.14  root     10628:                        // fixed drive
                   10629:                        REG16(AX) = 0x01;
1.1       root     10630:                }
                   10631:                break;
                   10632:        case 0x09: // check remote drive
1.1.1.44! root     10633:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     10634:                        // remote drive
                   10635:                        REG16(DX) = 0x1000;
1.1.1.44! root     10636:                } else if(msdos_is_subst_drive(drv)) {
        !          10637:                        // subst drive
        !          10638:                        REG16(DX) = 0x8000;
1.1       root     10639:                } else {
1.1.1.14  root     10640:                        // local drive
1.1.1.44! root     10641:                        REG16(DX) = 0x0000;
1.1       root     10642:                }
                   10643:                break;
1.1.1.21  root     10644:        case 0x0a: // check remote handle
                   10645:                REG16(DX) = 0x00; // FIXME
                   10646:                break;
1.1       root     10647:        case 0x0b: // set retry count
                   10648:                break;
1.1.1.22  root     10649:        case 0x0c: // generic character device request
                   10650:                if(REG8(CL) == 0x45) {
                   10651:                        // set iteration (retry) count
                   10652:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   10653:                } else if(REG8(CL) == 0x4a) {
                   10654:                        // select code page
                   10655:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   10656:                        msdos_nls_tables_update();
1.1.1.44! root     10657:                } else if(REG8(CL) == 0x4c) {
        !          10658:                        // start code-page preparation
        !          10659:                        int ids[3] = {437, 0, 0}; // 437: US English
        !          10660:                        int count = 1, offset = 0;
        !          10661:                        if(active_code_page != 437) {
        !          10662:                                ids[count++] = active_code_page;
        !          10663:                        }
        !          10664:                        if(system_code_page != 437 && system_code_page != active_code_page) {
        !          10665:                                ids[count++] = system_code_page;
        !          10666:                        }
        !          10667:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
        !          10668:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
        !          10669:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
        !          10670:                        for(int i = 0; i < count; i++) {
        !          10671:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
        !          10672:                        }
        !          10673:                } else if(REG8(CL) == 0x4d) {
        !          10674:                        // end code-page preparation
        !          10675:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
        !          10676:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.22  root     10677:                } else if(REG8(CL) == 0x65) {
                   10678:                        // get iteration (retry) count
                   10679:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   10680:                } else if(REG8(CL) == 0x6a) {
                   10681:                        // query selected code page
                   10682:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   10683:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   10684:                        
                   10685:                        CPINFO info;
                   10686:                        GetCPInfo(active_code_page, &info);
                   10687:                        
                   10688:                        if(info.MaxCharSize != 1) {
                   10689:                                for(int i = 0;; i++) {
                   10690:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   10691:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   10692:                                        
                   10693:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   10694:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   10695:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   10696:                                        
                   10697:                                        if(lo == 0 && hi == 0) {
                   10698:                                                break;
                   10699:                                        }
                   10700:                                }
                   10701:                        }
1.1.1.44! root     10702:                } else if(REG8(CL) == 0x6b) {
        !          10703:                        // query prepare list
        !          10704:                        int ids[3] = {437, 0, 0}; // 437: US English
        !          10705:                        int count = 1, offset = 0;
        !          10706:                        if(active_code_page != 437) {
        !          10707:                                ids[count++] = active_code_page;
        !          10708:                        }
        !          10709:                        if(system_code_page != 437 && system_code_page != active_code_page) {
        !          10710:                                ids[count++] = system_code_page;
        !          10711:                        }
        !          10712:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
        !          10713:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
        !          10714:                        for(int i = 0; i < count; i++) {
        !          10715:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
        !          10716:                        }
        !          10717:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
        !          10718:                        for(int i = 0; i < count; i++) {
        !          10719:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
        !          10720:                        }
1.1.1.22  root     10721:                } else if(REG8(CL) == 0x7f) {
1.1.1.44! root     10722:                        // get display information
1.1.1.22  root     10723:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;
                   10724:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;
                   10725:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;
                   10726:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;
                   10727:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;
                   10728:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;
                   10729:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;
                   10730:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a));
                   10731:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);
                   10732:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a);
                   10733:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;
                   10734:                } else {
                   10735:                        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));
                   10736:                        REG16(AX) = 0x01; // invalid function
                   10737:                        m_CF = 1;
                   10738:                }
                   10739:                break;
                   10740:        case 0x0d: // generic block device request
                   10741:                if(REG8(CL) == 0x40) {
                   10742:                        // set device parameters
                   10743:                } else if(REG8(CL) == 0x46) {
                   10744:                        // set volume serial number
                   10745:                } else if(REG8(CL) == 0x4a) {
                   10746:                        // lock logical volume
                   10747:                } else if(REG8(CL) == 0x4b) {
                   10748:                        // lock physical volume
                   10749:                } else if(REG8(CL) == 0x60) {
                   10750:                        // get device parameters
1.1.1.42  root     10751:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     10752:                        
1.1.1.42  root     10753:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   10754:                                drive_param_t *drive_param = &drive_params[drive_num];
                   10755:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   10756:                                
                   10757:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   10758:                                switch(geo->MediaType) {
                   10759:                                case F5_360_512:
                   10760:                                case F5_320_512:
                   10761:                                case F5_320_1024:
                   10762:                                case F5_180_512:
                   10763:                                case F5_160_512:
                   10764:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   10765:                                        break;
                   10766:                                case F5_1Pt2_512:
                   10767:                                case F3_1Pt2_512:
                   10768:                                case F3_1Pt23_1024:
                   10769:                                case F5_1Pt23_1024:
                   10770:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   10771:                                        break;
                   10772:                                case F3_720_512:
                   10773:                                case F3_640_512:
                   10774:                                case F5_640_512:
                   10775:                                case F5_720_512:
                   10776:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   10777:                                        break;
                   10778:                                case F8_256_128:
                   10779:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   10780:                                        break;
                   10781:                                case FixedMedia:
                   10782:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   10783:                                        break;
                   10784:                                case F3_1Pt44_512:
                   10785:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   10786:                                        break;
                   10787:                                case F3_2Pt88_512:
                   10788:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   10789:                                        break;
                   10790:                                default:
                   10791:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   10792: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   10793:                                        break;
1.1.1.22  root     10794:                                }
1.1.1.42  root     10795:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   10796:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   10797:                                switch(geo->MediaType) {
                   10798:                                case F5_360_512:
                   10799:                                case F5_320_512:
                   10800:                                case F5_320_1024:
                   10801:                                case F5_180_512:
                   10802:                                case F5_160_512:
                   10803:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   10804:                                        break;
                   10805:                                default:
                   10806:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   10807:                                        break;
                   10808:                                }
                   10809:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   10810:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   10811:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   10812:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   10813:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   10814:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   10815:                                switch(geo->MediaType) {
                   10816:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   10817:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   10818:                                        break;
                   10819:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   10820:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   10821:                                        break;
                   10822:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   10823:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   10824:                                        break;
                   10825:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   10826:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   10827:                                        break;
                   10828:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   10829:                                case F3_1Pt2_512:
                   10830:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   10831:                                case F5_720_512:
                   10832:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   10833:                                        break;
                   10834:                                case FixedMedia:        // hard disk
                   10835:                                case RemovableMedia:
                   10836:                                case Unknown:
                   10837:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   10838:                                        break;
                   10839:                                default:
                   10840:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   10841:                                        break;
                   10842:                                }
                   10843:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   10844:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   10845:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   10846:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   10847:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   10848:                                // 21h  BYTE    device type
                   10849:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     10850:                        } else {
                   10851:                                REG16(AX) = 0x0f; // invalid drive
                   10852:                                m_CF = 1;
                   10853:                        }
                   10854:                } else if(REG8(CL) == 0x66) {
                   10855:                        // get volume serial number
                   10856:                        char path[] = "A:\\";
                   10857:                        char volume_label[MAX_PATH];
                   10858:                        DWORD serial_number = 0;
                   10859:                        char file_system[MAX_PATH];
                   10860:                        
                   10861:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   10862:                        
                   10863:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   10864:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   10865:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   10866:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   10867:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   10868:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   10869:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   10870:                        } else {
                   10871:                                REG16(AX) = 0x0f; // invalid drive
                   10872:                                m_CF = 1;
                   10873:                        }
                   10874:                } else if(REG8(CL) == 0x67) {
                   10875:                        // get access flag
                   10876:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   10877:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   10878:                } else if(REG8(CL) == 0x68) {
                   10879:                        // sense media type
1.1.1.42  root     10880:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     10881:                        
1.1.1.42  root     10882:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   10883:                                drive_param_t *drive_param = &drive_params[drive_num];
                   10884:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   10885:                                
                   10886:                                switch(geo->MediaType) {
                   10887:                                case F3_720_512:
                   10888:                                case F5_720_512:
                   10889:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   10890:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   10891:                                        break;
                   10892:                                case F3_1Pt44_512:
                   10893:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   10894:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   10895:                                        break;
                   10896:                                case F3_2Pt88_512:
                   10897:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   10898:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   10899:                                        break;
                   10900:                                default:
                   10901:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   10902:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   10903:                                        break;
1.1.1.22  root     10904:                                }
                   10905:                        } else {
                   10906:                                REG16(AX) = 0x0f; // invalid drive
                   10907:                                m_CF = 1;
                   10908:                        }
                   10909:                } else if(REG8(CL) == 0x6a) {
                   10910:                        // unlock logical volume
                   10911:                } else if(REG8(CL) == 0x6b) {
                   10912:                        // unlock physical volume
                   10913:                } else {
                   10914:                        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));
                   10915:                        REG16(AX) = 0x01; // invalid function
                   10916:                        m_CF = 1;
                   10917:                }
                   10918:                break;
                   10919:        case 0x0e: // get logical drive map
1.1.1.44! root     10920:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
        !          10921:                        REG16(AX) = 0x0f; // invalid drive
        !          10922:                        m_CF = 1;
        !          10923:                } else {
        !          10924:                        REG8(AL) = 0;
1.1.1.22  root     10925:                }
                   10926:                break;
                   10927:        case 0x0f: // set logical drive map
1.1.1.44! root     10928:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
        !          10929:                        REG16(AX) = 0x0f; // invalid drive
        !          10930:                        m_CF = 1;
1.1.1.22  root     10931:                }
                   10932:                break;
                   10933:        case 0x10: // query generic ioctrl capability (handle)
                   10934:                switch(REG8(CL)) {
                   10935:                case 0x45:
                   10936:                case 0x4a:
                   10937:                case 0x65:
                   10938:                case 0x6a:
                   10939:                case 0x7f:
                   10940:                        REG16(AX) = 0x0000; // supported
                   10941:                        break;
                   10942:                default:
                   10943:                        REG8(AL) = 0x01; // ioctl capability not available
                   10944:                        m_CF = 1;
                   10945:                        break;
                   10946:                }
                   10947:                break;
                   10948:        case 0x11: // query generic ioctrl capability (drive)
                   10949:                switch(REG8(CL)) {
                   10950:                case 0x40:
                   10951:                case 0x46:
                   10952:                case 0x4a:
                   10953:                case 0x4b:
                   10954:                case 0x60:
                   10955:                case 0x66:
                   10956:                case 0x67:
                   10957:                case 0x68:
                   10958:                case 0x6a:
                   10959:                case 0x6b:
                   10960:                        REG16(AX) = 0x0000; // supported
                   10961:                        break;
                   10962:                default:
                   10963:                        REG8(AL) = 0x01; // ioctl capability not available
                   10964:                        m_CF = 1;
                   10965:                        break;
                   10966:                }
                   10967:                break;
                   10968:        case 0x12: // determine dos type
                   10969:        case 0x51: // concurrent dos v3.2+ - installation check
                   10970:        case 0x52: // determine dos type/get dr dos versuin
                   10971:                REG16(AX) = 0x01; // this  is not DR-DOS
                   10972:                m_CF = 1;
                   10973:                break;
1.1       root     10974:        default:
1.1.1.22  root     10975:                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     10976:                REG16(AX) = 0x01;
1.1.1.3   root     10977:                m_CF = 1;
1.1       root     10978:                break;
                   10979:        }
                   10980: }
                   10981: 
                   10982: inline void msdos_int_21h_45h()
                   10983: {
                   10984:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10985:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10986:        
1.1.1.20  root     10987:        if(fd < process->max_files && file_handler[fd].valid) {
                   10988:                int dup_fd = _dup(fd);
                   10989:                if(dup_fd != -1) {
                   10990:                        REG16(AX) = dup_fd;
                   10991:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   10992: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   10993:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     10994:                } else {
                   10995:                        REG16(AX) = errno;
1.1.1.3   root     10996:                        m_CF = 1;
1.1       root     10997:                }
                   10998:        } else {
                   10999:                REG16(AX) = 0x06;
1.1.1.3   root     11000:                m_CF = 1;
1.1       root     11001:        }
                   11002: }
                   11003: 
                   11004: inline void msdos_int_21h_46h()
                   11005: {
                   11006:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11007:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11008:        int dup_fd = REG16(CX);
                   11009:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     11010:        
1.1.1.20  root     11011:        if(REG16(BX) == REG16(CX)) {
                   11012:                REG16(AX) = 0x06;
                   11013:                m_CF = 1;
                   11014:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   11015:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   11016:                        _close(tmp_fd);
                   11017:                        msdos_file_handler_close(tmp_fd);
                   11018:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   11019:                }
                   11020:                if(_dup2(fd, dup_fd) != -1) {
                   11021:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11022: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11023:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11024:                } else {
                   11025:                        REG16(AX) = errno;
1.1.1.3   root     11026:                        m_CF = 1;
1.1       root     11027:                }
                   11028:        } else {
                   11029:                REG16(AX) = 0x06;
1.1.1.3   root     11030:                m_CF = 1;
1.1       root     11031:        }
                   11032: }
                   11033: 
                   11034: inline void msdos_int_21h_47h(int lfn)
                   11035: {
                   11036:        char path[MAX_PATH];
                   11037:        
                   11038:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
                   11039:                if(path[1] == ':') {
                   11040:                        // the returned path does not include a drive or the initial backslash
1.1.1.3   root     11041:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), (lfn ? path : msdos_short_path(path)) + 3);
1.1       root     11042:                } else {
1.1.1.3   root     11043:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn ? path : msdos_short_path(path));
1.1       root     11044:                }
                   11045:        } else {
                   11046:                REG16(AX) = errno;
1.1.1.3   root     11047:                m_CF = 1;
1.1       root     11048:        }
                   11049: }
                   11050: 
                   11051: inline void msdos_int_21h_48h()
                   11052: {
1.1.1.19  root     11053:        int seg, umb_linked;
1.1       root     11054:        
1.1.1.8   root     11055:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     11056:                // unlink umb not to allocate memory in umb
                   11057:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   11058:                        msdos_mem_unlink_umb();
                   11059:                }
1.1.1.8   root     11060:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11061:                        REG16(AX) = seg;
                   11062:                } else {
                   11063:                        REG16(AX) = 0x08;
                   11064:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   11065:                        m_CF = 1;
                   11066:                }
1.1.1.19  root     11067:                if(umb_linked != 0) {
                   11068:                        msdos_mem_link_umb();
                   11069:                }
1.1.1.8   root     11070:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   11071:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11072:                        REG16(AX) = seg;
                   11073:                } else {
                   11074:                        REG16(AX) = 0x08;
                   11075:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   11076:                        m_CF = 1;
                   11077:                }
                   11078:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   11079:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11080:                        REG16(AX) = seg;
                   11081:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11082:                        REG16(AX) = seg;
                   11083:                } else {
                   11084:                        REG16(AX) = 0x08;
                   11085:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   11086:                        m_CF = 1;
                   11087:                }
1.1       root     11088:        }
                   11089: }
                   11090: 
                   11091: inline void msdos_int_21h_49h()
                   11092: {
1.1.1.14  root     11093:        int mcb_seg = SREG(ES) - 1;
                   11094:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   11095:        
                   11096:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11097:                msdos_mem_free(SREG(ES));
                   11098:        } else {
1.1.1.33  root     11099:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     11100:                m_CF = 1;
                   11101:        }
1.1       root     11102: }
                   11103: 
                   11104: inline void msdos_int_21h_4ah()
                   11105: {
1.1.1.14  root     11106:        int mcb_seg = SREG(ES) - 1;
                   11107:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     11108:        int max_paragraphs;
                   11109:        
1.1.1.14  root     11110:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11111:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   11112:                        REG16(AX) = 0x08;
                   11113:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   11114:                        m_CF = 1;
                   11115:                }
                   11116:        } else {
1.1.1.33  root     11117:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     11118:                m_CF = 1;
1.1       root     11119:        }
                   11120: }
                   11121: 
                   11122: inline void msdos_int_21h_4bh()
                   11123: {
1.1.1.3   root     11124:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11125:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     11126:        
                   11127:        switch(REG8(AL)) {
                   11128:        case 0x00:
                   11129:        case 0x01:
                   11130:                if(msdos_process_exec(command, param, REG8(AL))) {
                   11131:                        REG16(AX) = 0x02;
1.1.1.3   root     11132:                        m_CF = 1;
1.1       root     11133:                }
                   11134:                break;
1.1.1.14  root     11135:        case 0x03:
                   11136:                {
                   11137:                        int fd;
                   11138:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   11139:                                REG16(AX) = 0x02;
                   11140:                                m_CF = 1;
                   11141:                                break;
                   11142:                        }
                   11143:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   11144:                        _close(fd);
                   11145:                        
                   11146:                        UINT16 *overlay = (UINT16 *)param;
                   11147:                        
                   11148:                        // check exe header
                   11149:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   11150:                        int header_size = 0;
                   11151:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   11152:                                header_size = header->header_size * 16;
                   11153:                                // relocation
                   11154:                                int start_seg = overlay[1];
                   11155:                                for(int i = 0; i < header->relocations; i++) {
                   11156:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   11157:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   11158:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   11159:                                }
                   11160:                        }
                   11161:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   11162:                }
                   11163:                break;
1.1.1.43  root     11164:        case 0xfd:
                   11165:        case 0xfe:
                   11166:                // unknown function called in FreeCOM
                   11167:                REG16(AX) = 0x01;
                   11168:                m_CF = 1;
                   11169:                break;
1.1       root     11170:        default:
1.1.1.22  root     11171:                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     11172:                REG16(AX) = 0x01;
1.1.1.3   root     11173:                m_CF = 1;
1.1       root     11174:                break;
                   11175:        }
                   11176: }
                   11177: 
                   11178: inline void msdos_int_21h_4ch()
                   11179: {
                   11180:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   11181: }
                   11182: 
                   11183: inline void msdos_int_21h_4dh()
                   11184: {
                   11185:        REG16(AX) = retval;
                   11186: }
                   11187: 
                   11188: inline void msdos_int_21h_4eh()
                   11189: {
                   11190:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11191:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11192:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.3   root     11193:        char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11194:        WIN32_FIND_DATA fd;
                   11195:        
1.1.1.14  root     11196:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   11197:        find->find_magic = FIND_MAGIC;
                   11198:        find->dta_index = dtainfo - dtalist;
1.1       root     11199:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     11200:        dtainfo->allowable_mask = REG8(CL);
                   11201:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     11202:        
1.1.1.14  root     11203:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   11204:                dtainfo->allowable_mask &= ~8;
1.1       root     11205:        }
1.1.1.14  root     11206:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   11207:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11208:                      !msdos_find_file_has_8dot3name(&fd)) {
                   11209:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11210:                                FindClose(dtainfo->find_handle);
                   11211:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11212:                                break;
                   11213:                        }
                   11214:                }
                   11215:        }
1.1.1.13  root     11216:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11217:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11218:                msdos_find_file_conv_local_time(&fd);
                   11219:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11220:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11221:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11222:                REG16(AX) = 0;
1.1.1.14  root     11223:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11224:                find->attrib = 8;
                   11225:                find->size = 0;
                   11226:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11227:                dtainfo->allowable_mask &= ~8;
1.1       root     11228:                REG16(AX) = 0;
                   11229:        } else {
                   11230:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     11231:                m_CF = 1;
1.1       root     11232:        }
                   11233: }
                   11234: 
                   11235: inline void msdos_int_21h_4fh()
                   11236: {
                   11237:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11238:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11239:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     11240:        WIN32_FIND_DATA fd;
                   11241:        
1.1.1.14  root     11242:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   11243:                REG16(AX) = 0x12;
                   11244:                m_CF = 1;
                   11245:                return;
                   11246:        }
                   11247:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     11248:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11249:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     11250:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11251:                              !msdos_find_file_has_8dot3name(&fd)) {
                   11252:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11253:                                        FindClose(dtainfo->find_handle);
                   11254:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11255:                                        break;
                   11256:                                }
                   11257:                        }
                   11258:                } else {
1.1.1.13  root     11259:                        FindClose(dtainfo->find_handle);
                   11260:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11261:                }
                   11262:        }
1.1.1.13  root     11263:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11264:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11265:                msdos_find_file_conv_local_time(&fd);
                   11266:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11267:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11268:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11269:                REG16(AX) = 0;
1.1.1.14  root     11270:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11271:                find->attrib = 8;
                   11272:                find->size = 0;
                   11273:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11274:                dtainfo->allowable_mask &= ~8;
1.1       root     11275:                REG16(AX) = 0;
                   11276:        } else {
                   11277:                REG16(AX) = 0x12;
1.1.1.3   root     11278:                m_CF = 1;
1.1       root     11279:        }
                   11280: }
                   11281: 
                   11282: inline void msdos_int_21h_50h()
                   11283: {
1.1.1.8   root     11284:        if(current_psp != REG16(BX)) {
                   11285:                process_t *process = msdos_process_info_get(current_psp);
                   11286:                if(process != NULL) {
                   11287:                        process->psp = REG16(BX);
                   11288:                }
                   11289:                current_psp = REG16(BX);
1.1.1.23  root     11290:                msdos_sda_update(current_psp);
1.1.1.8   root     11291:        }
1.1       root     11292: }
                   11293: 
                   11294: inline void msdos_int_21h_51h()
                   11295: {
                   11296:        REG16(BX) = current_psp;
                   11297: }
                   11298: 
                   11299: inline void msdos_int_21h_52h()
                   11300: {
1.1.1.25  root     11301:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     11302:        i386_load_segment_descriptor(ES);
1.1.1.25  root     11303:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     11304: }
                   11305: 
1.1.1.43  root     11306: inline void msdos_int_21h_53h()
                   11307: {
                   11308:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   11309:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   11310:        
                   11311:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   11312:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   11313:        dpb->shift_count = 0;
                   11314:        dpb->reserved_sectors = 0;
                   11315:        dpb->fat_num = bpb->fat_num;
                   11316:        dpb->root_entries = bpb->root_entries;
                   11317:        dpb->first_data_sector = 0;
                   11318:        if(bpb->sectors_per_cluster != 0) {
                   11319:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   11320:        } else {
                   11321:                dpb->highest_cluster_num = 0;
                   11322:        }
                   11323:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   11324:        dpb->first_dir_sector = 0;
                   11325:        dpb->device_driver_header = 0;
                   11326:        dpb->media_type = bpb->media_type;
                   11327:        dpb->drive_accessed = 0;
                   11328:        dpb->next_dpb_ofs = 0xffff;
                   11329:        dpb->next_dpb_seg = 0xffff;
                   11330:        dpb->first_free_cluster = 0;
                   11331:        dpb->free_clusters = 0xffff;
                   11332: }
                   11333: 
1.1       root     11334: inline void msdos_int_21h_54h()
                   11335: {
                   11336:        process_t *process = msdos_process_info_get(current_psp);
                   11337:        
                   11338:        REG8(AL) = process->verify;
                   11339: }
                   11340: 
                   11341: inline void msdos_int_21h_55h()
                   11342: {
                   11343:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   11344:        
                   11345:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   11346:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   11347:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   11348:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   11349:        psp->parent_psp = current_psp;
                   11350: }
                   11351: 
                   11352: inline void msdos_int_21h_56h(int lfn)
                   11353: {
                   11354:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     11355:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   11356:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     11357:        
                   11358:        if(rename(src, dst)) {
                   11359:                REG16(AX) = errno;
1.1.1.3   root     11360:                m_CF = 1;
1.1       root     11361:        }
                   11362: }
                   11363: 
                   11364: inline void msdos_int_21h_57h()
                   11365: {
                   11366:        FILETIME time, local;
1.1.1.14  root     11367:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     11368:        HANDLE hHandle;
1.1       root     11369:        
1.1.1.21  root     11370:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     11371:                REG16(AX) = (UINT16)GetLastError();
                   11372:                m_CF = 1;
                   11373:                return;
                   11374:        }
                   11375:        ctime = atime = mtime = NULL;
                   11376:        
1.1       root     11377:        switch(REG8(AL)) {
                   11378:        case 0x00:
1.1.1.6   root     11379:        case 0x01:
1.1.1.14  root     11380:                mtime = &time;
1.1.1.6   root     11381:                break;
                   11382:        case 0x04:
                   11383:        case 0x05:
1.1.1.14  root     11384:                atime = &time;
1.1       root     11385:                break;
1.1.1.6   root     11386:        case 0x06:
                   11387:        case 0x07:
1.1.1.14  root     11388:                ctime = &time;
                   11389:                break;
                   11390:        default:
1.1.1.22  root     11391:                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     11392:                REG16(AX) = 0x01;
                   11393:                m_CF = 1;
                   11394:                return;
                   11395:        }
                   11396:        if(REG8(AL) & 1) {
1.1       root     11397:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   11398:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     11399:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     11400:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11401:                        m_CF = 1;
1.1       root     11402:                }
1.1.1.14  root     11403:        } else {
1.1.1.21  root     11404:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     11405:                        // assume a device and use the current time
                   11406:                        GetSystemTimeAsFileTime(&time);
                   11407:                }
                   11408:                FileTimeToLocalFileTime(&time, &local);
                   11409:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     11410:        }
                   11411: }
                   11412: 
                   11413: inline void msdos_int_21h_58h()
                   11414: {
                   11415:        switch(REG8(AL)) {
                   11416:        case 0x00:
1.1.1.7   root     11417:                REG16(AX) = malloc_strategy;
                   11418:                break;
                   11419:        case 0x01:
1.1.1.24  root     11420: //             switch(REG16(BX)) {
                   11421:                switch(REG8(BL)) {
1.1.1.7   root     11422:                case 0x0000:
                   11423:                case 0x0001:
                   11424:                case 0x0002:
                   11425:                case 0x0040:
                   11426:                case 0x0041:
                   11427:                case 0x0042:
                   11428:                case 0x0080:
                   11429:                case 0x0081:
                   11430:                case 0x0082:
                   11431:                        malloc_strategy = REG16(BX);
1.1.1.23  root     11432:                        msdos_sda_update(current_psp);
1.1.1.7   root     11433:                        break;
                   11434:                default:
1.1.1.22  root     11435:                        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     11436:                        REG16(AX) = 0x01;
                   11437:                        m_CF = 1;
                   11438:                        break;
                   11439:                }
                   11440:                break;
                   11441:        case 0x02:
1.1.1.19  root     11442:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     11443:                break;
                   11444:        case 0x03:
1.1.1.24  root     11445: //             switch(REG16(BX)) {
                   11446:                switch(REG8(BL)) {
1.1.1.7   root     11447:                case 0x0000:
1.1.1.19  root     11448:                        msdos_mem_unlink_umb();
                   11449:                        break;
1.1.1.7   root     11450:                case 0x0001:
1.1.1.19  root     11451:                        msdos_mem_link_umb();
1.1.1.7   root     11452:                        break;
                   11453:                default:
1.1.1.22  root     11454:                        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     11455:                        REG16(AX) = 0x01;
                   11456:                        m_CF = 1;
                   11457:                        break;
                   11458:                }
1.1       root     11459:                break;
                   11460:        default:
1.1.1.22  root     11461:                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     11462:                REG16(AX) = 0x01;
1.1.1.3   root     11463:                m_CF = 1;
1.1       root     11464:                break;
                   11465:        }
                   11466: }
                   11467: 
                   11468: inline void msdos_int_21h_59h()
                   11469: {
1.1.1.23  root     11470:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   11471:        
                   11472:        REG16(AX) = sda->extended_error_code;
1.1.1.43  root     11473:        REG8(BL)  = sda->suggested_action;
                   11474:        REG8(BH)  = sda->error_class;
                   11475:        REG8(CL)  = sda->byte_reserved_1;
                   11476:        REG8(CH)  = sda->locus_of_last_error;
                   11477:        REG16(DX) = sda->word_reserved_1;
                   11478:        REG16(SI) = sda->word_reserved_2;
                   11479:        REG16(DI) = sda->last_error_pointer.w.l;
                   11480:        SREG(DS)  = sda->word_reserved_3;
                   11481:        i386_load_segment_descriptor(DS);
                   11482:        SREG(ES)  = sda->last_error_pointer.w.h;
                   11483:        i386_load_segment_descriptor(ES);
1.1       root     11484: }
                   11485: 
                   11486: inline void msdos_int_21h_5ah()
                   11487: {
1.1.1.3   root     11488:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11489:        int len = strlen(path);
                   11490:        char tmp[MAX_PATH];
                   11491:        
                   11492:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   11493:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11494:                
                   11495:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11496:                REG16(AX) = fd;
                   11497:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11498:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11499:                
                   11500:                strcpy(path, tmp);
                   11501:                int dx = REG16(DX) + len;
1.1.1.3   root     11502:                int ds = SREG(DS);
1.1       root     11503:                while(dx > 0xffff) {
                   11504:                        dx -= 0x10;
                   11505:                        ds++;
                   11506:                }
                   11507:                REG16(DX) = dx;
1.1.1.3   root     11508:                SREG(DS) = ds;
                   11509:                i386_load_segment_descriptor(DS);
1.1       root     11510:        } else {
                   11511:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11512:                m_CF = 1;
1.1       root     11513:        }
                   11514: }
                   11515: 
                   11516: inline void msdos_int_21h_5bh()
                   11517: {
1.1.1.3   root     11518:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11519:        
1.1.1.24  root     11520:        if(msdos_is_existing_file(path)) {
1.1       root     11521:                // already exists
                   11522:                REG16(AX) = 0x50;
1.1.1.3   root     11523:                m_CF = 1;
1.1       root     11524:        } else {
                   11525:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11526:                
                   11527:                if(fd != -1) {
                   11528:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11529:                        REG16(AX) = fd;
                   11530:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11531:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11532:                } else {
                   11533:                        REG16(AX) = errno;
1.1.1.3   root     11534:                        m_CF = 1;
1.1       root     11535:                }
                   11536:        }
                   11537: }
                   11538: 
                   11539: inline void msdos_int_21h_5ch()
                   11540: {
                   11541:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11542:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11543:        
1.1.1.20  root     11544:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     11545:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     11546:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     11547:                        UINT32 pos = _tell(fd);
                   11548:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   11549:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     11550:                                REG16(AX) = errno;
1.1.1.3   root     11551:                                m_CF = 1;
1.1       root     11552:                        }
1.1.1.20  root     11553:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     11554:                        
1.1       root     11555:                        // some seconds may be passed in _locking()
1.1.1.35  root     11556:                        REQUEST_HARDWRE_UPDATE();
1.1       root     11557:                } else {
                   11558:                        REG16(AX) = 0x01;
1.1.1.3   root     11559:                        m_CF = 1;
1.1       root     11560:                }
                   11561:        } else {
                   11562:                REG16(AX) = 0x06;
1.1.1.3   root     11563:                m_CF = 1;
1.1       root     11564:        }
                   11565: }
                   11566: 
1.1.1.22  root     11567: inline void msdos_int_21h_5dh()
                   11568: {
                   11569:        switch(REG8(AL)) {
                   11570:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     11571:                SREG(DS) = (SDA_TOP >> 4);
                   11572:                i386_load_segment_descriptor(DS);
                   11573:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   11574:                REG16(CX) = 0x80;
                   11575:                REG16(DX) = 0x1a;
                   11576:                break;
1.1.1.43  root     11577:        case 0x0a: // set extended error information
                   11578:                {
                   11579:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   11580:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
                   11581:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   11582:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
                   11583:                        sda->byte_reserved_1        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
                   11584:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
                   11585:                        sda->word_reserved_1        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   11586:                        sda->word_reserved_2        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
                   11587:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
                   11588:                        sda->word_reserved_3        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
                   11589:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   11590:                }
                   11591:                break;
1.1.1.23  root     11592:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     11593:                REG16(AX) = 0x01;
                   11594:                m_CF = 1;
                   11595:                break;
                   11596:        case 0x08: // set redirected printer mode
                   11597:        case 0x09: // flush redirected printer output
                   11598:                break;
                   11599:        default:
                   11600:                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));
                   11601:                REG16(AX) = 0x01;
                   11602:                m_CF = 1;
                   11603:                break;
                   11604:        }
                   11605: }
                   11606: 
1.1.1.42  root     11607: inline void msdos_int_21h_5eh()
                   11608: {
                   11609:        switch(REG8(AL)) {
                   11610:        case 0x00:
                   11611:                {
                   11612:                        char name[256] = {0};
                   11613:                        DWORD dwSize = 256;
                   11614:                        
                   11615:                        if(GetComputerName(name, &dwSize)) {
                   11616:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11617:                                for(int i = 0; i < 15; i++) {
                   11618:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   11619:                                }
                   11620:                                dest[15] = '\0';
                   11621:                                REG8(CH) = 0x01; // nonzero valid
                   11622:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   11623:                        } else {
                   11624:                                REG16(AX) = 0x01;
                   11625:                                m_CF = 1;
                   11626:                        }
                   11627:                }
                   11628:                break;
                   11629:        default:
                   11630:                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));
                   11631:                REG16(AX) = 0x01;
                   11632:                m_CF = 1;
                   11633:                break;
                   11634:        }
                   11635: }
                   11636: 
1.1.1.30  root     11637: inline void msdos_int_21h_5fh()
                   11638: {
                   11639:        switch(REG8(AL)) {
1.1.1.42  root     11640:        case 0x05:
1.1.1.44! root     11641:                REG16(BP) = 0;
        !          11642:                for(int i = 0; i < 26; i++) {
        !          11643:                        if(msdos_is_remote_drive(i)) {
        !          11644:                                REG16(BP)++;
1.1.1.42  root     11645:                        }
                   11646:                }
1.1.1.30  root     11647:        case 0x02:
1.1.1.44! root     11648:                for(int i = 0, index = 0; i < 26; i++) {
        !          11649:                        if(msdos_is_remote_drive(i)) {
        !          11650:                                if(index == REG16(BX)) {
        !          11651:                                        char volume[] = "A:";
1.1.1.30  root     11652:                                        volume[0] = 'A' + i;
1.1.1.44! root     11653:                                        DWORD dwSize = 128;
        !          11654:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
        !          11655:                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
        !          11656:                                        REG8(BH) = 0x00; // valid
        !          11657:                                        REG8(BL) = 0x04; // disk drive
        !          11658:                                        REG16(CX) = 0x00; // LANtastic
        !          11659:                                        return;
1.1.1.30  root     11660:                                }
1.1.1.44! root     11661:                                index++;
1.1.1.30  root     11662:                        }
                   11663:                }
                   11664:                REG16(AX) = 0x12; // no more files
                   11665:                m_CF = 1;
                   11666:                break;
1.1.1.44! root     11667:        case 0x07:
        !          11668:                if(msdos_is_valid_drive(REG8(DL))) {
        !          11669:                        msdos_cds_update(REG8(DL));
        !          11670:                } else {
        !          11671:                        REG16(AX) = 0x0f; // invalid drive
        !          11672:                        m_CF = 1;
        !          11673:                }
        !          11674:                break;
        !          11675:        case 0x08:
        !          11676:                if(msdos_is_valid_drive(REG8(DL))) {
        !          11677:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
        !          11678:                        cds->drive_attrib = 0x0000;
        !          11679:                } else {
        !          11680:                        REG16(AX) = 0x0f; // invalid drive
        !          11681:                        m_CF = 1;
        !          11682:                }
        !          11683:                break;
1.1.1.30  root     11684:        default:
                   11685:                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));
                   11686:                REG16(AX) = 0x01;
                   11687:                m_CF = 1;
                   11688:                break;
                   11689:        }
                   11690: }
                   11691: 
1.1       root     11692: inline void msdos_int_21h_60h(int lfn)
                   11693: {
1.1.1.14  root     11694:        char full[MAX_PATH], *path;
                   11695:        
1.1       root     11696:        if(lfn) {
1.1.1.14  root     11697:                char *name;
                   11698:                *full = '\0';
1.1.1.3   root     11699:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     11700:                switch(REG8(CL)) {
                   11701:                case 1:
                   11702:                        GetShortPathName(full, full, MAX_PATH);
                   11703:                        my_strupr(full);
                   11704:                        break;
                   11705:                case 2:
                   11706:                        GetLongPathName(full, full, MAX_PATH);
                   11707:                        break;
                   11708:                }
                   11709:                path = full;
                   11710:        } else {
                   11711:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   11712:        }
                   11713:        if(*path != '\0') {
                   11714:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     11715:        } else {
1.1.1.14  root     11716:                REG16(AX) = (UINT16)GetLastError();
                   11717:                m_CF = 1;
1.1       root     11718:        }
                   11719: }
                   11720: 
                   11721: inline void msdos_int_21h_61h()
                   11722: {
                   11723:        REG8(AL) = 0;
                   11724: }
                   11725: 
                   11726: inline void msdos_int_21h_62h()
                   11727: {
                   11728:        REG16(BX) = current_psp;
                   11729: }
                   11730: 
                   11731: inline void msdos_int_21h_63h()
                   11732: {
                   11733:        switch(REG8(AL)) {
                   11734:        case 0x00:
1.1.1.3   root     11735:                SREG(DS) = (DBCS_TABLE >> 4);
                   11736:                i386_load_segment_descriptor(DS);
1.1       root     11737:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   11738:                REG8(AL) = 0x00;
                   11739:                break;
1.1.1.22  root     11740:        case 0x01: // set korean input mode
                   11741:        case 0x02: // get korean input mode
                   11742:                REG8(AL) = 0xff; // not supported
                   11743:                break;
1.1       root     11744:        default:
1.1.1.22  root     11745:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     11746:                REG16(AX) = 0x01;
1.1.1.3   root     11747:                m_CF = 1;
1.1       root     11748:                break;
                   11749:        }
                   11750: }
                   11751: 
1.1.1.25  root     11752: UINT16 get_extended_country_info(UINT8 func)
1.1       root     11753: {
1.1.1.25  root     11754:        switch(func) {
1.1.1.17  root     11755:        case 0x01:
                   11756:                if(REG16(CX) >= 5) {
1.1.1.19  root     11757:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     11758:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   11759:                                REG16(CX) = sizeof(data);
                   11760:                        ZeroMemory(data, sizeof(data));
                   11761:                        data[0] = 0x01;
                   11762:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     11763:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     11764:                        *(UINT16 *)(data + 5) = active_code_page;
                   11765:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     11766: //                     REG16(AX) = active_code_page;
1.1.1.17  root     11767:                } else {
1.1.1.25  root     11768:                        return(0x08); // insufficient memory
1.1.1.17  root     11769:                }
                   11770:                break;
                   11771:        case 0x02:
                   11772:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   11773:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   11774:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     11775: //             REG16(AX) = active_code_page;
1.1.1.17  root     11776:                REG16(CX) = 0x05;
                   11777:                break;
1.1.1.23  root     11778:        case 0x03:
                   11779:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   11780:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   11781:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     11782: //             REG16(AX) = active_code_page;
1.1.1.23  root     11783:                REG16(CX) = 0x05;
                   11784:                break;
1.1.1.17  root     11785:        case 0x04:
                   11786:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   11787:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   11788:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     11789: //             REG16(AX) = active_code_page;
1.1.1.17  root     11790:                REG16(CX) = 0x05;
                   11791:                break;
                   11792:        case 0x05:
                   11793:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   11794:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   11795:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     11796: //             REG16(AX) = active_code_page;
1.1.1.17  root     11797:                REG16(CX) = 0x05;
                   11798:                break;
                   11799:        case 0x06:
                   11800:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   11801:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   11802:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     11803: //             REG16(AX) = active_code_page;
1.1.1.17  root     11804:                REG16(CX) = 0x05;
                   11805:                break;
1.1       root     11806:        case 0x07:
1.1.1.3   root     11807:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   11808:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   11809:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     11810: //             REG16(AX) = active_code_page;
1.1       root     11811:                REG16(CX) = 0x05;
                   11812:                break;
1.1.1.25  root     11813:        default:
                   11814:                return(0x01); // function number invalid
                   11815:        }
                   11816:        return(0x00);
                   11817: }
                   11818: 
                   11819: inline void msdos_int_21h_65h()
                   11820: {
                   11821:        char tmp[0x10000];
                   11822:        
                   11823:        switch(REG8(AL)) {
1.1.1.43  root     11824:        case 0x00:
                   11825:                if(REG16(CX) >= 7) {
                   11826:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   11827:                        REG16(AX) = system_code_page;
                   11828:                } else {
                   11829:                        REG16(AX) = 0x0c;
                   11830:                        m_CF = 1;
                   11831:                }
                   11832:                break;
1.1.1.25  root     11833:        case 0x01:
                   11834:        case 0x02:
                   11835:        case 0x03:
                   11836:        case 0x04:
                   11837:        case 0x05:
                   11838:        case 0x06:
                   11839:        case 0x07:
                   11840:                {
                   11841:                        UINT16 result = get_extended_country_info(REG8(AL));
                   11842:                        if(result) {
                   11843:                                REG16(AX) = result;
                   11844:                                m_CF = 1;
                   11845:                        } else {
                   11846:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   11847:                        }
                   11848:                }
                   11849:                break;
1.1       root     11850:        case 0x20:
1.1.1.25  root     11851:        case 0xa0:
1.1.1.19  root     11852:                memset(tmp, 0, sizeof(tmp));
                   11853:                tmp[0] = REG8(DL);
1.1       root     11854:                my_strupr(tmp);
                   11855:                REG8(DL) = tmp[0];
                   11856:                break;
                   11857:        case 0x21:
1.1.1.25  root     11858:        case 0xa1:
1.1       root     11859:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     11860:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     11861:                my_strupr(tmp);
1.1.1.3   root     11862:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     11863:                break;
                   11864:        case 0x22:
1.1.1.25  root     11865:        case 0xa2:
1.1.1.3   root     11866:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     11867:                break;
1.1.1.25  root     11868:        case 0x23:
                   11869:                // FIXME: need to check multi-byte (kanji) charactre?
                   11870:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   11871:                        // 8278h/8299h: multi-byte (kanji) Y and y
                   11872:                        REG16(AX) = 0x00;
                   11873:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
                   11874:                        // 826dh/828eh: multi-byte (kanji) N and n
                   11875:                        REG16(AX) = 0x01;
                   11876:                } else {
                   11877:                        REG16(AX) = 0x02;
                   11878:                }
                   11879:                break;
1.1       root     11880:        default:
1.1.1.22  root     11881:                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     11882:                REG16(AX) = 0x01;
1.1.1.3   root     11883:                m_CF = 1;
1.1       root     11884:                break;
                   11885:        }
                   11886: }
                   11887: 
                   11888: inline void msdos_int_21h_66h()
                   11889: {
                   11890:        switch(REG8(AL)) {
                   11891:        case 0x01:
                   11892:                REG16(BX) = active_code_page;
                   11893:                REG16(DX) = system_code_page;
                   11894:                break;
                   11895:        case 0x02:
                   11896:                if(active_code_page == REG16(BX)) {
                   11897:                        REG16(AX) = 0xeb41;
                   11898:                } else if(_setmbcp(REG16(BX)) == 0) {
                   11899:                        active_code_page = REG16(BX);
1.1.1.17  root     11900:                        msdos_nls_tables_update();
1.1       root     11901:                        REG16(AX) = 0xeb41;
1.1.1.32  root     11902:                        SetConsoleCP(active_code_page);
                   11903:                        SetConsoleOutputCP(active_code_page);
1.1       root     11904:                } else {
                   11905:                        REG16(AX) = 0x25;
1.1.1.3   root     11906:                        m_CF = 1;
1.1       root     11907:                }
                   11908:                break;
                   11909:        default:
1.1.1.22  root     11910:                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     11911:                REG16(AX) = 0x01;
1.1.1.3   root     11912:                m_CF = 1;
1.1       root     11913:                break;
                   11914:        }
                   11915: }
                   11916: 
                   11917: inline void msdos_int_21h_67h()
                   11918: {
                   11919:        process_t *process = msdos_process_info_get(current_psp);
                   11920:        
                   11921:        if(REG16(BX) <= MAX_FILES) {
                   11922:                process->max_files = max(REG16(BX), 20);
                   11923:        } else {
                   11924:                REG16(AX) = 0x08;
1.1.1.3   root     11925:                m_CF = 1;
1.1       root     11926:        }
                   11927: }
                   11928: 
                   11929: inline void msdos_int_21h_68h()
                   11930: {
                   11931:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11932:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11933:        
1.1.1.20  root     11934:        if(fd < process->max_files && file_handler[fd].valid) {
                   11935:                // fflush(_fdopen(fd, ""));
1.1       root     11936:        } else {
                   11937:                REG16(AX) = 0x06;
1.1.1.3   root     11938:                m_CF = 1;
1.1       root     11939:        }
                   11940: }
                   11941: 
                   11942: inline void msdos_int_21h_69h()
                   11943: {
1.1.1.3   root     11944:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11945:        char path[] = "A:\\";
                   11946:        char volume_label[MAX_PATH];
                   11947:        DWORD serial_number = 0;
                   11948:        char file_system[MAX_PATH];
                   11949:        
                   11950:        if(REG8(BL) == 0) {
                   11951:                path[0] = 'A' + _getdrive() - 1;
                   11952:        } else {
                   11953:                path[0] = 'A' + REG8(BL) - 1;
                   11954:        }
                   11955:        
                   11956:        switch(REG8(AL)) {
                   11957:        case 0x00:
                   11958:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   11959:                        info->info_level = 0;
                   11960:                        info->serial_number = serial_number;
                   11961:                        memset(info->volume_label, 0x20, 11);
                   11962:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   11963:                        memset(info->file_system, 0x20, 8);
                   11964:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   11965:                } else {
                   11966:                        REG16(AX) = errno;
1.1.1.3   root     11967:                        m_CF = 1;
1.1       root     11968:                }
                   11969:                break;
                   11970:        case 0x01:
                   11971:                REG16(AX) = 0x03;
1.1.1.3   root     11972:                m_CF = 1;
1.1       root     11973:        }
                   11974: }
                   11975: 
                   11976: inline void msdos_int_21h_6ah()
                   11977: {
                   11978:        REG8(AH) = 0x68;
                   11979:        msdos_int_21h_68h();
                   11980: }
                   11981: 
                   11982: inline void msdos_int_21h_6bh()
                   11983: {
                   11984:        REG8(AL) = 0;
                   11985: }
                   11986: 
                   11987: inline void msdos_int_21h_6ch(int lfn)
                   11988: {
1.1.1.3   root     11989:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     11990:        int mode = REG8(BL) & 0x03;
                   11991:        
                   11992:        if(mode < 0x03) {
1.1.1.29  root     11993:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     11994:                        // file exists
                   11995:                        if(REG8(DL) & 1) {
1.1.1.37  root     11996:                                int fd = -1;
1.1.1.11  root     11997:                                UINT16 info;
1.1.1.37  root     11998:                                int sio_port = 0;
                   11999:                                int lpt_port = 0;
1.1       root     12000:                                
1.1.1.11  root     12001:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     12002:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     12003:                                        info = 0x80d3;
1.1.1.37  root     12004:                                } else if((sio_port = msdos_is_comm_path(path)) != 0) {
                   12005:                                        fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14  root     12006:                                        info = 0x80d3;
1.1.1.37  root     12007:                                        msdos_set_comm_params(sio_port, path);
                   12008:                                } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
                   12009:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   12010:                                        info = 0xa8c0;
1.1.1.29  root     12011:                                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     12012:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   12013:                                        info = 0x80d3;
1.1.1.11  root     12014:                                } else {
1.1.1.13  root     12015:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     12016:                                        info = msdos_drive_number(path);
                   12017:                                }
1.1       root     12018:                                if(fd != -1) {
                   12019:                                        REG16(AX) = fd;
                   12020:                                        REG16(CX) = 1;
1.1.1.37  root     12021:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     12022:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12023:                                } else {
                   12024:                                        REG16(AX) = errno;
1.1.1.3   root     12025:                                        m_CF = 1;
1.1       root     12026:                                }
                   12027:                        } else if(REG8(DL) & 2) {
                   12028:                                int attr = GetFileAttributes(path);
1.1.1.37  root     12029:                                int fd = -1;
1.1.1.11  root     12030:                                UINT16 info;
1.1.1.37  root     12031:                                int sio_port = 0;
                   12032:                                int lpt_port = 0;
1.1       root     12033:                                
1.1.1.11  root     12034:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     12035:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     12036:                                        info = 0x80d3;
1.1.1.37  root     12037:                                } else if((sio_port = msdos_is_comm_path(path)) != 0) {
                   12038:                                        fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14  root     12039:                                        info = 0x80d3;
1.1.1.37  root     12040:                                        msdos_set_comm_params(sio_port, path);
                   12041:                                } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
                   12042:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   12043:                                        info = 0xa8c0;
1.1.1.29  root     12044:                                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     12045:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   12046:                                        info = 0x80d3;
1.1       root     12047:                                } else {
                   12048:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     12049:                                        info = msdos_drive_number(path);
1.1       root     12050:                                }
                   12051:                                if(fd != -1) {
                   12052:                                        if(attr == -1) {
                   12053:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   12054:                                        }
                   12055:                                        SetFileAttributes(path, attr);
                   12056:                                        REG16(AX) = fd;
                   12057:                                        REG16(CX) = 3;
1.1.1.37  root     12058:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     12059:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12060:                                } else {
                   12061:                                        REG16(AX) = errno;
1.1.1.3   root     12062:                                        m_CF = 1;
1.1       root     12063:                                }
                   12064:                        } else {
                   12065:                                REG16(AX) = 0x50;
1.1.1.3   root     12066:                                m_CF = 1;
1.1       root     12067:                        }
                   12068:                } else {
                   12069:                        // file not exists
                   12070:                        if(REG8(DL) & 0x10) {
                   12071:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12072:                                
                   12073:                                if(fd != -1) {
                   12074:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12075:                                        REG16(AX) = fd;
                   12076:                                        REG16(CX) = 2;
                   12077:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12078:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12079:                                } else {
                   12080:                                        REG16(AX) = errno;
1.1.1.3   root     12081:                                        m_CF = 1;
1.1       root     12082:                                }
                   12083:                        } else {
                   12084:                                REG16(AX) = 0x02;
1.1.1.3   root     12085:                                m_CF = 1;
1.1       root     12086:                        }
                   12087:                }
                   12088:        } else {
                   12089:                REG16(AX) = 0x0c;
1.1.1.3   root     12090:                m_CF = 1;
1.1       root     12091:        }
                   12092: }
                   12093: 
1.1.1.43  root     12094: inline void msdos_int_21h_70h()
                   12095: {
                   12096:        switch(REG8(AL)) {
                   12097:        case 0x02:
                   12098:                if(REG16(CX) >= 7) {
                   12099:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   12100:                        msdos_nls_tables_update();
                   12101:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   12102:                        REG16(AX) = system_code_page;
                   12103:                } else {
                   12104:                        REG16(AX) = 0x0c;
                   12105:                        m_CF = 1;
                   12106:                }
                   12107:                break;
                   12108:        default:
                   12109:                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));
                   12110:                REG16(AX) = 0x01;
                   12111:                m_CF = 1;
                   12112:                break;
                   12113:        }
                   12114: }
                   12115: 
1.1       root     12116: inline void msdos_int_21h_710dh()
                   12117: {
                   12118:        // reset drive
                   12119: }
                   12120: 
1.1.1.17  root     12121: inline void msdos_int_21h_7141h(int lfn)
                   12122: {
                   12123:        if(REG16(SI) == 0) {
                   12124:                msdos_int_21h_41h(lfn);
                   12125:                return;
                   12126:        }
                   12127:        if(REG16(SI) != 1) {
                   12128:                REG16(AX) = 5;
                   12129:                m_CF = 1;
                   12130:        }
                   12131:        /* wild card and matching attributes... */
                   12132:        char tmp[MAX_PATH * 2];
                   12133:        // copy search pathname (and quick check overrun)
                   12134:        ZeroMemory(tmp, sizeof(tmp));
                   12135:        tmp[MAX_PATH - 1] = '\0';
                   12136:        tmp[MAX_PATH] = 1;
                   12137:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   12138:        
                   12139:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   12140:                REG16(AX) = 1;
                   12141:                m_CF = 1;
                   12142:                return;
                   12143:        }
                   12144:        for(char *s = tmp; *s; ++s) {
                   12145:                if(*s == '/') {
                   12146:                        *s = '\\';
                   12147:                }
                   12148:        }
                   12149:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   12150:        if(tmp_name) {
                   12151:                ++tmp_name;
                   12152:        } else {
                   12153:                tmp_name = strchr(tmp, ':');
                   12154:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   12155:        }
                   12156:        
                   12157:        WIN32_FIND_DATAA fd;
                   12158:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   12159:        if(fh == INVALID_HANDLE_VALUE) {
                   12160:                REG16(AX) = 2;
                   12161:                m_CF = 1;
                   12162:                return;
                   12163:        }
                   12164:        do {
                   12165:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   12166:                        strcpy(tmp_name, fd.cFileName);
                   12167:                        if(remove(msdos_trimmed_path(tmp, lfn))) {
                   12168:                                REG16(AX) = 5;
                   12169:                                m_CF = 1;
                   12170:                                break;
                   12171:                        }
                   12172:                }
                   12173:        } while(FindNextFileA(fh, &fd));
                   12174:        if(!m_CF) {
                   12175:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   12176:                        m_CF = 1;
                   12177:                        REG16(AX) = 2;
                   12178:                }
                   12179:        }
                   12180:        FindClose(fh);
                   12181: }
                   12182: 
1.1       root     12183: inline void msdos_int_21h_714eh()
                   12184: {
                   12185:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12186:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   12187:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12188:        WIN32_FIND_DATA fd;
                   12189:        
1.1.1.13  root     12190:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12191:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12192:                FindClose(dtainfo->find_handle);
                   12193:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12194:        }
                   12195:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12196:        dtainfo->allowable_mask = REG8(CL);
                   12197:        dtainfo->required_mask = REG8(CH);
                   12198:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12199:        
1.1.1.14  root     12200:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12201:                dtainfo->allowable_mask &= ~8;
1.1       root     12202:        }
1.1.1.14  root     12203:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   12204:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12205:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12206:                                FindClose(dtainfo->find_handle);
                   12207:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12208:                                break;
                   12209:                        }
                   12210:                }
                   12211:        }
1.1.1.13  root     12212:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12213:                find->attrib = fd.dwFileAttributes;
                   12214:                msdos_find_file_conv_local_time(&fd);
                   12215:                if(REG16(SI) == 0) {
                   12216:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12217:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12218:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12219:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12220:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12221:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12222:                } else {
                   12223:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12224:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12225:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12226:                }
                   12227:                find->size_hi = fd.nFileSizeHigh;
                   12228:                find->size_lo = fd.nFileSizeLow;
                   12229:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12230:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12231:                REG16(AX) = dtainfo - dtalist + 1;
                   12232:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12233:                // volume label
                   12234:                find->attrib = 8;
                   12235:                find->size_hi = find->size_lo = 0;
                   12236:                strcpy(find->full_name, process->volume_label);
                   12237:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12238:                dtainfo->allowable_mask &= ~8;
                   12239:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     12240:        } else {
                   12241:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12242:                m_CF = 1;
1.1       root     12243:        }
                   12244: }
                   12245: 
                   12246: inline void msdos_int_21h_714fh()
                   12247: {
                   12248:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12249:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     12250:        WIN32_FIND_DATA fd;
                   12251:        
1.1.1.14  root     12252:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12253:                REG16(AX) = 6;
1.1.1.13  root     12254:                m_CF = 1;
                   12255:                return;
                   12256:        }
1.1.1.14  root     12257:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12258:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12259:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     12260:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12261:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12262:                                        FindClose(dtainfo->find_handle);
                   12263:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12264:                                        break;
                   12265:                                }
                   12266:                        }
                   12267:                } else {
1.1.1.13  root     12268:                        FindClose(dtainfo->find_handle);
                   12269:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12270:                }
                   12271:        }
1.1.1.13  root     12272:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12273:                find->attrib = fd.dwFileAttributes;
                   12274:                msdos_find_file_conv_local_time(&fd);
                   12275:                if(REG16(SI) == 0) {
                   12276:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12277:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12278:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12279:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12280:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12281:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12282:                } else {
                   12283:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12284:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12285:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12286:                }
                   12287:                find->size_hi = fd.nFileSizeHigh;
                   12288:                find->size_lo = fd.nFileSizeLow;
                   12289:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12290:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12291:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12292:                // volume label
                   12293:                find->attrib = 8;
                   12294:                find->size_hi = find->size_lo = 0;
                   12295:                strcpy(find->full_name, process->volume_label);
                   12296:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12297:                dtainfo->allowable_mask &= ~8;
1.1       root     12298:        } else {
                   12299:                REG16(AX) = 0x12;
1.1.1.3   root     12300:                m_CF = 1;
1.1       root     12301:        }
                   12302: }
                   12303: 
                   12304: inline void msdos_int_21h_71a0h()
                   12305: {
                   12306:        DWORD max_component_len, file_sys_flag;
                   12307:        
1.1.1.14  root     12308:        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))) {
                   12309:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   12310:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     12311:                REG16(CX) = (UINT16)max_component_len;          // 255
                   12312:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   12313:        } else {
                   12314:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12315:                m_CF = 1;
1.1       root     12316:        }
                   12317: }
                   12318: 
                   12319: inline void msdos_int_21h_71a1h()
                   12320: {
1.1.1.14  root     12321:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12322:                REG16(AX) = 6;
1.1.1.13  root     12323:                m_CF = 1;
                   12324:                return;
                   12325:        }
1.1.1.14  root     12326:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12327:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12328:                FindClose(dtainfo->find_handle);
                   12329:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12330:        }
                   12331: }
                   12332: 
                   12333: inline void msdos_int_21h_71a6h()
                   12334: {
                   12335:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12336:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   12337:        
1.1.1.3   root     12338:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12339:        struct _stat64 status;
                   12340:        DWORD serial_number = 0;
                   12341:        
1.1.1.20  root     12342:        if(fd < process->max_files && file_handler[fd].valid) {
                   12343:                if(_fstat64(fd, &status) == 0) {
                   12344:                        if(file_handler[fd].path[1] == ':') {
1.1       root     12345:                                // NOTE: we need to consider the network file path "\\host\share\"
                   12346:                                char volume[] = "A:\\";
1.1.1.20  root     12347:                                volume[0] = file_handler[fd].path[1];
1.1       root     12348:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   12349:                        }
1.1.1.20  root     12350:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     12351:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   12352:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   12353:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   12354:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   12355:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   12356:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   12357:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   12358:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   12359:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   12360:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     12361:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     12362:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     12363:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     12364:                } else {
                   12365:                        REG16(AX) = errno;
1.1.1.3   root     12366:                        m_CF = 1;
1.1       root     12367:                }
                   12368:        } else {
                   12369:                REG16(AX) = 0x06;
1.1.1.3   root     12370:                m_CF = 1;
1.1       root     12371:        }
                   12372: }
                   12373: 
                   12374: inline void msdos_int_21h_71a7h()
                   12375: {
                   12376:        switch(REG8(BL)) {
                   12377:        case 0x00:
1.1.1.3   root     12378:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     12379:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12380:                        m_CF = 1;
1.1       root     12381:                }
                   12382:                break;
                   12383:        case 0x01:
                   12384:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     12385:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     12386:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12387:                        m_CF = 1;
1.1       root     12388:                }
                   12389:                break;
                   12390:        default:
1.1.1.22  root     12391:                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     12392:                REG16(AX) = 0x01;
1.1.1.3   root     12393:                m_CF = 1;
1.1       root     12394:                break;
                   12395:        }
                   12396: }
                   12397: 
                   12398: inline void msdos_int_21h_71a8h()
                   12399: {
                   12400:        if(REG8(DH) == 0) {
                   12401:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     12402:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     12403:                memset(fcb, 0x20, sizeof(fcb));
                   12404:                int len = strlen(tmp);
1.1.1.21  root     12405:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     12406:                        if(tmp[i] == '.') {
                   12407:                                pos = 8;
                   12408:                        } else {
                   12409:                                if(msdos_lead_byte_check(tmp[i])) {
                   12410:                                        fcb[pos++] = tmp[i++];
                   12411:                                }
                   12412:                                fcb[pos++] = tmp[i];
                   12413:                        }
                   12414:                }
1.1.1.3   root     12415:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     12416:        } else {
1.1.1.3   root     12417:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     12418:        }
                   12419: }
                   12420: 
1.1.1.22  root     12421: inline void msdos_int_21h_71aah()
                   12422: {
                   12423:        char drv[] = "A:", path[MAX_PATH];
                   12424:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12425:        
                   12426:        if(REG8(BL) == 0) {
                   12427:                drv[0] = 'A' + _getdrive() - 1;
                   12428:        } else {
                   12429:                drv[0] = 'A' + REG8(BL) - 1;
                   12430:        }
                   12431:        switch(REG8(BH)) {
                   12432:        case 0x00:
1.1.1.44! root     12433:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
        !          12434:                        REG16(AX) = 0x0f; // invalid drive
        !          12435:                        m_CF = 1;
        !          12436:                } else if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
        !          12437:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     12438:                        m_CF = 1;
                   12439:                }
                   12440:                break;
                   12441:        case 0x01:
1.1.1.44! root     12442:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
        !          12443:                        REG16(AX) = 0x0f; // invalid drive
        !          12444:                        m_CF = 1;
        !          12445:                } else if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     12446:                        REG16(AX) = 0x0f; // invalid drive
                   12447:                        m_CF = 1;
                   12448:                }
                   12449:                break;
                   12450:        case 0x02:
1.1.1.44! root     12451:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
        !          12452:                        REG16(AX) = 0x0f; // invalid drive
        !          12453:                        m_CF = 1;
        !          12454:                } else if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     12455:                        REG16(AX) = 0x0f; // invalid drive
                   12456:                        m_CF = 1;
                   12457:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   12458:                        REG16(AX) = 0x0f; // invalid drive
                   12459:                        m_CF = 1;
                   12460:                } else {
                   12461:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   12462:                }
                   12463:                break;
                   12464:        default:
                   12465:                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));
                   12466:                REG16(AX) = 0x01;
                   12467:                m_CF = 1;
                   12468:                break;
                   12469:        }
                   12470: }
                   12471: 
1.1.1.14  root     12472: inline void msdos_int_21h_7300h()
                   12473: {
1.1.1.44! root     12474:        REG8(AL) = REG8(CL);
        !          12475:        REG8(AH) = 0;
1.1.1.14  root     12476: }
                   12477: 
                   12478: inline void msdos_int_21h_7302h()
                   12479: {
                   12480:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   12481:        UINT16 seg, ofs;
                   12482:        
                   12483:        if(REG16(CX) < 0x3f) {
                   12484:                REG8(AL) = 0x18;
                   12485:                m_CF = 1;
                   12486:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   12487:                REG8(AL) = 0xff;
                   12488:                m_CF = 1;
                   12489:        } else {
                   12490:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   12491:        }
                   12492: }
                   12493: 
1.1       root     12494: inline void msdos_int_21h_7303h()
                   12495: {
1.1.1.3   root     12496:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12497:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     12498:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   12499:        
                   12500:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   12501:                info->size_of_structure = sizeof(ext_space_info_t);
                   12502:                info->structure_version = 0;
                   12503:                info->sectors_per_cluster = sectors_per_cluster;
                   12504:                info->bytes_per_sector = bytes_per_sector;
                   12505:                info->available_clusters_on_drive = free_clusters;
                   12506:                info->total_clusters_on_drive = total_clusters;
                   12507:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   12508:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   12509:                info->available_allocation_units = free_clusters;       // ???
                   12510:                info->total_allocation_units = total_clusters;          // ???
                   12511:        } else {
                   12512:                REG16(AX) = errno;
1.1.1.3   root     12513:                m_CF = 1;
1.1       root     12514:        }
                   12515: }
                   12516: 
1.1.1.30  root     12517: inline void msdos_int_21h_dbh()
                   12518: {
                   12519:        // Novell NetWare - Workstation - Get Number of Local Drives
                   12520:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   12521:        REG8(AL) = dos_info->last_drive;
                   12522: }
                   12523: 
                   12524: inline void msdos_int_21h_dch()
                   12525: {
                   12526:        // Novell NetWare - Connection Services - Get Connection Number
                   12527:        REG8(AL) = 0x00;
                   12528: }
                   12529: 
1.1.1.32  root     12530: inline void msdos_int_24h()
                   12531: {
                   12532:        const char *message = NULL;
                   12533:        int key = 0;
                   12534:        
                   12535:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   12536:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   12537:                        if(active_code_page == 932) {
                   12538:                                message = critical_error_table[i].message_japanese;
                   12539:                        }
                   12540:                        if(message == NULL) {
                   12541:                                message = critical_error_table[i].message_english;
                   12542:                        }
                   12543:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   12544:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   12545:                        
                   12546:                        SREG(ES) = WORK_TOP >> 4;
                   12547:                        i386_load_segment_descriptor(ES);
                   12548:                        REG16(DI) = 0x0000;
                   12549:                        break;
                   12550:                }
                   12551:        }
                   12552:        fprintf(stderr, "\n%s", message);
                   12553:        if(!(REG8(AH) & 0x80)) {
                   12554:                if(REG8(AH) & 0x01) {
                   12555:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   12556:                } else {
                   12557:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   12558:                }
                   12559:        }
                   12560:        fprintf(stderr, "\n");
                   12561:        
1.1.1.33  root     12562:        {
1.1.1.32  root     12563:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     12564:        }
1.1.1.32  root     12565:        if(REG8(AH) & 0x10) {
                   12566:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   12567:        }
                   12568:        if(REG8(AH) & 0x20) {
                   12569:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   12570:        }
                   12571:        if(REG8(AH) & 0x08) {
                   12572:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   12573:        }
                   12574:        fprintf(stderr, "? ");
                   12575:        
                   12576:        while(1) {
                   12577:                while(!_kbhit()) {
                   12578:                        Sleep(10);
                   12579:                }
                   12580:                key = _getch();
                   12581:                
                   12582:                if(key == 'I' || key == 'i') {
                   12583:                        if(REG8(AH) & 0x20) {
                   12584:                                REG8(AL) = 0;
                   12585:                                break;
                   12586:                        }
                   12587:                } else if(key == 'R' || key == 'r') {
                   12588:                        if(REG8(AH) & 0x10) {
                   12589:                                REG8(AL) = 1;
                   12590:                                break;
                   12591:                        }
                   12592:                } else if(key == 'A' || key == 'a') {
                   12593:                        REG8(AL) = 2;
                   12594:                        break;
                   12595:                } else if(key == 'F' || key == 'f') {
                   12596:                        if(REG8(AH) & 0x08) {
                   12597:                                REG8(AL) = 3;
                   12598:                                break;
                   12599:                        }
                   12600:                }
                   12601:        }
                   12602:        fprintf(stderr, "%c\n", key);
                   12603: }
                   12604: 
1.1       root     12605: inline void msdos_int_25h()
                   12606: {
                   12607:        UINT16 seg, ofs;
                   12608:        DWORD dwSize;
                   12609:        
1.1.1.3   root     12610: #if defined(HAS_I386)
                   12611:        I386OP(pushf)();
                   12612: #else
                   12613:        PREFIX86(_pushf());
                   12614: #endif
1.1       root     12615:        
                   12616:        if(!(REG8(AL) < 26)) {
                   12617:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     12618:                m_CF = 1;
1.1       root     12619:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   12620:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12621:                m_CF = 1;
1.1       root     12622:        } else {
                   12623:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   12624:                char dev[64];
                   12625:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   12626:                
                   12627:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   12628:                if(hFile == INVALID_HANDLE_VALUE) {
                   12629:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12630:                        m_CF = 1;
1.1       root     12631:                } else {
1.1.1.19  root     12632:                        UINT32 top_sector  = REG16(DX);
                   12633:                        UINT16 sector_num  = REG16(CX);
                   12634:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   12635:                        
                   12636:                        if(sector_num == 0xffff) {
                   12637:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   12638:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   12639:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   12640:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   12641:                                buffer_addr = (seg << 4) + ofs;
                   12642:                        }
                   12643: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   12644: //                             REG8(AL) = 0x02; // drive not ready
                   12645: //                             m_CF = 1;
                   12646: //                     } else 
                   12647:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     12648:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     12649:                                m_CF = 1;
1.1.1.19  root     12650:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     12651:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     12652:                                m_CF = 1;
1.1       root     12653:                        }
                   12654:                        CloseHandle(hFile);
                   12655:                }
                   12656:        }
                   12657: }
                   12658: 
                   12659: inline void msdos_int_26h()
                   12660: {
1.1.1.42  root     12661:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     12662:        UINT16 seg, ofs;
                   12663:        DWORD dwSize;
                   12664:        
1.1.1.3   root     12665: #if defined(HAS_I386)
                   12666:        I386OP(pushf)();
                   12667: #else
                   12668:        PREFIX86(_pushf());
                   12669: #endif
1.1       root     12670:        
                   12671:        if(!(REG8(AL) < 26)) {
                   12672:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     12673:                m_CF = 1;
1.1       root     12674:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   12675:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12676:                m_CF = 1;
1.1       root     12677:        } else {
                   12678:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   12679:                char dev[64];
                   12680:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   12681:                
                   12682:                if(dpb->media_type == 0xf8) {
                   12683:                        // this drive is not a floppy
1.1.1.6   root     12684: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   12685: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   12686: //                     }
1.1       root     12687:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12688:                        m_CF = 1;
1.1       root     12689:                } else {
                   12690:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   12691:                        if(hFile == INVALID_HANDLE_VALUE) {
                   12692:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12693:                                m_CF = 1;
1.1       root     12694:                        } else {
1.1.1.19  root     12695:                                UINT32 top_sector  = REG16(DX);
                   12696:                                UINT16 sector_num  = REG16(CX);
                   12697:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   12698:                                
                   12699:                                if(sector_num == 0xffff) {
                   12700:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   12701:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   12702:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   12703:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   12704:                                        buffer_addr = (seg << 4) + ofs;
                   12705:                                }
1.1       root     12706:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   12707:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12708:                                        m_CF = 1;
1.1.1.19  root     12709:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     12710:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     12711:                                        m_CF = 1;
1.1.1.19  root     12712:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     12713:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     12714:                                        m_CF = 1;
1.1       root     12715:                                }
                   12716:                                CloseHandle(hFile);
                   12717:                        }
                   12718:                }
                   12719:        }
                   12720: }
                   12721: 
                   12722: inline void msdos_int_27h()
                   12723: {
1.1.1.29  root     12724:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   12725:        try {
                   12726:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   12727:        } catch(...) {
                   12728:                // recover the broken mcb
                   12729:                int mcb_seg = SREG(CS) - 1;
                   12730:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     12731:                
1.1.1.29  root     12732:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     12733:                        mcb->mz = 'M';
                   12734:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   12735:                        
1.1.1.29  root     12736:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     12737:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     12738:                        } else {
1.1.1.39  root     12739:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     12740:                        }
                   12741:                } else {
                   12742:                        mcb->mz = 'Z';
1.1.1.30  root     12743:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     12744:                }
                   12745:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   12746:        }
1.1.1.3   root     12747:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     12748: }
                   12749: 
                   12750: inline void msdos_int_29h()
                   12751: {
1.1.1.14  root     12752: #if 1
                   12753:        // need to check escape sequences
1.1       root     12754:        msdos_putch(REG8(AL));
1.1.1.14  root     12755: #else
                   12756:        DWORD num;
                   12757:        vram_flush();
1.1.1.23  root     12758:        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     12759:        cursor_moved = true;
                   12760: #endif
1.1       root     12761: }
                   12762: 
                   12763: inline void msdos_int_2eh()
                   12764: {
                   12765:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   12766:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12767:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     12768:        char *token = my_strtok(tmp, " ");
                   12769:        strcpy(command, token);
                   12770:        strcpy(opt, token + strlen(token) + 1);
                   12771:        
                   12772:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   12773:        param->env_seg = 0;
                   12774:        param->cmd_line.w.l = 44;
                   12775:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   12776:        param->fcb1.w.l = 24;
                   12777:        param->fcb1.w.h = (WORK_TOP >> 4);
                   12778:        param->fcb2.w.l = 24;
                   12779:        param->fcb2.w.h = (WORK_TOP >> 4);
                   12780:        
                   12781:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   12782:        
                   12783:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   12784:        cmd_line->len = strlen(opt);
                   12785:        strcpy(cmd_line->cmd, opt);
                   12786:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   12787:        
1.1.1.28  root     12788:        try {
                   12789:                if(msdos_process_exec(command, param, 0)) {
                   12790:                        REG16(AX) = 0xffff; // error before processing command
                   12791:                } else {
                   12792:                        // set flag to set retval to ax when the started process is terminated
                   12793:                        process_t *process = msdos_process_info_get(current_psp);
                   12794:                        process->called_by_int2eh = true;
                   12795:                }
                   12796:        } catch(...) {
                   12797:                REG16(AX) = 0xffff; // error before processing command
                   12798:        }
1.1       root     12799: }
                   12800: 
1.1.1.29  root     12801: inline void msdos_int_2fh_05h()
                   12802: {
                   12803:        switch(REG8(AL)) {
                   12804:        case 0x00:
1.1.1.32  root     12805:                REG8(AL) = 0xff;
                   12806:                break;
                   12807:        case 0x01:
                   12808:        case 0x02:
                   12809:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   12810:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   12811:                                const char *message = NULL;
                   12812:                                if(active_code_page == 932) {
                   12813:                                        message = standard_error_table[i].message_japanese;
                   12814:                                }
                   12815:                                if(message == NULL) {
                   12816:                                        message = standard_error_table[i].message_english;
                   12817:                                }
                   12818:                                strcpy((char *)(mem + WORK_TOP), message);
                   12819:                                
                   12820:                                SREG(ES) = WORK_TOP >> 4;
                   12821:                                i386_load_segment_descriptor(ES);
                   12822:                                REG16(DI) = 0x0000;
                   12823:                                REG8(AL) = 0x01;
                   12824:                                break;
                   12825:                        }
                   12826:                }
1.1.1.29  root     12827:                break;
                   12828:        default:
                   12829:                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));
                   12830:                m_CF = 1;
                   12831:        }
                   12832: }
                   12833: 
1.1.1.44! root     12834: inline void msdos_int_2fh_06h()
        !          12835: {
        !          12836:        switch(REG8(AL)) {
        !          12837:        case 0x00:
        !          12838:                // ASSIGN is not installed
        !          12839:                break;
        !          12840:        case 0x01:
        !          12841:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
        !          12842:                REG16(AX) = 0x01;
        !          12843:                m_CF = 1;
        !          12844:                break;
        !          12845:        default:
        !          12846:                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));
        !          12847:                REG16(AX) = 0x01;
        !          12848:                m_CF = 1;
        !          12849:                break;
        !          12850:        }
        !          12851: }
        !          12852: 
1.1.1.22  root     12853: inline void msdos_int_2fh_11h()
                   12854: {
                   12855:        switch(REG8(AL)) {
                   12856:        case 0x00:
1.1.1.29  root     12857:                if(i386_read_stack() == 0xdada) {
                   12858:                        // MSCDEX is not installed
                   12859: //                     REG8(AL) = 0x00;
                   12860:                } else {
                   12861:                        // Network Redirector is not installed
                   12862: //                     REG8(AL) = 0x00;
                   12863:                }
1.1.1.22  root     12864:                break;
                   12865:        default:
1.1.1.43  root     12866: //             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     12867:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     12868:                m_CF = 1;
                   12869:                break;
                   12870:        }
                   12871: }
                   12872: 
1.1.1.21  root     12873: inline void msdos_int_2fh_12h()
                   12874: {
                   12875:        switch(REG8(AL)) {
1.1.1.22  root     12876:        case 0x00:
1.1.1.29  root     12877:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     12878:                REG8(AL) = 0xff;
                   12879:                break;
1.1.1.29  root     12880: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   12881:        case 0x02:
                   12882:                {
                   12883:                        UINT16 stack = i386_read_stack();
                   12884:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   12885:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   12886:                        i386_load_segment_descriptor(ES);
                   12887:                }
                   12888:                break;
1.1.1.30  root     12889:        case 0x03:
                   12890:                SREG(DS) = (DEVICE_TOP >> 4);
                   12891:                i386_load_segment_descriptor(DS);
                   12892:                break;
1.1.1.29  root     12893:        case 0x04:
                   12894:                {
                   12895:                        UINT16 stack = i386_read_stack();
                   12896:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   12897: #if defined(HAS_I386)
                   12898:                        m_ZF = (REG8(AL) == '\\');
                   12899: #else
                   12900:                        m_ZeroVal = (REG8(AL) != '\\');
                   12901: #endif
                   12902:                }
                   12903:                break;
                   12904:        case 0x05:
                   12905:                msdos_putch(i386_read_stack());
                   12906:                break;
                   12907: //     case 0x06: // DOS 3.0+ internal - Invole Critical Error
                   12908: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   12909: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
                   12910: //     case 0x09: // DOS 3.0+ internal - Flush and FREE Disk Buffer
                   12911: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   12912: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   12913: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   12914:        case 0x0d:
                   12915:                {
                   12916:                        SYSTEMTIME time;
                   12917:                        FILETIME file_time;
                   12918:                        WORD dos_date, dos_time;
                   12919:                        GetLocalTime(&time);
                   12920:                        SystemTimeToFileTime(&time, &file_time);
                   12921:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   12922:                        REG16(AX) = dos_date;
                   12923:                        REG16(DX) = dos_time;
                   12924:                }
                   12925:                break;
                   12926: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   12927: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   12928: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   12929:        case 0x11:
                   12930:                {
                   12931:                        char path[MAX_PATH], *p;
                   12932:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12933:                        my_strupr(path);
                   12934:                        while((p = my_strchr(path, '/')) != NULL) {
                   12935:                                *p = '\\';
                   12936:                        }
                   12937:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   12938:                }
                   12939:                break;
                   12940:        case 0x12:
                   12941:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   12942:                break;
                   12943:        case 0x13:
                   12944:                {
                   12945:                        char tmp[2] = {0};
                   12946:                        tmp[0] = i386_read_stack();
                   12947:                        my_strupr(tmp);
                   12948:                        REG8(AL) = tmp[0];
                   12949:                }
                   12950:                break;
                   12951:        case 0x14:
                   12952: #if defined(HAS_I386)
                   12953:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   12954: #else
                   12955:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   12956: #endif
                   12957:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   12958:                break;
                   12959: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     12960:        case 0x16:
                   12961:                if(REG16(BX) < 20) {
                   12962:                        SREG(ES) = SFT_TOP >> 4;
                   12963:                        i386_load_segment_descriptor(ES);
                   12964:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   12965:                        
                   12966:                        // update system file table
                   12967:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   12968:                        if(file_handler[REG16(BX)].valid) {
                   12969:                                int count = 0;
                   12970:                                for(int i = 0; i < 20; i++) {
                   12971:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   12972:                                                count++;
                   12973:                                        }
                   12974:                                }
                   12975:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   12976:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   12977:                                _lseek(REG16(BX), 0, SEEK_END);
                   12978:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   12979:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   12980:                        } else {
                   12981:                                memset(sft, 0, 0x3b);
                   12982:                        }
                   12983:                } else {
                   12984:                        REG16(AX) = 0x06;
                   12985:                        m_CF = 1;
                   12986:                }
                   12987:                break;
1.1.1.29  root     12988: //     case 0x17: // DOS 3.0+ internal - Get Current Directory Structure for Drive
                   12989: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   12990: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   12991:        case 0x1a:
                   12992:                {
                   12993:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   12994:                        if(path[1] == ':') {
                   12995:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   12996:                                        REG8(AL) = path[0] - 'a' + 1;
                   12997:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   12998:                                        REG8(AL) = path[0] - 'A' + 1;
                   12999:                                } else {
                   13000:                                        REG8(AL) = 0xff; // invalid
                   13001:                                }
                   13002:                                strcpy(full, path);
                   13003:                                strcpy(path, full + 2);
                   13004:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   13005:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   13006:                                        REG8(AL) = full[0] - 'a' + 1;
                   13007:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   13008:                                        REG8(AL) = full[0] - 'A' + 1;
                   13009:                                } else {
                   13010:                                        REG8(AL) = 0xff; // invalid
                   13011:                                }
                   13012:                        } else {
                   13013:                                REG8(AL) = 0x00; // default
                   13014:                        }
                   13015:                }
                   13016:                break;
                   13017:        case 0x1b:
                   13018:                {
                   13019:                        int year = REG16(CX) + 1980;
                   13020:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   13021:                }
                   13022:                break;
                   13023: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   13024: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   13025:        case 0x1e:
                   13026:                {
                   13027:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   13028:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   13029:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   13030: #if defined(HAS_I386)
                   13031:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   13032: #else
                   13033:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   13034: #endif
                   13035:                        } else {
                   13036: #if defined(HAS_I386)
                   13037:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   13038: #else
                   13039:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   13040: #endif
                   13041:                        }
                   13042:                }
                   13043:                break;
                   13044: //     case 0x1f: // DOS 3.0+ internal - Build Current Directory Structure
1.1.1.21  root     13045:        case 0x20:
                   13046:                {
                   13047:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13048:                        
                   13049:                        if(fd < 20) {
                   13050:                                SREG(ES) = current_psp;
                   13051:                                i386_load_segment_descriptor(ES);
                   13052:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   13053:                        } else {
                   13054:                                REG16(AX) = 0x06;
                   13055:                                m_CF = 1;
                   13056:                        }
                   13057:                }
                   13058:                break;
1.1.1.29  root     13059:        case 0x21:
                   13060:                msdos_int_21h_60h(0);
                   13061:                break;
                   13062: //     case 0x22: // DOS 3.0+ internal - Set Extended Error Info
                   13063: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   13064: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   13065:        case 0x25:
                   13066:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13067:                break;
                   13068:        case 0x26:
                   13069:                REG8(AL) = REG8(CL);
                   13070:                msdos_int_21h_3dh();
                   13071:                break;
                   13072:        case 0x27:
                   13073:                msdos_int_21h_3eh();
                   13074:                break;
                   13075:        case 0x28:
                   13076:                REG16(AX) = REG16(BP);
                   13077:                msdos_int_21h_42h();
                   13078:                break;
                   13079:        case 0x29:
                   13080:                msdos_int_21h_3fh();
                   13081:                break;
                   13082: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   13083:        case 0x2b:
                   13084:                REG16(AX) = REG16(BP);
                   13085:                msdos_int_21h_44h();
                   13086:                break;
                   13087:        case 0x2c:
                   13088:                REG16(BX) = DEVICE_TOP >> 4;
                   13089:                REG16(AX) = 22;
                   13090:                break;
                   13091:        case 0x2d:
                   13092:                {
                   13093:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13094:                        REG16(AX) = sda->extended_error_code;
                   13095:                }
                   13096:                break;
                   13097:        case 0x2e:
                   13098:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     13099:                        SREG(ES) = 0x0001;
                   13100:                        i386_load_segment_descriptor(ES);
                   13101:                        REG16(DI) = 0x00;
                   13102:                } else if(REG8(DL) == 0x08) {
                   13103:                        // dummy parameter error message read routine is at fffd:0010
                   13104:                        SREG(ES) = 0xfffd;
1.1.1.22  root     13105:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     13106:                        REG16(DI) = 0x0010;
1.1.1.22  root     13107:                }
                   13108:                break;
1.1.1.29  root     13109:        case 0x2f:
                   13110:                if(REG16(DX) != 0) {
1.1.1.30  root     13111:                        dos_major_version = REG8(DL);
                   13112:                        dos_minor_version = REG8(DH);
1.1.1.29  root     13113:                } else {
                   13114:                        REG8(DL) = 7;
                   13115:                        REG8(DH) = 10;
                   13116:                }
                   13117:                break;
                   13118: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   13119: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     13120:        default:
                   13121:                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));
                   13122:                REG16(AX) = 0x01;
                   13123:                m_CF = 1;
                   13124:                break;
                   13125:        }
                   13126: }
                   13127: 
1.1.1.30  root     13128: inline void msdos_int_2fh_13h()
                   13129: {
                   13130:        static UINT16 prevDS = 0, prevDX = 0;
                   13131:        static UINT16 prevES = 0, prevBX = 0;
                   13132:        UINT16 tmp;
                   13133:        
                   13134:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   13135:        i386_load_segment_descriptor(DS);
                   13136:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   13137:        
                   13138:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   13139:        i386_load_segment_descriptor(ES);
                   13140:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   13141: }
                   13142: 
1.1.1.22  root     13143: inline void msdos_int_2fh_14h()
                   13144: {
                   13145:        switch(REG8(AL)) {
                   13146:        case 0x00:
1.1.1.29  root     13147:                // NLSFUNC.COM is installed
                   13148:                REG8(AL) = 0xff;
1.1.1.25  root     13149:                break;
                   13150:        case 0x01:
                   13151:        case 0x03:
                   13152:                REG8(AL) = 0x00;
                   13153:                active_code_page = REG16(BX);
                   13154:                msdos_nls_tables_update();
                   13155:                break;
                   13156:        case 0x02:
                   13157:                REG8(AL) = get_extended_country_info(REG16(BP));
                   13158:                break;
                   13159:        case 0x04:
1.1.1.42  root     13160:                for(int i = 0;; i++) {
                   13161:                        if(country_table[i].code == REG16(DX)) {
                   13162:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   13163:                                break;
                   13164:                        } else if(country_table[i].code == -1) {
                   13165:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13166:                                break;
                   13167:                        }
                   13168:                }
1.1.1.25  root     13169:                REG8(AL) = 0x00;
1.1.1.22  root     13170:                break;
                   13171:        default:
                   13172:                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));
                   13173:                REG16(AX) = 0x01;
                   13174:                m_CF = 1;
                   13175:                break;
                   13176:        }
                   13177: }
                   13178: 
                   13179: inline void msdos_int_2fh_15h()
                   13180: {
                   13181:        switch(REG8(AL)) {
1.1.1.29  root     13182:        case 0x00: // CD-ROM - Installation Check
                   13183:                if(REG16(BX) == 0x0000) {
1.1.1.43  root     13184: #if 0
                   13185:                        // MSCDEX is installed
                   13186:                        REG16(BX) = 0;
                   13187:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44! root     13188:                                if(msdos_is_cdrom_drive(i)) {
        !          13189:                                        if(REG16(BX) == 0) {
        !          13190:                                                REG16(CX) = i;
1.1.1.43  root     13191:                                        }
1.1.1.44! root     13192:                                        REG16(BX)++;
1.1.1.43  root     13193:                                }
                   13194:                        }
                   13195: #else
1.1.1.29  root     13196:                        // MSCDEX is not installed
                   13197: //                     REG8(AL) = 0x00;
1.1.1.43  root     13198: #endif
1.1.1.29  root     13199:                } else {
                   13200:                        // GRAPHICS.COM is not installed
                   13201: //                     REG8(AL) = 0x00;
                   13202:                }
1.1.1.22  root     13203:                break;
1.1.1.43  root     13204:        case 0x0b:
1.1.1.44! root     13205:                // this call is available from within DOSSHELL even if MSCDEX is not installed
        !          13206:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
        !          13207:                REG16(BX) = 0xadad;
1.1.1.43  root     13208:                break;
                   13209:        case 0x0d:
1.1.1.44! root     13210:                for(int i = 0, n = 0; i < 26; i++) {
        !          13211:                        if(msdos_is_cdrom_drive(i)) {
        !          13212:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     13213:                        }
                   13214:                }
                   13215:                break;
1.1.1.22  root     13216:        case 0xff:
1.1.1.29  root     13217:                if(REG16(BX) == 0x0000) {
                   13218:                        // CORELCDX is not installed
                   13219:                } else {
                   13220:                        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));
                   13221:                        REG16(AX) = 0x01;
                   13222:                        m_CF = 1;
                   13223:                }
1.1.1.22  root     13224:                break;
1.1.1.21  root     13225:        default:
1.1.1.22  root     13226:                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     13227:                REG16(AX) = 0x01;
                   13228:                m_CF = 1;
                   13229:                break;
                   13230:        }
                   13231: }
                   13232: 
1.1       root     13233: inline void msdos_int_2fh_16h()
                   13234: {
                   13235:        switch(REG8(AL)) {
                   13236:        case 0x00:
1.1.1.14  root     13237:                if(no_windows) {
1.1.1.29  root     13238:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   13239: //                     REG8(AL) = 0x00;
1.1.1.14  root     13240:                } else {
1.1.1.30  root     13241:                        REG8(AL) = win_major_version;
                   13242:                        REG8(AH) = win_minor_version;
1.1       root     13243:                }
                   13244:                break;
1.1.1.43  root     13245:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     13246:                // from DOSBox
                   13247:                i386_set_a20_line(1);
                   13248:                break;
1.1.1.43  root     13249:        case 0x06: // Windows Enhanced Mode & 286 DOSX exit Broadcast
                   13250:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   13251:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   13252:                break;
                   13253:        case 0x07:
                   13254:                // Virtual Device Call API
                   13255:                break;
1.1.1.22  root     13256:        case 0x0a:
                   13257:                if(!no_windows) {
                   13258:                        REG16(AX) = 0x0000;
1.1.1.30  root     13259:                        REG8(BH) = win_major_version;
                   13260:                        REG8(BL) = win_minor_version;
1.1.1.22  root     13261:                        REG16(CX) = 0x0003; // enhanced
                   13262:                }
                   13263:                break;
1.1.1.30  root     13264:        case 0x0b:
                   13265:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     13266:        case 0x0e:
                   13267:        case 0x0f:
1.1.1.30  root     13268:        case 0x10:
1.1.1.22  root     13269:        case 0x11:
                   13270:        case 0x12:
                   13271:        case 0x13:
                   13272:        case 0x14:
1.1.1.30  root     13273:        case 0x15:
1.1.1.43  root     13274:        case 0x81:
                   13275:        case 0x82:
1.1.1.44! root     13276:        case 0x84:
1.1.1.33  root     13277:        case 0x86:
1.1.1.22  root     13278:        case 0x87:
1.1.1.30  root     13279:        case 0x89:
1.1.1.33  root     13280:        case 0x8a:
1.1.1.22  root     13281:                // function not supported, do not clear AX
                   13282:                break;
1.1.1.14  root     13283:        case 0x80:
                   13284:                Sleep(10);
1.1.1.35  root     13285:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     13286:                REG8(AL) = 0x00;
1.1.1.14  root     13287:                break;
1.1.1.33  root     13288:        case 0x83:
                   13289:                REG16(BX) = 0x01; // system vm id
                   13290:                break;
1.1.1.22  root     13291:        case 0x8e:
                   13292:                REG16(AX) = 0x00; // failed
                   13293:                break;
1.1.1.20  root     13294:        case 0x8f:
                   13295:                switch(REG8(DH)) {
                   13296:                case 0x00:
                   13297:                case 0x02:
                   13298:                case 0x03:
                   13299:                        REG16(AX) = 0x00;
                   13300:                        break;
                   13301:                case 0x01:
                   13302:                        REG16(AX) = 0x168f;
                   13303:                        break;
                   13304:                }
                   13305:                break;
1.1       root     13306:        default:
1.1.1.22  root     13307:                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));
                   13308:                REG16(AX) = 0x01;
                   13309:                m_CF = 1;
                   13310:                break;
                   13311:        }
                   13312: }
                   13313: 
                   13314: inline void msdos_int_2fh_19h()
                   13315: {
                   13316:        switch(REG8(AL)) {
                   13317:        case 0x00:
1.1.1.29  root     13318:                // SHELLB.COM is not installed
                   13319: //             REG8(AL) = 0x00;
1.1.1.22  root     13320:                break;
                   13321:        case 0x01:
                   13322:        case 0x02:
                   13323:        case 0x03:
                   13324:        case 0x04:
                   13325:                REG16(AX) = 0x01;
                   13326:                m_CF = 1;
                   13327:                break;
1.1.1.29  root     13328:        case 0x80:
                   13329:                // IBM ROM-DOS v4.0 is not installed
                   13330: //             REG8(AL) = 0x00;
                   13331:                break;
1.1.1.22  root     13332:        default:
                   13333:                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     13334:                REG16(AX) = 0x01;
1.1.1.3   root     13335:                m_CF = 1;
1.1       root     13336:                break;
                   13337:        }
                   13338: }
                   13339: 
                   13340: inline void msdos_int_2fh_1ah()
                   13341: {
                   13342:        switch(REG8(AL)) {
                   13343:        case 0x00:
1.1.1.29  root     13344:                // ANSI.SYS is installed
1.1       root     13345:                REG8(AL) = 0xff;
                   13346:                break;
                   13347:        default:
1.1.1.22  root     13348:                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));
                   13349:                REG16(AX) = 0x01;
                   13350:                m_CF = 1;
                   13351:                break;
                   13352:        }
                   13353: }
                   13354: 
1.1.1.30  root     13355: inline void msdos_int_2fh_40h()
1.1.1.22  root     13356: {
                   13357:        switch(REG8(AL)) {
                   13358:        case 0x00:
1.1.1.30  root     13359:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   13360:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     13361:                break;
1.1.1.43  root     13362:        case 0x10:
                   13363:                // OS/2 v2.0+ - Installation Check
                   13364:                REG16(AX) = 0x01;
                   13365:                m_CF = 1;
                   13366:                break;
1.1.1.22  root     13367:        default:
                   13368:                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     13369:                REG16(AX) = 0x01;
1.1.1.3   root     13370:                m_CF = 1;
1.1       root     13371:                break;
                   13372:        }
                   13373: }
                   13374: 
                   13375: inline void msdos_int_2fh_43h()
                   13376: {
                   13377:        switch(REG8(AL)) {
                   13378:        case 0x00:
1.1.1.29  root     13379:                // XMS is installed ?
1.1.1.19  root     13380: #ifdef SUPPORT_XMS
                   13381:                if(support_xms) {
                   13382:                        REG8(AL) = 0x80;
1.1.1.44! root     13383:                }
        !          13384: #endif
        !          13385:                break;
        !          13386:        case 0x08:
        !          13387: #ifdef SUPPORT_XMS
        !          13388:                if(support_xms) {
        !          13389:                        REG8(AL) = 0x43;
        !          13390:                        REG8(BL) = 0x01; // IBM PC/AT
        !          13391:                        REG8(BH) = 0x01; // Fast AT A20 switch time
        !          13392:                }
1.1.1.19  root     13393: #endif
                   13394:                break;
                   13395:        case 0x10:
                   13396:                SREG(ES) = XMS_TOP >> 4;
                   13397:                i386_load_segment_descriptor(ES);
1.1.1.26  root     13398:                REG16(BX) = 0x15;
1.1       root     13399:                break;
1.1.1.44! root     13400:        case 0xe0:
        !          13401:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
        !          13402:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
        !          13403:                        break;
        !          13404:                }
1.1       root     13405:        default:
1.1.1.22  root     13406:                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));
                   13407:                REG16(AX) = 0x01;
                   13408:                m_CF = 1;
                   13409:                break;
                   13410:        }
                   13411: }
                   13412: 
                   13413: inline void msdos_int_2fh_46h()
                   13414: {
                   13415:        switch(REG8(AL)) {
                   13416:        case 0x80:
1.1.1.29  root     13417:                // Windows v3.0 is not installed
                   13418: //             REG8(AL) = 0x00;
1.1.1.22  root     13419:                break;
                   13420:        default:
                   13421:                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));
                   13422:                REG16(AX) = 0x01;
                   13423:                m_CF = 1;
                   13424:                break;
                   13425:        }
                   13426: }
                   13427: 
                   13428: inline void msdos_int_2fh_48h()
                   13429: {
                   13430:        switch(REG8(AL)) {
                   13431:        case 0x00:
1.1.1.29  root     13432:                // DOSKEY is not installed
                   13433: //             REG8(AL) = 0x00;
1.1.1.22  root     13434:                break;
                   13435:        case 0x10:
                   13436:                msdos_int_21h_0ah();
                   13437:                REG16(AX) = 0x00;
                   13438:                break;
                   13439:        default:
                   13440:                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     13441:                REG16(AX) = 0x01;
1.1.1.3   root     13442:                m_CF = 1;
1.1       root     13443:                break;
                   13444:        }
                   13445: }
                   13446: 
                   13447: inline void msdos_int_2fh_4ah()
                   13448: {
                   13449:        switch(REG8(AL)) {
1.1.1.29  root     13450: #ifdef SUPPORT_HMA
                   13451:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   13452:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   13453:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13454:                                // restore first free mcb in high memory area
                   13455:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13456:                        }
                   13457:                        int offset = 0xffff;
                   13458:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   13459:                                REG16(DI) = offset + 0x10;
                   13460:                        } else {
                   13461:                                REG16(DI) = 0xffff;
                   13462:                        }
                   13463:                } else {
                   13464:                        // HMA is already used
                   13465:                        REG16(BX) = 0;
                   13466:                        REG16(DI) = 0xffff;
                   13467:                }
                   13468:                SREG(ES) = 0xffff;
                   13469:                i386_load_segment_descriptor(ES);
                   13470:                break;
                   13471:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   13472:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   13473:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13474:                                // restore first free mcb in high memory area
                   13475:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13476:                        }
                   13477:                        int size = REG16(BX), offset;
                   13478:                        if((size % 16) != 0) {
                   13479:                                size &= ~15;
                   13480:                                size += 16;
                   13481:                        }
                   13482:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   13483:                                REG16(BX) = size;
                   13484:                                REG16(DI) = offset + 0x10;
                   13485:                                is_hma_used_by_int_2fh = true;
                   13486:                        } else {
                   13487:                                REG16(BX) = 0;
                   13488:                                REG16(DI) = 0xffff;
                   13489:                        }
                   13490:                } else {
                   13491:                        // HMA is already used
                   13492:                        REG16(BX) = 0;
                   13493:                        REG16(DI) = 0xffff;
                   13494:                }
                   13495:                SREG(ES) = 0xffff;
                   13496:                i386_load_segment_descriptor(ES);
                   13497:                break;
                   13498:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   13499:                if(REG8(DL) == 0x00) {
                   13500:                        if(!is_hma_used_by_xms) {
                   13501:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13502:                                        // restore first free mcb in high memory area
                   13503:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13504:                                        is_hma_used_by_int_2fh = false;
                   13505:                                }
                   13506:                                int size = REG16(BX), offset;
                   13507:                                if((size % 16) != 0) {
                   13508:                                        size &= ~15;
                   13509:                                        size += 16;
                   13510:                                }
                   13511:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   13512: //                                     REG16(BX) = size;
                   13513:                                        SREG(ES) = 0xffff;
                   13514:                                        i386_load_segment_descriptor(ES);
                   13515:                                        REG16(DI) = offset + 0x10;
                   13516:                                        is_hma_used_by_int_2fh = true;
                   13517:                                } else {
                   13518:                                        REG16(DI) = 0xffff;
                   13519:                                }
                   13520:                        } else {
                   13521:                                REG16(DI) = 0xffff;
                   13522:                        }
                   13523:                } else if(REG8(DL) == 0x01) {
                   13524:                        if(!is_hma_used_by_xms) {
                   13525:                                int size = REG16(BX);
                   13526:                                if((size % 16) != 0) {
                   13527:                                        size &= ~15;
                   13528:                                        size += 16;
                   13529:                                }
                   13530:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   13531:                                        // memory block address is not changed
                   13532:                                } else {
                   13533:                                        REG16(DI) = 0xffff;
                   13534:                                }
                   13535:                        } else {
                   13536:                                REG16(DI) = 0xffff;
                   13537:                        }
                   13538:                } else if(REG8(DL) == 0x02) {
                   13539:                        if(!is_hma_used_by_xms) {
                   13540:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13541:                                        // restore first free mcb in high memory area
                   13542:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13543:                                        is_hma_used_by_int_2fh = false;
                   13544:                                } else {
                   13545:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   13546:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   13547:                                                is_hma_used_by_int_2fh = false;
                   13548:                                        }
                   13549:                                }
                   13550:                        }
                   13551:                } else {
                   13552:                        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));
                   13553:                        REG16(AX) = 0x01;
                   13554:                        m_CF = 1;
                   13555:                }
                   13556:                break;
                   13557:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   13558:                if(!is_hma_used_by_xms) {
                   13559:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13560:                                // restore first free mcb in high memory area
                   13561:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13562:                                is_hma_used_by_int_2fh = false;
                   13563:                        }
                   13564:                        REG16(AX) = 0x0000;
                   13565:                        SREG(ES) = 0xffff;
                   13566:                        i386_load_segment_descriptor(ES);
                   13567:                        REG16(DI) = 0x10;
                   13568:                }
                   13569:                break;
                   13570: #else
1.1       root     13571:        case 0x01:
                   13572:        case 0x02:
1.1.1.29  root     13573:                // HMA is already used
1.1.1.27  root     13574:                REG16(BX) = 0x0000;
1.1.1.3   root     13575:                SREG(ES) = 0xffff;
                   13576:                i386_load_segment_descriptor(ES);
1.1       root     13577:                REG16(DI) = 0xffff;
                   13578:                break;
1.1.1.19  root     13579:        case 0x03:
                   13580:                // unable to allocate
                   13581:                REG16(DI) = 0xffff;
                   13582:                break;
                   13583:        case 0x04:
                   13584:                // function not supported, do not clear AX
                   13585:                break;
1.1.1.29  root     13586: #endif
                   13587:        case 0x10:
1.1.1.42  root     13588:                switch(REG16(BX)) {
                   13589:                case 0x0000:
                   13590:                case 0x0001:
                   13591:                case 0x0002:
                   13592:                case 0x0003:
                   13593:                case 0x0004:
                   13594:                case 0x0005:
                   13595:                case 0x0006:
                   13596:                case 0x0007:
                   13597:                case 0x0008:
                   13598:                case 0x000a:
                   13599:                case 0x1234:
                   13600:                        // SMARTDRV v4.00+ is not installed
                   13601:                        break;
                   13602:                default:
1.1.1.29  root     13603:                        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));
                   13604:                        REG16(AX) = 0x01;
                   13605:                        m_CF = 1;
1.1.1.42  root     13606:                        break;
1.1.1.29  root     13607:                }
                   13608:                break;
                   13609:        case 0x11:
1.1.1.42  root     13610:                switch(REG16(BX)) {
                   13611:                case 0x0000:
                   13612:                case 0x0001:
                   13613:                case 0x0002:
                   13614:                case 0x0003:
                   13615:                case 0x0004:
                   13616:                case 0x0005:
                   13617:                case 0x0006:
                   13618:                case 0x0007:
                   13619:                case 0x0008:
                   13620:                case 0x0009:
                   13621:                case 0x000a:
                   13622:                case 0x000b:
                   13623:                case 0xfffe:
                   13624:                case 0xffff:
1.1.1.29  root     13625:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     13626:                        break;
                   13627:                default:
                   13628:                        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));
                   13629:                        REG16(AX) = 0x01;
                   13630:                        m_CF = 1;
                   13631:                        break;
                   13632:                }
                   13633:                break;
                   13634:        case 0x12:
                   13635:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   13636:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   13637:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   13638:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     13639:                } else {
                   13640:                        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));
                   13641:                        REG16(AX) = 0x01;
                   13642:                        m_CF = 1;
                   13643:                }
1.1.1.22  root     13644:                break;
1.1.1.42  root     13645:        case 0x13:
                   13646:                // DBLSPACE.BIN is not installed
                   13647:                break;
1.1.1.22  root     13648:        default:
                   13649:                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));
                   13650:                REG16(AX) = 0x01;
                   13651:                m_CF = 1;
                   13652:                break;
                   13653:        }
                   13654: }
                   13655: 
                   13656: inline void msdos_int_2fh_4bh()
                   13657: {
                   13658:        switch(REG8(AL)) {
1.1.1.24  root     13659:        case 0x01:
1.1.1.22  root     13660:        case 0x02:
1.1.1.29  root     13661:                // Task Switcher is not installed
1.1.1.24  root     13662:                break;
                   13663:        case 0x03:
                   13664:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   13665:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     13666:                break;
1.1.1.30  root     13667:        case 0x04:
                   13668:                REG16(BX) = 0x0000; // free switcher id successfully
                   13669:                break;
1.1.1.43  root     13670:        case 0x05:
                   13671:                REG16(BX) = 0x0000; // no instance data chain
                   13672:                SREG(ES) = 0x0000;
                   13673:                i386_load_segment_descriptor(ES);
                   13674:                break;
1.1       root     13675:        default:
1.1.1.22  root     13676:                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     13677:                REG16(AX) = 0x01;
1.1.1.3   root     13678:                m_CF = 1;
1.1       root     13679:                break;
                   13680:        }
                   13681: }
                   13682: 
1.1.1.44! root     13683: inline void msdos_int_2fh_4dh()
        !          13684: {
        !          13685:        switch(REG8(AL)) {
        !          13686:        case 0x00:
        !          13687:                // KKCFUNC is not installed ???
        !          13688:                break;
        !          13689:        default:
        !          13690: //             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));
        !          13691:                REG16(AX) = 0x01; // invalid function
        !          13692:                m_CF = 1;
        !          13693:                break;
        !          13694:        }
        !          13695: }
        !          13696: 
1.1       root     13697: inline void msdos_int_2fh_4fh()
                   13698: {
                   13699:        switch(REG8(AL)) {
                   13700:        case 0x00:
1.1.1.29  root     13701:                // BILING is installed
1.1.1.27  root     13702:                REG16(AX) = 0x0000;
                   13703:                REG8(DL) = 0x01;        // major version
                   13704:                REG8(DH) = 0x00;        // minor version
1.1       root     13705:                break;
                   13706:        case 0x01:
1.1.1.27  root     13707:                REG16(AX) = 0x0000;
1.1       root     13708:                REG16(BX) = active_code_page;
                   13709:                break;
                   13710:        default:
1.1.1.22  root     13711:                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));
                   13712:                REG16(AX) = 0x01;
                   13713:                m_CF = 1;
                   13714:                break;
                   13715:        }
                   13716: }
                   13717: 
                   13718: inline void msdos_int_2fh_55h()
                   13719: {
                   13720:        switch(REG8(AL)) {
                   13721:        case 0x00:
                   13722:        case 0x01:
                   13723: //             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));
                   13724:                break;
                   13725:        default:
                   13726:                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     13727:                REG16(AX) = 0x01;
1.1.1.3   root     13728:                m_CF = 1;
1.1       root     13729:                break;
                   13730:        }
                   13731: }
                   13732: 
1.1.1.44! root     13733: inline void msdos_int_2fh_56h()
        !          13734: {
        !          13735:        switch(REG8(AL)) {
        !          13736:        case 0x00:
        !          13737:                // INTERLNK is not installed
        !          13738:                break;
        !          13739:        case 0x01:
        !          13740:                // this call is available from within SCANDISK even if INTERLNK is not installed
        !          13741: //             if(msdos_is_remote_drive(REG8(BH))) {
        !          13742: //                     REG8(AL) = 0x00;
        !          13743: //             }
        !          13744:                break;
        !          13745:        default:
        !          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:                break;
        !          13750:        }
        !          13751: }
        !          13752: 
1.1.1.24  root     13753: inline void msdos_int_2fh_adh()
                   13754: {
                   13755:        switch(REG8(AL)) {
                   13756:        case 0x00:
1.1.1.29  root     13757:                // DISPLAY.SYS is installed
1.1.1.24  root     13758:                REG8(AL) = 0xff;
                   13759:                REG16(BX) = 0x100; // ???
                   13760:                break;
                   13761:        case 0x01:
                   13762:                active_code_page = REG16(BX);
                   13763:                msdos_nls_tables_update();
                   13764:                REG16(AX) = 0x01;
                   13765:                break;
                   13766:        case 0x02:
                   13767:                REG16(BX) = active_code_page;
                   13768:                break;
                   13769:        case 0x03:
                   13770:                // FIXME
                   13771:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   13772:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   13773:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   13774:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   13775:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   13776:                break;
                   13777:        case 0x80:
                   13778:                break; // keyb.com is not installed
                   13779:        default:
                   13780:                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));
                   13781:                REG16(AX) = 0x01;
                   13782:                m_CF = 1;
                   13783:                break;
                   13784:        }
                   13785: }
                   13786: 
1.1       root     13787: inline void msdos_int_2fh_aeh()
                   13788: {
                   13789:        switch(REG8(AL)) {
                   13790:        case 0x00:
1.1.1.28  root     13791:                // FIXME: we need to check the given command line
                   13792:                REG8(AL) = 0x00; // the command should be executed as usual
                   13793: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     13794:                break;
                   13795:        case 0x01:
                   13796:                {
                   13797:                        char command[MAX_PATH];
                   13798:                        memset(command, 0, sizeof(command));
1.1.1.3   root     13799:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     13800:                        
                   13801:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   13802:                        param->env_seg = 0;
                   13803:                        param->cmd_line.w.l = 44;
                   13804:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13805:                        param->fcb1.w.l = 24;
                   13806:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   13807:                        param->fcb2.w.l = 24;
                   13808:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   13809:                        
                   13810:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   13811:                        
                   13812:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     13813:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   13814:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     13815:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   13816:                        
1.1.1.28  root     13817:                        try {
                   13818:                                msdos_process_exec(command, param, 0);
                   13819:                        } catch(...) {
                   13820:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     13821:                        }
                   13822:                }
                   13823:                break;
                   13824:        default:
1.1.1.22  root     13825:                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     13826:                REG16(AX) = 0x01;
1.1.1.3   root     13827:                m_CF = 1;
1.1       root     13828:                break;
                   13829:        }
                   13830: }
                   13831: 
1.1.1.34  root     13832: inline void msdos_int_2fh_b7h()
                   13833: {
                   13834:        switch(REG8(AL)) {
                   13835:        case 0x00:
                   13836:                // APPEND is not installed
                   13837: //             REG8(AL) = 0x00;
                   13838:                break;
1.1.1.44! root     13839:        case 0x06:
        !          13840:                REG16(BX) = 0x0000;
        !          13841:                break;
1.1.1.34  root     13842:        case 0x07:
1.1.1.43  root     13843:        case 0x11:
1.1.1.34  root     13844:                // COMMAND.COM calls this service without checking APPEND is installed
                   13845:                break;
                   13846:        default:
                   13847:                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));
                   13848:                REG16(AX) = 0x01;
                   13849:                m_CF = 1;
                   13850:                break;
                   13851:        }
                   13852: }
                   13853: 
1.1.1.24  root     13854: inline void msdos_int_33h_0000h()
                   13855: {
                   13856:        REG16(AX) = 0xffff; // hardware/driver installed
                   13857:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   13858: }
                   13859: 
                   13860: inline void msdos_int_33h_0001h()
                   13861: {
1.1.1.34  root     13862:        if(mouse.hidden > 0) {
                   13863:                mouse.hidden--;
                   13864:        }
                   13865:        if(mouse.hidden == 0) {
1.1.1.24  root     13866:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   13867:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   13868:                }
                   13869:                pic[1].imr &= ~0x10;    // enable irq12
                   13870:        }
                   13871: }
                   13872: 
                   13873: inline void msdos_int_33h_0002h()
                   13874: {
1.1.1.34  root     13875:        mouse.hidden++;
                   13876: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
                   13877:        pic[1].imr |= 0x10;     // enable irq12
1.1.1.24  root     13878: }
                   13879: 
                   13880: inline void msdos_int_33h_0003h()
                   13881: {
1.1.1.34  root     13882: //     if(mouse.hidden > 0) {
                   13883:                update_console_input();
                   13884: //     }
1.1.1.24  root     13885:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     13886:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   13887:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   13888: }
                   13889: 
                   13890: inline void msdos_int_33h_0004h()
                   13891: {
                   13892:        mouse.position.x = REG16(CX);
                   13893:        mouse.position.x = REG16(DX);
1.1.1.24  root     13894: }
                   13895: 
                   13896: inline void msdos_int_33h_0005h()
                   13897: {
1.1.1.34  root     13898: //     if(mouse.hidden > 0) {
                   13899:                update_console_input();
                   13900: //     }
1.1.1.24  root     13901:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   13902:                int idx = REG16(BX);
1.1.1.34  root     13903:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   13904:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   13905:                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     13906:                mouse.buttons[idx].pressed_times = 0;
                   13907:        } else {
                   13908:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   13909:        }
                   13910:        REG16(AX) = mouse.get_buttons();
                   13911: }
                   13912: 
                   13913: inline void msdos_int_33h_0006h()
                   13914: {
1.1.1.34  root     13915: //     if(mouse.hidden > 0) {
                   13916:                update_console_input();
                   13917: //     }
1.1.1.24  root     13918:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   13919:                int idx = REG16(BX);
1.1.1.34  root     13920:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   13921:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   13922:                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     13923:                mouse.buttons[idx].released_times = 0;
                   13924:        } else {
                   13925:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   13926:        }
                   13927:        REG16(AX) = mouse.get_buttons();
                   13928: }
                   13929: 
                   13930: inline void msdos_int_33h_0007h()
                   13931: {
                   13932:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   13933:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   13934: }
                   13935: 
                   13936: inline void msdos_int_33h_0008h()
                   13937: {
                   13938:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   13939:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   13940: }
                   13941: 
                   13942: inline void msdos_int_33h_0009h()
                   13943: {
                   13944:        mouse.hot_spot[0] = REG16(BX);
                   13945:        mouse.hot_spot[1] = REG16(CX);
                   13946: }
                   13947: 
                   13948: inline void msdos_int_33h_000bh()
                   13949: {
1.1.1.34  root     13950: //     if(mouse.hidden > 0) {
                   13951:                update_console_input();
                   13952: //     }
1.1.1.24  root     13953:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   13954:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   13955:        mouse.prev_position.x = mouse.position.x;
                   13956:        mouse.prev_position.y = mouse.position.y;
                   13957:        REG16(CX) = dx;
                   13958:        REG16(DX) = dy;
                   13959: }
                   13960: 
                   13961: inline void msdos_int_33h_000ch()
                   13962: {
                   13963:        mouse.call_mask = REG16(CX);
                   13964:        mouse.call_addr.w.l = REG16(DX);
                   13965:        mouse.call_addr.w.h = SREG(ES);
                   13966: }
                   13967: 
                   13968: inline void msdos_int_33h_000fh()
                   13969: {
                   13970:        mouse.mickey.x = REG16(CX);
                   13971:        mouse.mickey.y = REG16(DX);
                   13972: }
                   13973: 
                   13974: inline void msdos_int_33h_0011h()
                   13975: {
                   13976:        REG16(AX) = 0xffff;
                   13977:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   13978: }
                   13979: 
                   13980: inline void msdos_int_33h_0014h()
                   13981: {
                   13982:        UINT16 old_mask = mouse.call_mask;
                   13983:        UINT16 old_ofs = mouse.call_addr.w.l;
                   13984:        UINT16 old_seg = mouse.call_addr.w.h;
                   13985:        
                   13986:        mouse.call_mask = REG16(CX);
                   13987:        mouse.call_addr.w.l = REG16(DX);
                   13988:        mouse.call_addr.w.h = SREG(ES);
                   13989:        
                   13990:        REG16(CX) = old_mask;
                   13991:        REG16(DX) = old_ofs;
                   13992:        SREG(ES) = old_seg;
                   13993:        i386_load_segment_descriptor(ES);
                   13994: }
                   13995: 
                   13996: inline void msdos_int_33h_0015h()
                   13997: {
                   13998:        REG16(BX) = sizeof(mouse);
                   13999: }
                   14000: 
                   14001: inline void msdos_int_33h_0016h()
                   14002: {
                   14003:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   14004: }
                   14005: 
                   14006: inline void msdos_int_33h_0017h()
                   14007: {
                   14008:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   14009: }
                   14010: 
1.1.1.43  root     14011: inline void msdos_int_33h_0018h()
                   14012: {
                   14013:        for(int i = 0; i < 8; i++) {
                   14014:                if(REG16(CX) & (1 << i)) {
                   14015:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   14016:                                // event handler already exists
                   14017:                                REG16(AX) = 0xffff;
                   14018:                                break;
                   14019:                        }
                   14020:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   14021:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   14022:                }
                   14023:        }
                   14024: }
                   14025: 
                   14026: inline void msdos_int_33h_0019h()
                   14027: {
                   14028:        UINT16 call_mask = REG16(CX);
                   14029:        
                   14030:        REG16(CX) = 0;
                   14031:        
                   14032:        for(int i = 0; i < 8; i++) {
                   14033:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   14034:                        for(int j = 0; j < 8; j++) {
                   14035:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   14036:                                        REG16(CX) |= (1 << j);
                   14037:                                }
                   14038:                        }
                   14039:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   14040:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   14041:                        break;
                   14042:                }
                   14043:        }
                   14044: }
                   14045: 
1.1.1.24  root     14046: inline void msdos_int_33h_001ah()
                   14047: {
                   14048:        mouse.sensitivity[0] = REG16(BX);
                   14049:        mouse.sensitivity[1] = REG16(CX);
                   14050:        mouse.sensitivity[2] = REG16(DX);
                   14051: }
                   14052: 
                   14053: inline void msdos_int_33h_001bh()
                   14054: {
                   14055:        REG16(BX) = mouse.sensitivity[0];
                   14056:        REG16(CX) = mouse.sensitivity[1];
                   14057:        REG16(DX) = mouse.sensitivity[2];
                   14058: }
                   14059: 
                   14060: inline void msdos_int_33h_001dh()
                   14061: {
                   14062:        mouse.display_page = REG16(BX);
                   14063: }
                   14064: 
                   14065: inline void msdos_int_33h_001eh()
                   14066: {
                   14067:        REG16(BX) = mouse.display_page;
                   14068: }
                   14069: 
1.1.1.34  root     14070: inline void msdos_int_33h_001fh()
                   14071: {
                   14072:        // from DOSBox
                   14073:        REG16(BX) = 0x0000;
                   14074:        SREG(ES) = 0x0000;
                   14075:        i386_load_segment_descriptor(ES);
                   14076:        mouse.enabled = false;
                   14077:        mouse.old_hidden = mouse.hidden;
                   14078:        mouse.hidden = 1;
                   14079: }
                   14080: 
                   14081: inline void msdos_int_33h_0020h()
                   14082: {
                   14083:        // from DOSBox
                   14084:        mouse.enabled = true;
                   14085:        mouse.hidden = mouse.old_hidden;
                   14086: }
                   14087: 
1.1.1.24  root     14088: inline void msdos_int_33h_0021h()
                   14089: {
                   14090:        REG16(AX) = 0xffff;
                   14091:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14092: }
                   14093: 
                   14094: inline void msdos_int_33h_0022h()
                   14095: {
                   14096:        mouse.language = REG16(BX);
                   14097: }
                   14098: 
                   14099: inline void msdos_int_33h_0023h()
                   14100: {
                   14101:        REG16(BX) = mouse.language;
                   14102: }
                   14103: 
                   14104: inline void msdos_int_33h_0024h()
                   14105: {
                   14106:        REG16(BX) = 0x0805; // V8.05
                   14107:        REG16(CX) = 0x0400; // PS/2
                   14108: }
                   14109: 
                   14110: inline void msdos_int_33h_0026h()
                   14111: {
                   14112:        REG16(BX) = 0x0000;
                   14113:        REG16(CX) = mouse.max_position.x;
                   14114:        REG16(DX) = mouse.max_position.y;
                   14115: }
                   14116: 
                   14117: inline void msdos_int_33h_002ah()
                   14118: {
1.1.1.34  root     14119:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     14120:        REG16(BX) = mouse.hot_spot[0];
                   14121:        REG16(CX) = mouse.hot_spot[1];
                   14122:        REG16(DX) = 4; // PS/2
                   14123: }
                   14124: 
                   14125: inline void msdos_int_33h_0031h()
                   14126: {
                   14127:        REG16(AX) = mouse.min_position.x;
                   14128:        REG16(BX) = mouse.min_position.y;
                   14129:        REG16(CX) = mouse.max_position.x;
                   14130:        REG16(DX) = mouse.max_position.y;
                   14131: }
                   14132: 
                   14133: inline void msdos_int_33h_0032h()
                   14134: {
                   14135:        REG16(AX) = 0;
                   14136: //     REG16(AX) |= 0x8000; // 0025h
                   14137:        REG16(AX) |= 0x4000; // 0026h
                   14138: //     REG16(AX) |= 0x2000; // 0027h
                   14139: //     REG16(AX) |= 0x1000; // 0028h
                   14140: //     REG16(AX) |= 0x0800; // 0029h
                   14141:        REG16(AX) |= 0x0400; // 002ah
                   14142: //     REG16(AX) |= 0x0200; // 002bh
                   14143: //     REG16(AX) |= 0x0100; // 002ch
                   14144: //     REG16(AX) |= 0x0080; // 002dh
                   14145: //     REG16(AX) |= 0x0040; // 002eh
                   14146:        REG16(AX) |= 0x0020; // 002fh
                   14147: //     REG16(AX) |= 0x0010; // 0030h
                   14148:        REG16(AX) |= 0x0008; // 0031h
                   14149:        REG16(AX) |= 0x0004; // 0032h
                   14150: //     REG16(AX) |= 0x0002; // 0033h
                   14151: //     REG16(AX) |= 0x0001; // 0034h
                   14152: }
                   14153: 
1.1.1.19  root     14154: inline void msdos_int_67h_40h()
                   14155: {
                   14156:        if(!support_ems) {
                   14157:                REG8(AH) = 0x84;
                   14158:        } else {
                   14159:                REG8(AH) = 0x00;
                   14160:        }
                   14161: }
                   14162: 
                   14163: inline void msdos_int_67h_41h()
                   14164: {
                   14165:        if(!support_ems) {
                   14166:                REG8(AH) = 0x84;
                   14167:        } else {
                   14168:                REG8(AH) = 0x00;
                   14169:                REG16(BX) = EMS_TOP >> 4;
                   14170:        }
                   14171: }
                   14172: 
                   14173: inline void msdos_int_67h_42h()
                   14174: {
                   14175:        if(!support_ems) {
                   14176:                REG8(AH) = 0x84;
                   14177:        } else {
                   14178:                REG8(AH) = 0x00;
                   14179:                REG16(BX) = free_ems_pages;
                   14180:                REG16(DX) = MAX_EMS_PAGES;
                   14181:        }
                   14182: }
                   14183: 
                   14184: inline void msdos_int_67h_43h()
                   14185: {
                   14186:        if(!support_ems) {
                   14187:                REG8(AH) = 0x84;
                   14188:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   14189:                REG8(AH) = 0x87;
                   14190:        } else if(REG16(BX) > free_ems_pages) {
                   14191:                REG8(AH) = 0x88;
                   14192:        } else if(REG16(BX) == 0) {
                   14193:                REG8(AH) = 0x89;
                   14194:        } else {
1.1.1.31  root     14195:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14196:                        if(!ems_handles[i].allocated) {
                   14197:                                ems_allocate_pages(i, REG16(BX));
                   14198:                                REG8(AH) = 0x00;
                   14199:                                REG16(DX) = i;
                   14200:                                return;
                   14201:                        }
                   14202:                }
                   14203:                REG8(AH) = 0x85;
                   14204:        }
                   14205: }
                   14206: 
                   14207: inline void msdos_int_67h_44h()
                   14208: {
                   14209:        if(!support_ems) {
                   14210:                REG8(AH) = 0x84;
1.1.1.31  root     14211:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14212:                REG8(AH) = 0x83;
                   14213:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   14214:                REG8(AH) = 0x8a;
                   14215: //     } else if(!(REG8(AL) < 4)) {
                   14216: //             REG8(AH) = 0x8b;
                   14217:        } else if(REG16(BX) == 0xffff) {
                   14218:                ems_unmap_page(REG8(AL) & 3);
                   14219:                REG8(AH) = 0x00;
                   14220:        } else {
                   14221:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   14222:                REG8(AH) = 0x00;
                   14223:        }
                   14224: }
                   14225: 
                   14226: inline void msdos_int_67h_45h()
                   14227: {
                   14228:        if(!support_ems) {
                   14229:                REG8(AH) = 0x84;
1.1.1.31  root     14230:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14231:                REG8(AH) = 0x83;
                   14232:        } else {
                   14233:                ems_release_pages(REG16(DX));
                   14234:                REG8(AH) = 0x00;
                   14235:        }
                   14236: }
                   14237: 
                   14238: inline void msdos_int_67h_46h()
                   14239: {
                   14240:        if(!support_ems) {
                   14241:                REG8(AH) = 0x84;
                   14242:        } else {
1.1.1.29  root     14243: //             REG16(AX) = 0x0032; // EMS 3.2
                   14244:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     14245:        }
                   14246: }
                   14247: 
                   14248: inline void msdos_int_67h_47h()
                   14249: {
                   14250:        // NOTE: the map data should be stored in the specified ems page, not process data
                   14251:        process_t *process = msdos_process_info_get(current_psp);
                   14252:        
                   14253:        if(!support_ems) {
                   14254:                REG8(AH) = 0x84;
1.1.1.31  root     14255: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14256: //             REG8(AH) = 0x83;
                   14257:        } else if(process->ems_pages_stored) {
                   14258:                REG8(AH) = 0x8d;
                   14259:        } else {
                   14260:                for(int i = 0; i < 4; i++) {
                   14261:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   14262:                        process->ems_pages[i].page   = ems_pages[i].page;
                   14263:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   14264:                }
                   14265:                process->ems_pages_stored = true;
                   14266:                REG8(AH) = 0x00;
                   14267:        }
                   14268: }
                   14269: 
                   14270: inline void msdos_int_67h_48h()
                   14271: {
                   14272:        // NOTE: the map data should be restored from the specified ems page, not process data
                   14273:        process_t *process = msdos_process_info_get(current_psp);
                   14274:        
                   14275:        if(!support_ems) {
                   14276:                REG8(AH) = 0x84;
1.1.1.31  root     14277: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14278: //             REG8(AH) = 0x83;
                   14279:        } else if(!process->ems_pages_stored) {
                   14280:                REG8(AH) = 0x8e;
                   14281:        } else {
                   14282:                for(int i = 0; i < 4; i++) {
                   14283:                        if(process->ems_pages[i].mapped) {
                   14284:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   14285:                        } else {
                   14286:                                ems_unmap_page(i);
                   14287:                        }
                   14288:                }
                   14289:                process->ems_pages_stored = false;
                   14290:                REG8(AH) = 0x00;
                   14291:        }
                   14292: }
                   14293: 
                   14294: inline void msdos_int_67h_4bh()
                   14295: {
                   14296:        if(!support_ems) {
                   14297:                REG8(AH) = 0x84;
                   14298:        } else {
                   14299:                REG8(AH) = 0x00;
                   14300:                REG16(BX) = 0;
1.1.1.31  root     14301:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14302:                        if(ems_handles[i].allocated) {
                   14303:                                REG16(BX)++;
                   14304:                        }
                   14305:                }
                   14306:        }
                   14307: }
                   14308: 
                   14309: inline void msdos_int_67h_4ch()
                   14310: {
                   14311:        if(!support_ems) {
                   14312:                REG8(AH) = 0x84;
1.1.1.31  root     14313:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14314:                REG8(AH) = 0x83;
                   14315:        } else {
                   14316:                REG8(AH) = 0x00;
                   14317:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   14318:        }
                   14319: }
                   14320: 
                   14321: inline void msdos_int_67h_4dh()
                   14322: {
                   14323:        if(!support_ems) {
                   14324:                REG8(AH) = 0x84;
                   14325:        } else {
                   14326:                REG8(AH) = 0x00;
                   14327:                REG16(BX) = 0;
1.1.1.31  root     14328:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14329:                        if(ems_handles[i].allocated) {
                   14330:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   14331:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   14332:                                REG16(BX)++;
                   14333:                        }
                   14334:                }
                   14335:        }
                   14336: }
                   14337: 
1.1.1.20  root     14338: inline void msdos_int_67h_4eh()
                   14339: {
                   14340:        if(!support_ems) {
                   14341:                REG8(AH) = 0x84;
                   14342:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14343:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   14344:                        // save page map
                   14345:                        for(int i = 0; i < 4; i++) {
                   14346:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   14347:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   14348:                        }
                   14349:                }
                   14350:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14351:                        // restore page map
                   14352:                        for(int i = 0; i < 4; i++) {
                   14353:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   14354:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   14355:                                
1.1.1.31  root     14356:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     14357:                                        ems_map_page(i, handle, page);
                   14358:                                } else {
                   14359:                                        ems_unmap_page(i);
                   14360:                                }
                   14361:                        }
                   14362:                }
                   14363:                REG8(AH) = 0x00;
                   14364:        } else if(REG8(AL) == 0x03) {
                   14365:                REG8(AH) = 0x00;
1.1.1.21  root     14366:                REG8(AL) = 4 * 4;
                   14367:        } else {
1.1.1.22  root     14368:                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     14369:                REG8(AH) = 0x8f;
                   14370:        }
                   14371: }
                   14372: 
                   14373: inline void msdos_int_67h_4fh()
                   14374: {
                   14375:        if(!support_ems) {
                   14376:                REG8(AH) = 0x84;
                   14377:        } else if(REG8(AL) == 0x00) {
                   14378:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   14379:                
                   14380:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   14381:                for(int i = 0; i < count; i++) {
                   14382:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   14383:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   14384:                        
                   14385: //                     if(!(physical < 4)) {
                   14386: //                             REG8(AH) = 0x8b;
                   14387: //                             return;
                   14388: //                     }
                   14389:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     14390:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   14391:                        *(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     14392:                }
                   14393:                REG8(AH) = 0x00;
                   14394:        } else if(REG8(AL) == 0x01) {
                   14395:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   14396:                
                   14397:                for(int i = 0; i < count; i++) {
                   14398:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   14399:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   14400:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   14401:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   14402:                        
                   14403: //                     if(!(physical < 4)) {
                   14404: //                             REG8(AH) = 0x8b;
                   14405: //                             return;
                   14406: //                     } else
1.1.1.41  root     14407:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     14408:                                ems_map_page(physical & 3, handle, logical);
                   14409:                        } else {
1.1.1.41  root     14410:                                ems_unmap_page(physical & 3);
1.1.1.21  root     14411:                        }
                   14412:                }
                   14413:                REG8(AH) = 0x00;
                   14414:        } else if(REG8(AL) == 0x02) {
                   14415:                REG8(AH) = 0x00;
                   14416:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     14417:        } else {
1.1.1.22  root     14418:                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     14419:                REG8(AH) = 0x8f;
                   14420:        }
                   14421: }
                   14422: 
                   14423: inline void msdos_int_67h_50h()
                   14424: {
                   14425:        if(!support_ems) {
                   14426:                REG8(AH) = 0x84;
1.1.1.31  root     14427:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     14428:                REG8(AH) = 0x83;
                   14429:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   14430:                for(int i = 0; i < REG16(CX); i++) {
                   14431:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   14432:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   14433:                        
                   14434:                        if(REG8(AL) == 0x01) {
                   14435:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   14436:                        }
                   14437: //                     if(!(physical < 4)) {
                   14438: //                             REG8(AH) = 0x8b;
                   14439: //                             return;
                   14440: //                     } else
                   14441:                        if(logical == 0xffff) {
                   14442:                                ems_unmap_page(physical & 3);
                   14443:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   14444:                                ems_map_page(physical & 3, REG16(DX), logical);
                   14445:                        } else {
                   14446:                                REG8(AH) = 0x8a;
                   14447:                                return;
                   14448:                        }
                   14449:                }
                   14450:                REG8(AH) = 0x00;
                   14451:        } else {
1.1.1.22  root     14452:                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     14453:                REG8(AH) = 0x8f;
                   14454:        }
                   14455: }
                   14456: 
1.1.1.19  root     14457: inline void msdos_int_67h_51h()
                   14458: {
                   14459:        if(!support_ems) {
                   14460:                REG8(AH) = 0x84;
1.1.1.31  root     14461:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14462:                REG8(AH) = 0x83;
                   14463:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   14464:                REG8(AH) = 0x87;
                   14465:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   14466:                REG8(AH) = 0x88;
                   14467:        } else {
                   14468:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   14469:                REG8(AH) = 0x00;
                   14470:        }
                   14471: }
                   14472: 
1.1.1.20  root     14473: inline void msdos_int_67h_52h()
                   14474: {
                   14475:        if(!support_ems) {
                   14476:                REG8(AH) = 0x84;
1.1.1.31  root     14477: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   14478: //             REG8(AH) = 0x83;
1.1.1.20  root     14479:        } else if(REG8(AL) == 0x00) {
                   14480:                REG8(AL) = 0x00; // handle is volatile
                   14481:                REG8(AH) = 0x00;
                   14482:        } else if(REG8(AL) == 0x01) {
                   14483:                if(REG8(BL) == 0x00) {
                   14484:                        REG8(AH) = 0x00;
                   14485:                } else {
                   14486:                        REG8(AH) = 0x90; // undefined attribute type
                   14487:                }
                   14488:        } else if(REG8(AL) == 0x02) {
                   14489:                REG8(AL) = 0x00; // only volatile handles supported
                   14490:                REG8(AH) = 0x00;
                   14491:        } else {
1.1.1.22  root     14492:                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     14493:                REG8(AH) = 0x8f;
                   14494:        }
                   14495: }
                   14496: 
1.1.1.19  root     14497: inline void msdos_int_67h_53h()
                   14498: {
                   14499:        if(!support_ems) {
                   14500:                REG8(AH) = 0x84;
1.1.1.31  root     14501:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14502:                REG8(AH) = 0x83;
                   14503:        } else if(REG8(AL) == 0x00) {
                   14504:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   14505:                REG8(AH) = 0x00;
                   14506:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     14507:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14508:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   14509:                                REG8(AH) = 0xa1;
                   14510:                                return;
                   14511:                        }
                   14512:                }
                   14513:                REG8(AH) = 0x00;
                   14514:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   14515:        } else {
1.1.1.22  root     14516:                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     14517:                REG8(AH) = 0x8f;
1.1.1.19  root     14518:        }
                   14519: }
                   14520: 
                   14521: inline void msdos_int_67h_54h()
                   14522: {
                   14523:        if(!support_ems) {
                   14524:                REG8(AH) = 0x84;
                   14525:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     14526:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14527:                        if(ems_handles[i].allocated) {
                   14528:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   14529:                        } else {
                   14530:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   14531:                        }
                   14532:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   14533:                }
                   14534:                REG8(AH) = 0x00;
                   14535:                REG8(AL) = MAX_EMS_HANDLES;
                   14536:        } else if(REG8(AL) == 0x01) {
                   14537:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     14538:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14539:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   14540:                                REG8(AH) = 0x00;
                   14541:                                REG16(DX) = i;
                   14542:                                break;
                   14543:                        }
                   14544:                }
                   14545:        } else if(REG8(AL) == 0x02) {
                   14546:                REG8(AH) = 0x00;
                   14547:                REG16(BX) = MAX_EMS_HANDLES;
                   14548:        } else {
1.1.1.22  root     14549:                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     14550:                REG8(AH) = 0x8f;
                   14551:        }
                   14552: }
                   14553: 
                   14554: inline void msdos_int_67h_57h_tmp()
                   14555: {
                   14556:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   14557:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   14558:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   14559:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   14560:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   14561:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   14562:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   14563:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   14564:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   14565:        
1.1.1.32  root     14566:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     14567:        UINT32 src_addr, dest_addr;
                   14568:        UINT32 src_addr_max, dest_addr_max;
                   14569:        
                   14570:        if(src_type == 0) {
                   14571:                src_buffer = mem;
                   14572:                src_addr = (src_seg << 4) + src_ofs;
                   14573:                src_addr_max = MAX_MEM;
                   14574:        } else {
1.1.1.31  root     14575:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     14576:                        REG8(AH) = 0x83;
                   14577:                        return;
                   14578:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   14579:                        REG8(AH) = 0x8a;
                   14580:                        return;
                   14581:                }
1.1.1.32  root     14582:                if(ems_handles[src_handle].buffer != NULL) {
                   14583:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   14584:                }
1.1.1.20  root     14585:                src_addr = src_ofs;
1.1.1.32  root     14586:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     14587:        }
                   14588:        if(dest_type == 0) {
                   14589:                dest_buffer = mem;
                   14590:                dest_addr = (dest_seg << 4) + dest_ofs;
                   14591:                dest_addr_max = MAX_MEM;
                   14592:        } else {
1.1.1.31  root     14593:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     14594:                        REG8(AH) = 0x83;
                   14595:                        return;
                   14596:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   14597:                        REG8(AH) = 0x8a;
                   14598:                        return;
                   14599:                }
1.1.1.32  root     14600:                if(ems_handles[dest_handle].buffer != NULL) {
                   14601:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   14602:                }
1.1.1.20  root     14603:                dest_addr = dest_ofs;
1.1.1.32  root     14604:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     14605:        }
1.1.1.32  root     14606:        if(src_buffer != NULL && dest_buffer != NULL) {
                   14607:                for(int i = 0; i < copy_length; i++) {
                   14608:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   14609:                                if(REG8(AL) == 0x00) {
                   14610:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   14611:                                } else if(REG8(AL) == 0x01) {
                   14612:                                        UINT8 tmp = dest_buffer[dest_addr];
                   14613:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   14614:                                        src_buffer[src_addr++] = tmp;
                   14615:                                }
                   14616:                        } else {
                   14617:                                REG8(AH) = 0x93;
                   14618:                                return;
1.1.1.20  root     14619:                        }
                   14620:                }
1.1.1.32  root     14621:                REG8(AH) = 0x00;
                   14622:        } else {
                   14623:                REG8(AH) = 0x80;
1.1.1.20  root     14624:        }
                   14625: }
                   14626: 
                   14627: inline void msdos_int_67h_57h()
                   14628: {
                   14629:        if(!support_ems) {
                   14630:                REG8(AH) = 0x84;
                   14631:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   14632:                struct {
                   14633:                        UINT16 handle;
                   14634:                        UINT16 page;
                   14635:                        bool mapped;
                   14636:                } tmp_pages[4];
                   14637:                
                   14638:                // unmap pages to copy memory data to ems buffer
                   14639:                for(int i = 0; i < 4; i++) {
                   14640:                        tmp_pages[i].handle = ems_pages[i].handle;
                   14641:                        tmp_pages[i].page   = ems_pages[i].page;
                   14642:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   14643:                        ems_unmap_page(i);
                   14644:                }
                   14645:                
                   14646:                // run move/exchange operation
                   14647:                msdos_int_67h_57h_tmp();
                   14648:                
                   14649:                // restore unmapped pages
                   14650:                for(int i = 0; i < 4; i++) {
                   14651:                        if(tmp_pages[i].mapped) {
                   14652:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   14653:                        }
                   14654:                }
                   14655:        } else {
1.1.1.22  root     14656:                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     14657:                REG8(AH) = 0x8f;
                   14658:        }
                   14659: }
                   14660: 
                   14661: inline void msdos_int_67h_58h()
                   14662: {
                   14663:        if(!support_ems) {
                   14664:                REG8(AH) = 0x84;
                   14665:        } else if(REG8(AL) == 0x00) {
                   14666:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     14667:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   14668:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     14669:                }
                   14670:                REG8(AH) = 0x00;
                   14671:                REG16(CX) = 4;
                   14672:        } else if(REG8(AL) == 0x01) {
                   14673:                REG8(AH) = 0x00;
                   14674:                REG16(CX) = 4;
                   14675:        } else {
1.1.1.22  root     14676:                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     14677:                REG8(AH) = 0x8f;
                   14678:        }
                   14679: }
                   14680: 
1.1.1.42  root     14681: inline void msdos_int_67h_59h()
                   14682: {
                   14683:        if(!support_ems) {
                   14684:                REG8(AH) = 0x84;
                   14685:        } else if(REG8(AL) == 0x00) {
                   14686:                REG8(AH) = 0xa4; // access denied by operating system
                   14687:        } else if(REG8(AL) == 0x01) {
                   14688:                REG8(AH) = 0x00;
                   14689:                REG16(BX) = free_ems_pages;
                   14690:                REG16(DX) = MAX_EMS_PAGES;
                   14691:        } else {
                   14692:                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));
                   14693:                REG8(AH) = 0x8f;
                   14694:        }
                   14695: }
                   14696: 
1.1.1.20  root     14697: inline void msdos_int_67h_5ah()
                   14698: {
                   14699:        if(!support_ems) {
1.1.1.19  root     14700:                REG8(AH) = 0x84;
1.1.1.20  root     14701:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   14702:                REG8(AH) = 0x87;
                   14703:        } else if(REG16(BX) > free_ems_pages) {
                   14704:                REG8(AH) = 0x88;
                   14705: //     } else if(REG16(BX) == 0) {
                   14706: //             REG8(AH) = 0x89;
                   14707:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     14708:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     14709:                        if(!ems_handles[i].allocated) {
                   14710:                                ems_allocate_pages(i, REG16(BX));
                   14711:                                REG8(AH) = 0x00;
                   14712:                                REG16(DX) = i;
                   14713:                                return;
                   14714:                        }
                   14715:                }
                   14716:                REG8(AH) = 0x85;
                   14717:        } else {
1.1.1.22  root     14718:                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     14719:                REG8(AH) = 0x8f;
1.1.1.19  root     14720:        }
                   14721: }
                   14722: 
1.1.1.43  root     14723: inline void msdos_int_67h_5dh()
                   14724: {
                   14725:        if(!support_ems) {
                   14726:                REG8(AH) = 0x84;
                   14727:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14728:                REG8(AH) = 0xa4; // operating system denied access
                   14729:        } else {
                   14730:                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));
                   14731:                REG8(AH) = 0x8f;
                   14732:        }
                   14733: }
                   14734: 
1.1.1.30  root     14735: inline void msdos_int_67h_deh()
                   14736: {
                   14737:        REG8(AH) = 0x84;
                   14738: }
                   14739: 
1.1.1.19  root     14740: #ifdef SUPPORT_XMS
                   14741: 
1.1.1.32  root     14742: void msdos_xms_init()
1.1.1.26  root     14743: {
1.1.1.30  root     14744:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   14745:        emb_handle_top->address = EMB_TOP;
                   14746:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     14747:        xms_a20_local_enb_count = 0;
                   14748: }
                   14749: 
1.1.1.32  root     14750: void msdos_xms_finish()
                   14751: {
                   14752:        msdos_xms_release();
                   14753: }
                   14754: 
                   14755: void msdos_xms_release()
1.1.1.30  root     14756: {
                   14757:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   14758:                emb_handle_t *next_handle = emb_handle->next;
                   14759:                free(emb_handle);
                   14760:                emb_handle = next_handle;
                   14761:        }
                   14762: }
                   14763: 
                   14764: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   14765: {
                   14766:        if(handle != 0) {
                   14767:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   14768:                        if(emb_handle->handle == handle) {
                   14769:                                return(emb_handle);
                   14770:                        }
                   14771:                }
                   14772:        }
                   14773:        return(NULL);
                   14774: }
                   14775: 
                   14776: int msdos_xms_get_unused_emb_handle_id()
                   14777: {
                   14778:        for(int handle = 1;; handle++) {
                   14779:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   14780:                        return(handle);
                   14781:                }
                   14782:        }
                   14783:        return(0);
                   14784: }
                   14785: 
                   14786: int msdos_xms_get_unused_emb_handle_count()
                   14787: {
                   14788:        int count = 64; //255;
                   14789:        
                   14790:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   14791:                if(emb_handle->handle != 0) {
                   14792:                        if(--count == 1) {
                   14793:                                break;
                   14794:                        }
                   14795:                }
                   14796:        }
                   14797:        return(count);
                   14798: }
                   14799: 
                   14800: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   14801: {
                   14802:        if(emb_handle->size_kb > size_kb) {
                   14803:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   14804:                
                   14805:                new_handle->address = emb_handle->address + size_kb * 1024;
                   14806:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   14807:                emb_handle->size_kb = size_kb;
                   14808:                
                   14809:                new_handle->prev = emb_handle;
                   14810:                new_handle->next = emb_handle->next;
                   14811:                if(emb_handle->next != NULL) {
                   14812:                        emb_handle->next->prev = new_handle;
                   14813:                }
                   14814:                emb_handle->next = new_handle;
                   14815:        }
                   14816: }
                   14817: 
                   14818: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   14819: {
                   14820:        emb_handle_t *next_handle = emb_handle->next;
                   14821:        
                   14822:        if(next_handle != NULL) {
                   14823:                emb_handle->size_kb += next_handle->size_kb;
                   14824:                
                   14825:                if(next_handle->next != NULL) {
                   14826:                        next_handle->next->prev = emb_handle;
                   14827:                }
                   14828:                emb_handle->next = next_handle->next;
                   14829:                free(next_handle);
                   14830:        }
                   14831: }
                   14832: 
                   14833: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   14834: {
                   14835:        emb_handle_t *target_handle = NULL;
                   14836:        
                   14837:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   14838:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   14839:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   14840:                                target_handle = emb_handle;
                   14841:                        }
                   14842:                }
                   14843:        }
                   14844:        if(target_handle != NULL) {
                   14845:                if(target_handle->size_kb > size_kb) {
                   14846:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   14847:                }
                   14848: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   14849:                return(target_handle);
                   14850:        }
                   14851:        return(NULL);
                   14852: }
                   14853: 
                   14854: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   14855: {
                   14856:        emb_handle_t *prev_handle = emb_handle->prev;
                   14857:        emb_handle_t *next_handle = emb_handle->next;
                   14858:        
                   14859:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   14860:                msdos_xms_combine_emb_handles(prev_handle);
                   14861:                emb_handle = prev_handle;
                   14862:        }
                   14863:        if(next_handle != NULL && next_handle->handle == 0) {
                   14864:                msdos_xms_combine_emb_handles(emb_handle);
                   14865:        }
                   14866:        emb_handle->handle = 0;
                   14867: }
                   14868: 
1.1.1.19  root     14869: inline void msdos_call_xms_00h()
                   14870: {
1.1.1.29  root     14871: #if defined(HAS_I386)
                   14872:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
                   14873:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   14874: #else
                   14875:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   14876:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   14877: #endif
                   14878: //     REG16(DX) = 0x0000; // HMA does not exist
                   14879:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     14880: }
                   14881: 
                   14882: inline void msdos_call_xms_01h()
                   14883: {
1.1.1.29  root     14884:        if(REG8(AL) == 0x40) {
                   14885:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   14886:                // DX=KB free extended memory returned by last call of function 08h
                   14887:                REG16(AX) = 0x0000;
                   14888:                REG8(BL) = 0x91;
                   14889:                REG16(DX) = xms_dx_after_call_08h;
                   14890:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   14891:                REG16(AX) = 0x0000;
                   14892:                REG8(BL) = 0x81; // Vdisk was detected
                   14893: #ifdef SUPPORT_HMA
                   14894:        } else if(is_hma_used_by_int_2fh) {
                   14895:                REG16(AX) = 0x0000;
                   14896:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   14897:        } else if(is_hma_used_by_xms) {
                   14898:                REG16(AX) = 0x0000;
                   14899:                REG8(BL) = 0x91; // HMA is already in use
                   14900:        } else {
                   14901:                REG16(AX) = 0x0001;
                   14902:                is_hma_used_by_xms = true;
                   14903: #else
                   14904:        } else {
                   14905:                REG16(AX) = 0x0000;
                   14906:                REG8(BL) = 0x91; // HMA is already in use
                   14907: #endif
                   14908:        }
1.1.1.19  root     14909: }
                   14910: 
                   14911: inline void msdos_call_xms_02h()
                   14912: {
1.1.1.29  root     14913:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   14914:                REG16(AX) = 0x0000;
                   14915:                REG8(BL) = 0x81; // Vdisk was detected
                   14916: #ifdef SUPPORT_HMA
                   14917:        } else if(is_hma_used_by_int_2fh) {
                   14918:                REG16(AX) = 0x0000;
                   14919:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   14920:        } else if(!is_hma_used_by_xms) {
                   14921:                REG16(AX) = 0x0000;
                   14922:                REG8(BL) = 0x93; // HMA is not allocated
                   14923:        } else {
                   14924:                REG16(AX) = 0x0001;
                   14925:                is_hma_used_by_xms = false;
                   14926:                // restore first free mcb in high memory area
                   14927:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14928: #else
                   14929:        } else {
                   14930:                REG16(AX) = 0x0000;
                   14931:                REG8(BL) = 0x91; // HMA is already in use
                   14932: #endif
                   14933:        }
1.1.1.19  root     14934: }
                   14935: 
                   14936: inline void msdos_call_xms_03h()
                   14937: {
                   14938:        i386_set_a20_line(1);
                   14939:        REG16(AX) = 0x0001;
                   14940:        REG8(BL) = 0x00;
                   14941: }
                   14942: 
                   14943: inline void msdos_call_xms_04h()
                   14944: {
1.1.1.21  root     14945:        i386_set_a20_line(0);
                   14946:        REG16(AX) = 0x0001;
                   14947:        REG8(BL) = 0x00;
1.1.1.19  root     14948: }
                   14949: 
                   14950: inline void msdos_call_xms_05h()
                   14951: {
                   14952:        i386_set_a20_line(1);
                   14953:        REG16(AX) = 0x0001;
                   14954:        REG8(BL) = 0x00;
1.1.1.21  root     14955:        xms_a20_local_enb_count++;
1.1.1.19  root     14956: }
                   14957: 
                   14958: void msdos_call_xms_06h()
                   14959: {
1.1.1.21  root     14960:        if(xms_a20_local_enb_count > 0) {
                   14961:                xms_a20_local_enb_count--;
                   14962:        }
                   14963:        if(xms_a20_local_enb_count == 0) {
1.1.1.19  root     14964:                i386_set_a20_line(0);
1.1.1.21  root     14965:        }
                   14966:        if((m_a20_mask >> 20) & 1) {
1.1.1.19  root     14967:                REG16(AX) = 0x0000;
                   14968:                REG8(BL) = 0x94;
1.1.1.21  root     14969:        } else {
                   14970:                REG16(AX) = 0x0001;
                   14971:                REG8(BL) = 0x00;
1.1.1.19  root     14972:        }
                   14973: }
                   14974: 
                   14975: inline void msdos_call_xms_07h()
                   14976: {
                   14977:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   14978:        REG8(BL) = 0x00;
                   14979: }
                   14980: 
                   14981: inline void msdos_call_xms_08h()
                   14982: {
                   14983:        REG16(AX) = REG16(DX) = 0x0000;
                   14984:        
1.1.1.30  root     14985:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   14986:                if(emb_handle->handle == 0) {
                   14987:                        if(REG16(AX) < emb_handle->size_kb) {
                   14988:                                REG16(AX) = emb_handle->size_kb;
1.1.1.19  root     14989:                        }
1.1.1.30  root     14990:                        REG16(DX) += emb_handle->size_kb;
1.1.1.19  root     14991:                }
                   14992:        }
                   14993:        
                   14994:        if(REG16(AX) == 0 && REG16(DX) == 0) {
                   14995:                REG8(BL) = 0xa0;
                   14996:        } else {
                   14997:                REG8(BL) = 0x00;
                   14998:        }
1.1.1.29  root     14999:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     15000: }
                   15001: 
1.1.1.30  root     15002: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     15003: {
1.1.1.30  root     15004:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   15005:        
                   15006:        if(emb_handle != NULL) {
                   15007:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   15008:                
                   15009:                REG16(AX) = 0x0001;
                   15010:                REG16(DX) = emb_handle->handle;
                   15011:                REG8(BL) = 0x00;
                   15012:        } else {
                   15013:                REG16(AX) = REG16(DX) = 0x0000;
                   15014:                REG8(BL) = 0xa0;
1.1.1.19  root     15015:        }
1.1.1.30  root     15016: }
                   15017: 
                   15018: inline void msdos_call_xms_09h()
                   15019: {
                   15020:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     15021: }
                   15022: 
                   15023: inline void msdos_call_xms_0ah()
                   15024: {
1.1.1.30  root     15025:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15026:        
                   15027:        if(emb_handle == NULL) {
1.1.1.19  root     15028:                REG16(AX) = 0x0000;
                   15029:                REG8(BL) = 0xa2;
1.1.1.30  root     15030:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     15031:                REG16(AX) = 0x0000;
                   15032:                REG8(BL) = 0xab;
                   15033:        } else {
1.1.1.30  root     15034:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     15035:                
                   15036:                REG16(AX) = 0x0001;
                   15037:                REG8(BL) = 0x00;
                   15038:        }
                   15039: }
                   15040: 
                   15041: inline void msdos_call_xms_0bh()
                   15042: {
                   15043:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15044:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15045:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   15046:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   15047:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15048:        
                   15049:        UINT8 *src_buffer, *dest_buffer;
                   15050:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     15051:        emb_handle_t *emb_handle;
1.1.1.19  root     15052:        
                   15053:        if(src_handle == 0) {
                   15054:                src_buffer = mem;
                   15055:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   15056:                src_addr_max = MAX_MEM;
1.1.1.30  root     15057: 
1.1.1.19  root     15058:        } else {
1.1.1.30  root     15059:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     15060:                        REG16(AX) = 0x0000;
                   15061:                        REG8(BL) = 0xa3;
                   15062:                        return;
                   15063:                }
1.1.1.30  root     15064:                src_buffer = mem + emb_handle->address;
                   15065:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     15066:        }
                   15067:        if(dest_handle == 0) {
                   15068:                dest_buffer = mem;
                   15069:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   15070:                dest_addr_max = MAX_MEM;
                   15071:        } else {
1.1.1.30  root     15072:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     15073:                        REG16(AX) = 0x0000;
                   15074:                        REG8(BL) = 0xa5;
                   15075:                        return;
                   15076:                }
1.1.1.30  root     15077:                dest_buffer = mem + emb_handle->address;
                   15078:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     15079:        }
                   15080:        for(int i = 0; i < copy_length; i++) {
                   15081:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   15082:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   15083:                } else {
                   15084:                        break;
                   15085:                }
                   15086:        }
                   15087:        REG16(AX) = 0x0001;
                   15088:        REG8(BL) = 0x00;
                   15089: }
                   15090: 
                   15091: inline void msdos_call_xms_0ch()
                   15092: {
1.1.1.30  root     15093:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15094:        
                   15095:        if(emb_handle == NULL) {
1.1.1.19  root     15096:                REG16(AX) = 0x0000;
                   15097:                REG8(BL) = 0xa2;
                   15098:        } else {
1.1.1.30  root     15099:                emb_handle->lock++;
1.1.1.19  root     15100:                REG16(AX) = 0x0001;
                   15101:                REG8(BL) = 0x00;
1.1.1.30  root     15102:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   15103:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     15104:        }
                   15105: }
                   15106: 
                   15107: inline void msdos_call_xms_0dh()
                   15108: {
1.1.1.30  root     15109:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15110:        
                   15111:        if(emb_handle == NULL) {
1.1.1.19  root     15112:                REG16(AX) = 0x0000;
                   15113:                REG8(BL) = 0xa2;
1.1.1.30  root     15114:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     15115:                REG16(AX) = 0x0000;
                   15116:                REG8(BL) = 0xaa;
                   15117:        } else {
1.1.1.30  root     15118:                emb_handle->lock--;
1.1.1.19  root     15119:                REG16(AX) = 0x0001;
                   15120:                REG8(BL) = 0x00;
                   15121:        }
                   15122: }
                   15123: 
                   15124: inline void msdos_call_xms_0eh()
                   15125: {
1.1.1.30  root     15126:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15127:        
                   15128:        if(emb_handle == NULL) {
1.1.1.19  root     15129:                REG16(AX) = 0x0000;
                   15130:                REG8(BL) = 0xa2;
                   15131:        } else {
                   15132:                REG16(AX) = 0x0001;
1.1.1.30  root     15133:                REG8(BH) = emb_handle->lock;
                   15134:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   15135:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     15136:        }
                   15137: }
                   15138: 
1.1.1.30  root     15139: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     15140: {
1.1.1.30  root     15141:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15142:        
                   15143:        if(emb_handle == NULL) {
1.1.1.19  root     15144:                REG16(AX) = 0x0000;
                   15145:                REG8(BL) = 0xa2;
1.1.1.30  root     15146:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     15147:                REG16(AX) = 0x0000;
                   15148:                REG8(BL) = 0xab;
                   15149:        } else {
1.1.1.30  root     15150:                if(emb_handle->size_kb < size_kb) {
                   15151:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   15152:                                msdos_xms_combine_emb_handles(emb_handle);
                   15153:                                if(emb_handle->size_kb > size_kb) {
                   15154:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   15155:                                }
                   15156:                        } else {
                   15157:                                int old_handle = emb_handle->handle;
                   15158:                                int old_size_kb = emb_handle->size_kb;
                   15159:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   15160:                                
                   15161:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   15162:                                msdos_xms_free_emb_handle(emb_handle);
                   15163:                                
                   15164:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   15165:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   15166:                                }
                   15167:                                emb_handle->handle = old_handle;
                   15168:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   15169:                                free(buffer);
                   15170:                        }
                   15171:                } else if(emb_handle->size_kb > size_kb) {
                   15172:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   15173:                }
                   15174:                if(emb_handle->size_kb != size_kb) {
                   15175:                        REG16(AX) = 0x0000;
                   15176:                        REG8(BL) = 0xa0;
                   15177:                } else {
                   15178:                        REG16(AX) = 0x0001;
                   15179:                        REG8(BL) = 0x00;
                   15180:                }
1.1.1.19  root     15181:        }
                   15182: }
                   15183: 
1.1.1.30  root     15184: inline void msdos_call_xms_0fh()
                   15185: {
                   15186:        msdos_call_xms_0fh(REG16(BX));
                   15187: }
                   15188: 
1.1.1.19  root     15189: inline void msdos_call_xms_10h()
                   15190: {
                   15191:        int seg;
                   15192:        
                   15193:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   15194:                REG16(AX) = 0x0001;
                   15195:                REG16(BX) = seg;
                   15196:        } else {
                   15197:                REG16(AX) = 0x0000;
                   15198:                REG8(BL) = 0xb0;
                   15199:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   15200:        }
                   15201: }
                   15202: 
                   15203: inline void msdos_call_xms_11h()
                   15204: {
                   15205:        int mcb_seg = REG16(DX) - 1;
                   15206:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   15207:        
                   15208:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   15209:                msdos_mem_free(REG16(DX));
                   15210:                REG16(AX) = 0x0001;
                   15211:                REG8(BL) = 0x00;
                   15212:        } else {
                   15213:                REG16(AX) = 0x0000;
                   15214:                REG8(BL) = 0xb2;
                   15215:        }
                   15216: }
                   15217: 
                   15218: inline void msdos_call_xms_12h()
                   15219: {
                   15220:        int mcb_seg = REG16(DX) - 1;
                   15221:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   15222:        int max_paragraphs;
                   15223:        
                   15224:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   15225:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   15226:                        REG16(AX) = 0x0001;
                   15227:                        REG8(BL) = 0x00;
                   15228:                } else {
                   15229:                        REG16(AX) = 0x0000;
                   15230:                        REG8(BL) = 0xb0;
                   15231:                        REG16(DX) = max_paragraphs;
                   15232:                }
                   15233:        } else {
                   15234:                REG16(AX) = 0x0000;
                   15235:                REG8(BL) = 0xb2;
                   15236:        }
                   15237: }
                   15238: 
1.1.1.29  root     15239: #if defined(HAS_I386)
                   15240: 
                   15241: inline void msdos_call_xms_88h()
                   15242: {
                   15243:        REG32(EAX) = REG32(EDX) = 0x0000;
                   15244:        
1.1.1.30  root     15245:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15246:                if(emb_handle->handle == 0) {
                   15247:                        if(REG32(EAX) < emb_handle->size_kb) {
                   15248:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     15249:                        }
1.1.1.30  root     15250:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     15251:                }
                   15252:        }
                   15253:        
                   15254:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   15255:                REG8(BL) = 0xa0;
                   15256:        } else {
                   15257:                REG8(BL) = 0x00;
                   15258:        }
                   15259:        REG32(ECX) = EMB_END - 1;
                   15260: }
                   15261: 
                   15262: inline void msdos_call_xms_89h()
                   15263: {
1.1.1.30  root     15264:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     15265: }
                   15266: 
                   15267: inline void msdos_call_xms_8eh()
                   15268: {
1.1.1.30  root     15269:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15270:        
                   15271:        if(emb_handle == NULL) {
1.1.1.29  root     15272:                REG16(AX) = 0x0000;
                   15273:                REG8(BL) = 0xa2;
                   15274:        } else {
                   15275:                REG16(AX) = 0x0001;
1.1.1.30  root     15276:                REG8(BH) = emb_handle->lock;
                   15277:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   15278:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     15279:        }
                   15280: }
                   15281: 
                   15282: inline void msdos_call_xms_8fh()
                   15283: {
1.1.1.30  root     15284:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     15285: }
                   15286: 
                   15287: #endif
1.1.1.19  root     15288: #endif
                   15289: 
1.1.1.26  root     15290: UINT16 msdos_get_equipment()
                   15291: {
                   15292:        static UINT16 equip = 0;
                   15293:        
                   15294:        if(equip == 0) {
                   15295: #ifdef SUPPORT_FPU
                   15296:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   15297: #endif
                   15298:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   15299:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   15300: //             equip |= (1 << 8);      // 0 if DMA installed
                   15301:                equip |= (2 << 9);      // number of serial ports
                   15302:                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     15303:                
                   15304:                // check only A: and B: if it is floppy drive
                   15305:                int n = 0;
                   15306:                for(int i = 0; i < 2; i++) {
1.1.1.44! root     15307:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
        !          15308:                                n++;
1.1.1.28  root     15309:                        }
                   15310:                }
                   15311:                if(n != 0) {
                   15312:                        equip |= (1 << 0);      // floppy disk(s) installed
                   15313:                        n--;
                   15314:                        equip |= (n << 6);      // number of floppies installed less 1
                   15315:                }
                   15316: //             if(joyGetNumDevs() != 0) {
                   15317: //                     equip |= (1 << 12);     // game port installed
                   15318: //             }
1.1.1.26  root     15319:        }
                   15320:        return(equip);
                   15321: }
                   15322: 
1.1       root     15323: void msdos_syscall(unsigned num)
                   15324: {
1.1.1.22  root     15325: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     15326:        if(num == 0x08 || num == 0x1c) {
                   15327:                // don't log the timer interrupts
                   15328:        } else if(num == 0x68) {
1.1.1.22  root     15329:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     15330:                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     15331:        } else if(num == 0x69) {
                   15332:                // dummy interrupt for XMS (call far)
1.1.1.33  root     15333:                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.22  root     15334:        } else if(num == 0x6a) {
                   15335:                // dummy interrupt for case map routine pointed in the country info
                   15336:        } else {
1.1.1.33  root     15337:                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     15338:        }
                   15339: #endif
1.1.1.36  root     15340:        // update cursor position
                   15341:        if(cursor_moved) {
                   15342:                pcbios_update_cursor_position();
                   15343:                cursor_moved = false;
                   15344:        }
1.1.1.33  root     15345:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     15346:        
1.1       root     15347:        switch(num) {
                   15348:        case 0x00:
1.1.1.28  root     15349:                try {
                   15350:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   15351:                        error("division by zero\n");
                   15352:                } catch(...) {
                   15353:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   15354:                }
1.1       root     15355:                break;
                   15356:        case 0x04:
1.1.1.28  root     15357:                try {
                   15358:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   15359:                        error("overflow\n");
                   15360:                } catch(...) {
                   15361:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   15362:                }
1.1       root     15363:                break;
                   15364:        case 0x06:
                   15365:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     15366:                if(!ignore_illegal_insn) {
1.1.1.28  root     15367:                        try {
                   15368:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   15369:                                error("illegal instruction\n");
                   15370:                        } catch(...) {
                   15371:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   15372:                        }
1.1.1.14  root     15373:                } else {
                   15374: #if defined(HAS_I386)
1.1.1.39  root     15375:                        m_eip = m_int6h_skip_eip;
                   15376: #elif defined(HAS_I286)
                   15377:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     15378: #else
1.1.1.39  root     15379:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     15380: #endif
                   15381:                }
1.1       root     15382:                break;
1.1.1.33  root     15383:        case 0x09:
                   15384:                // ctrl-break is pressed
                   15385:                if(raise_int_1bh) {
                   15386: #if defined(HAS_I386)
                   15387:                        m_ext = 0; // not an external interrupt
                   15388:                        i386_trap(0x1b, 1, 0);
                   15389:                        m_ext = 1;
                   15390: #else
                   15391:                        PREFIX86(_interrupt)(0x1b);
                   15392: #endif
                   15393:                        raise_int_1bh = false;
                   15394:                }
1.1.1.8   root     15395:        case 0x08:
1.1.1.14  root     15396: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     15397:        case 0x0b:
                   15398:        case 0x0c:
                   15399:        case 0x0d:
                   15400:        case 0x0e:
                   15401:        case 0x0f:
                   15402:                // EOI
                   15403:                pic[0].isr &= ~(1 << (num - 0x08));
                   15404:                pic_update();
                   15405:                break;
1.1       root     15406:        case 0x10:
                   15407:                // PC BIOS - Video
1.1.1.14  root     15408:                if(!restore_console_on_exit) {
1.1.1.15  root     15409:                        change_console_size(scr_width, scr_height);
1.1       root     15410:                }
1.1.1.3   root     15411:                m_CF = 0;
1.1       root     15412:                switch(REG8(AH)) {
1.1.1.16  root     15413:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     15414:                case 0x01: pcbios_int_10h_01h(); break;
                   15415:                case 0x02: pcbios_int_10h_02h(); break;
                   15416:                case 0x03: pcbios_int_10h_03h(); break;
                   15417:                case 0x05: pcbios_int_10h_05h(); break;
                   15418:                case 0x06: pcbios_int_10h_06h(); break;
                   15419:                case 0x07: pcbios_int_10h_07h(); break;
                   15420:                case 0x08: pcbios_int_10h_08h(); break;
                   15421:                case 0x09: pcbios_int_10h_09h(); break;
                   15422:                case 0x0a: pcbios_int_10h_0ah(); break;
                   15423:                case 0x0b: break;
1.1.1.40  root     15424:                case 0x0c: pcbios_int_10h_0ch(); break;
                   15425:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     15426:                case 0x0e: pcbios_int_10h_0eh(); break;
                   15427:                case 0x0f: pcbios_int_10h_0fh(); break;
                   15428:                case 0x10: break;
1.1.1.14  root     15429:                case 0x11: pcbios_int_10h_11h(); break;
                   15430:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     15431:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     15432:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     15433:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     15434:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   15435:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     15436:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     15437:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   15438:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     15439:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     15440:                case 0x6f: break;
1.1.1.22  root     15441:                case 0x80: m_CF = 1; break; // unknown
                   15442:                case 0x81: m_CF = 1; break; // unknown
1.1       root     15443:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     15444:                case 0x83: pcbios_int_10h_83h(); break;
                   15445:                case 0x8b: break;
                   15446:                case 0x8c: m_CF = 1; break; // unknown
                   15447:                case 0x8d: m_CF = 1; break; // unknown
                   15448:                case 0x8e: m_CF = 1; break; // unknown
                   15449:                case 0x90: pcbios_int_10h_90h(); break;
                   15450:                case 0x91: pcbios_int_10h_91h(); break;
                   15451:                case 0x92: break;
                   15452:                case 0x93: break;
                   15453:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     15454:                case 0xfa: break; // ega register interface library is not installed
1.1       root     15455:                case 0xfe: pcbios_int_10h_feh(); break;
                   15456:                case 0xff: pcbios_int_10h_ffh(); break;
                   15457:                default:
1.1.1.22  root     15458:                        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));
                   15459:                        m_CF = 1;
1.1       root     15460:                        break;
                   15461:                }
                   15462:                break;
                   15463:        case 0x11:
                   15464:                // PC BIOS - Get Equipment List
1.1.1.26  root     15465:                REG16(AX) = msdos_get_equipment();
1.1       root     15466:                break;
                   15467:        case 0x12:
                   15468:                // PC BIOS - Get Memory Size
1.1.1.33  root     15469:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     15470:                break;
                   15471:        case 0x13:
1.1.1.42  root     15472:                // PC BIOS - Disk I/O
                   15473:                {
                   15474:                        static UINT8 last = 0x00;
                   15475:                        switch(REG8(AH)) {
                   15476:                        case 0x00: pcbios_int_13h_00h(); break;
                   15477:                        case 0x01: // get last status
                   15478:                                REG8(AH) = last;
                   15479:                                break;
                   15480:                        case 0x02: pcbios_int_13h_02h(); break;
                   15481:                        case 0x03: pcbios_int_13h_03h(); break;
                   15482:                        case 0x04: pcbios_int_13h_04h(); break;
                   15483:                        case 0x08: pcbios_int_13h_08h(); break;
                   15484:                        case 0x0a: pcbios_int_13h_02h(); break;
                   15485:                        case 0x0b: pcbios_int_13h_03h(); break;
                   15486:                        case 0x0d: pcbios_int_13h_00h(); break;
                   15487:                        case 0x10: pcbios_int_13h_10h(); break;
                   15488:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     15489:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     15490:                        case 0x05: // format
                   15491:                        case 0x06:
                   15492:                        case 0x07:
                   15493:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   15494:                                m_CF = 1;
                   15495:                                break;
                   15496:                        case 0x09:
                   15497:                        case 0x0c: // seek
                   15498:                        case 0x11: // recalib
                   15499:                        case 0x14:
                   15500:                        case 0x17:
                   15501:                                REG8(AH) = 0x00; // successful completion
                   15502:                                break;
1.1.1.43  root     15503:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   15504:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   15505:                                REG8(AH) = 0x01; // invalid function
                   15506:                                m_CF = 1;
                   15507:                                break;
1.1.1.42  root     15508:                        default:
                   15509:                                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));
                   15510:                                REG8(AH) = 0x01; // invalid function
                   15511:                                m_CF = 1;
                   15512:                                break;
                   15513:                        }
                   15514:                        last = REG8(AH);
                   15515:                }
1.1       root     15516:                break;
                   15517:        case 0x14:
                   15518:                // PC BIOS - Serial I/O
1.1.1.25  root     15519:                switch(REG8(AH)) {
                   15520:                case 0x00: pcbios_int_14h_00h(); break;
                   15521:                case 0x01: pcbios_int_14h_01h(); break;
                   15522:                case 0x02: pcbios_int_14h_02h(); break;
                   15523:                case 0x03: pcbios_int_14h_03h(); break;
                   15524:                case 0x04: pcbios_int_14h_04h(); break;
                   15525:                case 0x05: pcbios_int_14h_05h(); break;
                   15526:                default:
                   15527:                        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));
                   15528:                        break;
                   15529:                }
1.1       root     15530:                break;
                   15531:        case 0x15:
                   15532:                // PC BIOS
1.1.1.3   root     15533:                m_CF = 0;
1.1       root     15534:                switch(REG8(AH)) {
1.1.1.14  root     15535:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     15536:                case 0x23: pcbios_int_15h_23h(); break;
                   15537:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     15538:                case 0x41: break;
1.1       root     15539:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     15540:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     15541:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     15542:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     15543:                case 0x86: pcbios_int_15h_86h(); break;
                   15544:                case 0x87: pcbios_int_15h_87h(); break;
                   15545:                case 0x88: pcbios_int_15h_88h(); break;
                   15546:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     15547:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     15548:                case 0xc0: // PS/2 ???
                   15549:                case 0xc1:
                   15550:                case 0xc2:
1.1.1.30  root     15551:                case 0xc3: // PS50+ ???
                   15552:                case 0xc4:
1.1.1.22  root     15553:                        REG8(AH) = 0x86;
                   15554:                        m_CF = 1;
                   15555:                        break;
1.1.1.3   root     15556: #if defined(HAS_I386)
1.1       root     15557:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     15558: #endif
1.1       root     15559:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     15560:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     15561:                default:
1.1.1.22  root     15562:                        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));
                   15563:                        REG8(AH) = 0x86;
1.1.1.3   root     15564:                        m_CF = 1;
1.1       root     15565:                        break;
                   15566:                }
                   15567:                break;
                   15568:        case 0x16:
                   15569:                // PC BIOS - Keyboard
1.1.1.3   root     15570:                m_CF = 0;
1.1       root     15571:                switch(REG8(AH)) {
                   15572:                case 0x00: pcbios_int_16h_00h(); break;
                   15573:                case 0x01: pcbios_int_16h_01h(); break;
                   15574:                case 0x02: pcbios_int_16h_02h(); break;
                   15575:                case 0x03: pcbios_int_16h_03h(); break;
                   15576:                case 0x05: pcbios_int_16h_05h(); break;
                   15577:                case 0x10: pcbios_int_16h_00h(); break;
                   15578:                case 0x11: pcbios_int_16h_01h(); break;
                   15579:                case 0x12: pcbios_int_16h_12h(); break;
                   15580:                case 0x13: pcbios_int_16h_13h(); break;
                   15581:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     15582:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     15583:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     15584:                case 0xda: break; // unknown
1.1.1.43  root     15585:                case 0xdb: break; // unknown
1.1.1.22  root     15586:                case 0xff: break; // unknown
1.1       root     15587:                default:
1.1.1.22  root     15588:                        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     15589:                        break;
                   15590:                }
                   15591:                break;
                   15592:        case 0x17:
                   15593:                // PC BIOS - Printer
1.1.1.37  root     15594:                m_CF = 0;
                   15595:                switch(REG8(AH)) {
                   15596:                case 0x00: pcbios_int_17h_00h(); break;
                   15597:                case 0x01: pcbios_int_17h_01h(); break;
                   15598:                case 0x02: pcbios_int_17h_02h(); break;
                   15599:                case 0x03: pcbios_int_17h_03h(); break;
                   15600:                case 0x50: pcbios_int_17h_50h(); break;
                   15601:                case 0x51: pcbios_int_17h_51h(); break;
                   15602:                case 0x52: pcbios_int_17h_52h(); break;
                   15603:                case 0x84: pcbios_int_17h_84h(); break;
                   15604:                case 0x85: pcbios_int_17h_85h(); break;
                   15605:                default:
                   15606:                        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));
                   15607:                        break;
                   15608:                }
1.1       root     15609:                break;
                   15610:        case 0x1a:
                   15611:                // PC BIOS - Timer
1.1.1.3   root     15612:                m_CF = 0;
1.1       root     15613:                switch(REG8(AH)) {
                   15614:                case 0x00: pcbios_int_1ah_00h(); break;
                   15615:                case 0x01: break;
                   15616:                case 0x02: pcbios_int_1ah_02h(); break;
                   15617:                case 0x03: break;
                   15618:                case 0x04: pcbios_int_1ah_04h(); break;
                   15619:                case 0x05: break;
                   15620:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   15621:                case 0x0b: break;
1.1.1.14  root     15622:                case 0x35: break; // Word Perfect Third Party Interface?
                   15623:                case 0x36: break; // Word Perfect Third Party Interface
                   15624:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44! root     15625:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     15626:                case 0xb1: break; // PCI BIOS v2.0c+
                   15627:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     15628:                default:
1.1.1.22  root     15629:                        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     15630:                        break;
                   15631:                }
                   15632:                break;
1.1.1.33  root     15633:        case 0x1b:
                   15634:                mem[0x471] = 0x00;
                   15635:                break;
1.1       root     15636:        case 0x20:
1.1.1.28  root     15637:                try {
                   15638:                        msdos_process_terminate(SREG(CS), retval, 1);
                   15639:                } catch(...) {
                   15640:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   15641:                }
1.1       root     15642:                break;
                   15643:        case 0x21:
                   15644:                // MS-DOS System Call
1.1.1.3   root     15645:                m_CF = 0;
1.1.1.28  root     15646:                try {
                   15647:                        switch(REG8(AH)) {
                   15648:                        case 0x00: msdos_int_21h_00h(); break;
                   15649:                        case 0x01: msdos_int_21h_01h(); break;
                   15650:                        case 0x02: msdos_int_21h_02h(); break;
                   15651:                        case 0x03: msdos_int_21h_03h(); break;
                   15652:                        case 0x04: msdos_int_21h_04h(); break;
                   15653:                        case 0x05: msdos_int_21h_05h(); break;
                   15654:                        case 0x06: msdos_int_21h_06h(); break;
                   15655:                        case 0x07: msdos_int_21h_07h(); break;
                   15656:                        case 0x08: msdos_int_21h_08h(); break;
                   15657:                        case 0x09: msdos_int_21h_09h(); break;
                   15658:                        case 0x0a: msdos_int_21h_0ah(); break;
                   15659:                        case 0x0b: msdos_int_21h_0bh(); break;
                   15660:                        case 0x0c: msdos_int_21h_0ch(); break;
                   15661:                        case 0x0d: msdos_int_21h_0dh(); break;
                   15662:                        case 0x0e: msdos_int_21h_0eh(); break;
                   15663:                        case 0x0f: msdos_int_21h_0fh(); break;
                   15664:                        case 0x10: msdos_int_21h_10h(); break;
                   15665:                        case 0x11: msdos_int_21h_11h(); break;
                   15666:                        case 0x12: msdos_int_21h_12h(); break;
                   15667:                        case 0x13: msdos_int_21h_13h(); break;
                   15668:                        case 0x14: msdos_int_21h_14h(); break;
                   15669:                        case 0x15: msdos_int_21h_15h(); break;
                   15670:                        case 0x16: msdos_int_21h_16h(); break;
                   15671:                        case 0x17: msdos_int_21h_17h(); break;
                   15672:                        case 0x18: msdos_int_21h_18h(); break;
                   15673:                        case 0x19: msdos_int_21h_19h(); break;
                   15674:                        case 0x1a: msdos_int_21h_1ah(); break;
                   15675:                        case 0x1b: msdos_int_21h_1bh(); break;
                   15676:                        case 0x1c: msdos_int_21h_1ch(); break;
                   15677:                        case 0x1d: msdos_int_21h_1dh(); break;
                   15678:                        case 0x1e: msdos_int_21h_1eh(); break;
                   15679:                        case 0x1f: msdos_int_21h_1fh(); break;
                   15680:                        case 0x20: msdos_int_21h_20h(); break;
                   15681:                        case 0x21: msdos_int_21h_21h(); break;
                   15682:                        case 0x22: msdos_int_21h_22h(); break;
                   15683:                        case 0x23: msdos_int_21h_23h(); break;
                   15684:                        case 0x24: msdos_int_21h_24h(); break;
                   15685:                        case 0x25: msdos_int_21h_25h(); break;
                   15686:                        case 0x26: msdos_int_21h_26h(); break;
                   15687:                        case 0x27: msdos_int_21h_27h(); break;
                   15688:                        case 0x28: msdos_int_21h_28h(); break;
                   15689:                        case 0x29: msdos_int_21h_29h(); break;
                   15690:                        case 0x2a: msdos_int_21h_2ah(); break;
                   15691:                        case 0x2b: msdos_int_21h_2bh(); break;
                   15692:                        case 0x2c: msdos_int_21h_2ch(); break;
                   15693:                        case 0x2d: msdos_int_21h_2dh(); break;
                   15694:                        case 0x2e: msdos_int_21h_2eh(); break;
                   15695:                        case 0x2f: msdos_int_21h_2fh(); break;
                   15696:                        case 0x30: msdos_int_21h_30h(); break;
                   15697:                        case 0x31: msdos_int_21h_31h(); break;
                   15698:                        case 0x32: msdos_int_21h_32h(); break;
                   15699:                        case 0x33: msdos_int_21h_33h(); break;
                   15700:                        case 0x34: msdos_int_21h_34h(); break;
                   15701:                        case 0x35: msdos_int_21h_35h(); break;
                   15702:                        case 0x36: msdos_int_21h_36h(); break;
                   15703:                        case 0x37: msdos_int_21h_37h(); break;
                   15704:                        case 0x38: msdos_int_21h_38h(); break;
                   15705:                        case 0x39: msdos_int_21h_39h(0); break;
                   15706:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   15707:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   15708:                        case 0x3c: msdos_int_21h_3ch(); break;
                   15709:                        case 0x3d: msdos_int_21h_3dh(); break;
                   15710:                        case 0x3e: msdos_int_21h_3eh(); break;
                   15711:                        case 0x3f: msdos_int_21h_3fh(); break;
                   15712:                        case 0x40: msdos_int_21h_40h(); break;
                   15713:                        case 0x41: msdos_int_21h_41h(0); break;
                   15714:                        case 0x42: msdos_int_21h_42h(); break;
                   15715:                        case 0x43: msdos_int_21h_43h(0); break;
                   15716:                        case 0x44: msdos_int_21h_44h(); break;
                   15717:                        case 0x45: msdos_int_21h_45h(); break;
                   15718:                        case 0x46: msdos_int_21h_46h(); break;
                   15719:                        case 0x47: msdos_int_21h_47h(0); break;
                   15720:                        case 0x48: msdos_int_21h_48h(); break;
                   15721:                        case 0x49: msdos_int_21h_49h(); break;
                   15722:                        case 0x4a: msdos_int_21h_4ah(); break;
                   15723:                        case 0x4b: msdos_int_21h_4bh(); break;
                   15724:                        case 0x4c: msdos_int_21h_4ch(); break;
                   15725:                        case 0x4d: msdos_int_21h_4dh(); break;
                   15726:                        case 0x4e: msdos_int_21h_4eh(); break;
                   15727:                        case 0x4f: msdos_int_21h_4fh(); break;
                   15728:                        case 0x50: msdos_int_21h_50h(); break;
                   15729:                        case 0x51: msdos_int_21h_51h(); break;
                   15730:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     15731:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     15732:                        case 0x54: msdos_int_21h_54h(); break;
                   15733:                        case 0x55: msdos_int_21h_55h(); break;
                   15734:                        case 0x56: msdos_int_21h_56h(0); break;
                   15735:                        case 0x57: msdos_int_21h_57h(); break;
                   15736:                        case 0x58: msdos_int_21h_58h(); break;
                   15737:                        case 0x59: msdos_int_21h_59h(); break;
                   15738:                        case 0x5a: msdos_int_21h_5ah(); break;
                   15739:                        case 0x5b: msdos_int_21h_5bh(); break;
                   15740:                        case 0x5c: msdos_int_21h_5ch(); break;
                   15741:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     15742:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     15743:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     15744:                        case 0x60: msdos_int_21h_60h(0); break;
                   15745:                        case 0x61: msdos_int_21h_61h(); break;
                   15746:                        case 0x62: msdos_int_21h_62h(); break;
                   15747:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     15748:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     15749:                        case 0x65: msdos_int_21h_65h(); break;
                   15750:                        case 0x66: msdos_int_21h_66h(); break;
                   15751:                        case 0x67: msdos_int_21h_67h(); break;
                   15752:                        case 0x68: msdos_int_21h_68h(); break;
                   15753:                        case 0x69: msdos_int_21h_69h(); break;
                   15754:                        case 0x6a: msdos_int_21h_6ah(); break;
                   15755:                        case 0x6b: msdos_int_21h_6bh(); break;
                   15756:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.33  root     15757:                        // 0x6d: Find First ROM Program
                   15758:                        // 0x6e: Find Next ROM Program
                   15759:                        // 0x6f: Get/Set ROM Scan Start Address
1.1.1.43  root     15760:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.28  root     15761:                        case 0x71:
1.1.1.33  root     15762:                                // Windows95 - Long Filename Functions
1.1.1.28  root     15763:                                switch(REG8(AL)) {
                   15764:                                case 0x0d: msdos_int_21h_710dh(); break;
                   15765:                                case 0x39: msdos_int_21h_39h(1); break;
                   15766:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   15767:                                case 0x3b: msdos_int_21h_3bh(1); break;
                   15768:                                case 0x41: msdos_int_21h_7141h(1); break;
                   15769:                                case 0x43: msdos_int_21h_43h(1); break;
                   15770:                                case 0x47: msdos_int_21h_47h(1); break;
                   15771:                                case 0x4e: msdos_int_21h_714eh(); break;
                   15772:                                case 0x4f: msdos_int_21h_714fh(); break;
                   15773:                                case 0x56: msdos_int_21h_56h(1); break;
                   15774:                                case 0x60: msdos_int_21h_60h(1); break;
                   15775:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   15776:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   15777:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   15778:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   15779:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   15780:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.33  root     15781:                                // 0xa9: Server Create/Open File
1.1.1.28  root     15782:                                case 0xaa: msdos_int_21h_71aah(); break;
                   15783:                                default:
                   15784:                                        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));
                   15785:                                        REG16(AX) = 0x7100;
                   15786:                                        m_CF = 1;
                   15787:                                        break;
                   15788:                                }
                   15789:                                break;
                   15790:                        // 0x72: Windows95 beta - LFN FindClose
                   15791:                        case 0x73:
1.1.1.33  root     15792:                                // Windows95 - FAT32 Functions
1.1.1.28  root     15793:                                switch(REG8(AL)) {
                   15794:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     15795:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     15796:                                case 0x02: msdos_int_21h_7302h(); break;
                   15797:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     15798:                                // 0x04: Set DPB to Use for Formatting
                   15799:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     15800:                                default:
                   15801:                                        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));
                   15802:                                        REG16(AX) = 0x7300;
                   15803:                                        m_CF = 1;
                   15804:                                        break;
                   15805:                                }
1.1       root     15806:                                break;
1.1.1.30  root     15807:                        case 0xdb: msdos_int_21h_dbh(); break;
                   15808:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     15809:                        default:
1.1.1.22  root     15810:                                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     15811:                                REG16(AX) = 0x01;
1.1.1.3   root     15812:                                m_CF = 1;
1.1       root     15813:                                break;
                   15814:                        }
1.1.1.28  root     15815:                } catch(int error) {
                   15816:                        REG16(AX) = error;
                   15817:                        m_CF = 1;
                   15818:                } catch(...) {
                   15819:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     15820:                        m_CF = 1;
1.1       root     15821:                }
1.1.1.3   root     15822:                if(m_CF) {
1.1.1.23  root     15823:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   15824:                        sda->extended_error_code = REG16(AX);
                   15825:                        switch(sda->extended_error_code) {
                   15826:                        case  4: // Too many open files
                   15827:                        case  8: // Insufficient memory
                   15828:                                sda->error_class = 1; // Out of resource
                   15829:                                break;
                   15830:                        case  5: // Access denied
                   15831:                                sda->error_class = 3; // Authorization
                   15832:                                break;
                   15833:                        case  7: // Memory control block destroyed
                   15834:                                sda->error_class = 4; // Internal
                   15835:                                break;
                   15836:                        case  2: // File not found
                   15837:                        case  3: // Path not found
                   15838:                        case 15: // Invaid drive specified
                   15839:                        case 18: // No more files
                   15840:                                sda->error_class = 8; // Not found
                   15841:                                break;
                   15842:                        case 32: // Sharing violation
                   15843:                        case 33: // Lock violation
                   15844:                                sda->error_class = 10; // Locked
                   15845:                                break;
                   15846: //                     case 16: // Removal of current directory attempted
                   15847:                        case 19: // Attempted write on protected disk
                   15848:                        case 21: // Drive not ready
                   15849: //                     case 29: // Write failure
                   15850: //                     case 30: // Read failure
                   15851: //                     case 82: // Cannot create subdirectory
                   15852:                                sda->error_class = 11; // Media
                   15853:                                break;
                   15854:                        case 80: // File already exists
                   15855:                                sda->error_class = 12; // Already exist
                   15856:                                break;
                   15857:                        default:
                   15858:                                sda->error_class = 13; // Unknown
                   15859:                                break;
                   15860:                        }
                   15861:                        sda->suggested_action = 1; // Retry
                   15862:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     15863:                }
1.1.1.33  root     15864:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     15865:                        // raise int 23h
                   15866: #if defined(HAS_I386)
                   15867:                        m_ext = 0; // not an external interrupt
                   15868:                        i386_trap(0x23, 1, 0);
                   15869:                        m_ext = 1;
                   15870: #else
                   15871:                        PREFIX86(_interrupt)(0x23);
                   15872: #endif
                   15873:                }
1.1       root     15874:                break;
                   15875:        case 0x22:
                   15876:                fatalerror("int 22h (terminate address)\n");
                   15877:        case 0x23:
1.1.1.28  root     15878:                try {
                   15879:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   15880:                } catch(...) {
                   15881:                        fatalerror("failed to terminate the current process by int 23h\n");
                   15882:                }
1.1       root     15883:                break;
                   15884:        case 0x24:
1.1.1.32  root     15885: /*
1.1.1.28  root     15886:                try {
                   15887:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   15888:                } catch(...) {
                   15889:                        fatalerror("failed to terminate the current process by int 24h\n");
                   15890:                }
1.1.1.32  root     15891: */
                   15892:                msdos_int_24h();
1.1       root     15893:                break;
                   15894:        case 0x25:
                   15895:                msdos_int_25h();
                   15896:                break;
                   15897:        case 0x26:
                   15898:                msdos_int_26h();
                   15899:                break;
                   15900:        case 0x27:
1.1.1.28  root     15901:                try {
                   15902:                        msdos_int_27h();
                   15903:                } catch(...) {
                   15904:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   15905:                }
1.1       root     15906:                break;
                   15907:        case 0x28:
                   15908:                Sleep(10);
1.1.1.35  root     15909:                REQUEST_HARDWRE_UPDATE();
1.1       root     15910:                break;
                   15911:        case 0x29:
                   15912:                msdos_int_29h();
                   15913:                break;
                   15914:        case 0x2e:
                   15915:                msdos_int_2eh();
                   15916:                break;
                   15917:        case 0x2f:
                   15918:                // multiplex interrupt
                   15919:                switch(REG8(AH)) {
1.1.1.22  root     15920:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44! root     15921:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     15922:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     15923:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     15924:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     15925:                case 0x14: msdos_int_2fh_14h(); break;
                   15926:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     15927:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     15928:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     15929:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     15930:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     15931:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     15932:                case 0x46: msdos_int_2fh_46h(); break;
                   15933:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     15934:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     15935:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44! root     15936:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     15937:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     15938:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44! root     15939:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     15940:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     15941:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     15942:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     15943:                default:
1.1.1.30  root     15944:                        switch(REG8(AL)) {
                   15945:                        case 0x00:
                   15946:                                // This is not installed
                   15947: //                             REG8(AL) = 0x00;
                   15948:                                break;
1.1.1.33  root     15949:                        case 0x01:
1.1.1.42  root     15950:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   15951:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   15952:                                        break;
                   15953:                                }
1.1.1.33  root     15954:                                // Banyan VINES v4+ is not installed
                   15955:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   15956:                                        break;
                   15957:                                }
1.1.1.42  root     15958:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   15959:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   15960:                                        break;
                   15961:                                }
1.1.1.30  root     15962:                        default:
1.1.1.42  root     15963:                                // NORTON UTILITIES 5.0+
                   15964:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   15965:                                        break;
                   15966:                                }
1.1.1.30  root     15967:                                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     15968:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     15969:                                m_CF = 1;
                   15970:                                break;
                   15971:                        }
                   15972:                        break;
1.1       root     15973:                }
                   15974:                break;
1.1.1.24  root     15975:        case 0x33:
                   15976:                switch(REG8(AH)) {
                   15977:                case 0x00:
                   15978:                        // Mouse
                   15979:                        switch(REG8(AL)) {
                   15980:                        case 0x00: msdos_int_33h_0000h(); break;
                   15981:                        case 0x01: msdos_int_33h_0001h(); break;
                   15982:                        case 0x02: msdos_int_33h_0002h(); break;
                   15983:                        case 0x03: msdos_int_33h_0003h(); break;
1.1.1.34  root     15984:                        case 0x04: msdos_int_33h_0004h(); break;
1.1.1.24  root     15985:                        case 0x05: msdos_int_33h_0005h(); break;
                   15986:                        case 0x06: msdos_int_33h_0006h(); break;
                   15987:                        case 0x07: msdos_int_33h_0007h(); break;
                   15988:                        case 0x08: msdos_int_33h_0008h(); break;
                   15989:                        case 0x09: msdos_int_33h_0009h(); break;
1.1.1.34  root     15990:                        case 0x0a: break; // Define Text Cursor
1.1.1.24  root     15991:                        case 0x0b: msdos_int_33h_000bh(); break;
                   15992:                        case 0x0c: msdos_int_33h_000ch(); break;
1.1.1.34  root     15993:                        case 0x0d: break; // Light Pen Emulation On
                   15994:                        case 0x0e: break; // Light Pen Emulation Off
1.1.1.24  root     15995:                        case 0x0f: msdos_int_33h_000fh(); break;
1.1.1.34  root     15996:                        case 0x10: break; // Define Screen Region for Updating
1.1.1.24  root     15997:                        case 0x11: msdos_int_33h_0011h(); break;
1.1.1.34  root     15998:                        case 0x12: REG16(AX) = 0xffff; break; // Set Large Graphics Cursor Block
                   15999:                        case 0x13: break; // Define Double-Speed Threshold
1.1.1.24  root     16000:                        case 0x14: msdos_int_33h_0014h(); break;
                   16001:                        case 0x15: msdos_int_33h_0015h(); break;
                   16002:                        case 0x16: msdos_int_33h_0016h(); break;
                   16003:                        case 0x17: msdos_int_33h_0017h(); break;
1.1.1.43  root     16004:                        case 0x18: msdos_int_33h_0018h(); break;
                   16005:                        case 0x19: msdos_int_33h_0019h(); break;
1.1.1.24  root     16006:                        case 0x1a: msdos_int_33h_001ah(); break;
                   16007:                        case 0x1b: msdos_int_33h_001bh(); break;
                   16008:                        case 0x1d: msdos_int_33h_001dh(); break;
                   16009:                        case 0x1e: msdos_int_33h_001eh(); break;
1.1.1.34  root     16010:                        case 0x1f: msdos_int_33h_001fh(); break;
                   16011:                        case 0x20: msdos_int_33h_0020h(); break;
1.1.1.24  root     16012:                        case 0x21: msdos_int_33h_0021h(); break;
                   16013:                        case 0x22: msdos_int_33h_0022h(); break;
                   16014:                        case 0x23: msdos_int_33h_0023h(); break;
                   16015:                        case 0x24: msdos_int_33h_0024h(); break;
                   16016:                        case 0x26: msdos_int_33h_0026h(); break;
                   16017:                        case 0x2a: msdos_int_33h_002ah(); break;
1.1.1.34  root     16018:                        case 0x2f: break; // Mouse Hardware Reset
1.1.1.24  root     16019:                        case 0x31: msdos_int_33h_0031h(); break;
                   16020:                        case 0x32: msdos_int_33h_0032h(); break;
                   16021:                        default:
                   16022:                                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));
                   16023:                                break;
                   16024:                        }
                   16025:                        break;
                   16026:                default:
                   16027:                        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));
                   16028:                        break;
                   16029:                }
                   16030:                break;
1.1.1.19  root     16031:        case 0x68:
                   16032:                // dummy interrupt for EMS (int 67h)
                   16033:                switch(REG8(AH)) {
                   16034:                case 0x40: msdos_int_67h_40h(); break;
                   16035:                case 0x41: msdos_int_67h_41h(); break;
                   16036:                case 0x42: msdos_int_67h_42h(); break;
                   16037:                case 0x43: msdos_int_67h_43h(); break;
                   16038:                case 0x44: msdos_int_67h_44h(); break;
                   16039:                case 0x45: msdos_int_67h_45h(); break;
                   16040:                case 0x46: msdos_int_67h_46h(); break;
                   16041:                case 0x47: msdos_int_67h_47h(); break;
                   16042:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     16043:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   16044:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     16045:                case 0x4b: msdos_int_67h_4bh(); break;
                   16046:                case 0x4c: msdos_int_67h_4ch(); break;
                   16047:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     16048:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     16049:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     16050:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     16051:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     16052:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     16053:                case 0x53: msdos_int_67h_53h(); break;
                   16054:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.20  root     16055:                // 0x55: LIM EMS 4.0 - Alter Page Map And JUMP
                   16056:                // 0x56: LIM EMS 4.0 - Alter Page Map And CALL
                   16057:                case 0x57: msdos_int_67h_57h(); break;
                   16058:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     16059:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     16060:                case 0x5a: msdos_int_67h_5ah(); break;
                   16061:                // 0x5b: LIM EMS 4.0 - Alternate Map Register Set (for DOS Kernel)
1.1.1.43  root     16062:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   16063:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.31  root     16064:                // 0x60: EEMS - Get Physical Window Array
                   16065:                // 0x61: EEMS - Generic Accelerator Card Support
                   16066:                // 0x68: EEMS - Get Address of All Pge Frames om System
                   16067:                // 0x69: EEMS - Map Page into Frame
                   16068:                // 0x6a: EEMS - Page Mapping
                   16069:                // 0xde: VCPI
1.1.1.30  root     16070:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     16071:                default:
1.1.1.22  root     16072:                        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     16073:                        REG8(AH) = 0x84;
                   16074:                        break;
                   16075:                }
                   16076:                break;
                   16077: #ifdef SUPPORT_XMS
                   16078:        case 0x69:
                   16079:                // dummy interrupt for XMS (call far)
1.1.1.28  root     16080:                try {
                   16081:                        switch(REG8(AH)) {
                   16082:                        case 0x00: msdos_call_xms_00h(); break;
                   16083:                        case 0x01: msdos_call_xms_01h(); break;
                   16084:                        case 0x02: msdos_call_xms_02h(); break;
                   16085:                        case 0x03: msdos_call_xms_03h(); break;
                   16086:                        case 0x04: msdos_call_xms_04h(); break;
                   16087:                        case 0x05: msdos_call_xms_05h(); break;
                   16088:                        case 0x06: msdos_call_xms_06h(); break;
                   16089:                        case 0x07: msdos_call_xms_07h(); break;
                   16090:                        case 0x08: msdos_call_xms_08h(); break;
                   16091:                        case 0x09: msdos_call_xms_09h(); break;
                   16092:                        case 0x0a: msdos_call_xms_0ah(); break;
                   16093:                        case 0x0b: msdos_call_xms_0bh(); break;
                   16094:                        case 0x0c: msdos_call_xms_0ch(); break;
                   16095:                        case 0x0d: msdos_call_xms_0dh(); break;
                   16096:                        case 0x0e: msdos_call_xms_0eh(); break;
                   16097:                        case 0x0f: msdos_call_xms_0fh(); break;
                   16098:                        case 0x10: msdos_call_xms_10h(); break;
                   16099:                        case 0x11: msdos_call_xms_11h(); break;
                   16100:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     16101: #if defined(HAS_I386)
                   16102:                        case 0x88: msdos_call_xms_88h(); break;
                   16103:                        case 0x89: msdos_call_xms_89h(); break;
                   16104:                        case 0x8e: msdos_call_xms_8eh(); break;
                   16105:                        case 0x8f: msdos_call_xms_8fh(); break;
                   16106: #endif
1.1.1.28  root     16107:                        default:
                   16108:                                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));
                   16109:                                REG16(AX) = 0x0000;
                   16110:                                REG8(BL) = 0x80; // function not implemented
                   16111:                                break;
                   16112:                        }
                   16113:                } catch(...) {
1.1.1.19  root     16114:                        REG16(AX) = 0x0000;
1.1.1.28  root     16115:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     16116:                }
                   16117:                break;
                   16118: #endif
                   16119:        case 0x6a:
1.1.1.24  root     16120:                // irq12 (mouse)
                   16121:                mouse_push_ax = REG16(AX);
                   16122:                mouse_push_bx = REG16(BX);
                   16123:                mouse_push_cx = REG16(CX);
                   16124:                mouse_push_dx = REG16(DX);
                   16125:                mouse_push_si = REG16(SI);
                   16126:                mouse_push_di = REG16(DI);
                   16127:                
1.1.1.43  root     16128:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     16129:                        REG16(AX) = mouse.status_irq;
                   16130:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     16131:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   16132:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     16133:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   16134:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   16135:                        
                   16136:                        mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   16137:                        mem[0xfffd0 + 0x03] = mouse.call_addr.w.l & 0xff;
                   16138:                        mem[0xfffd0 + 0x04] = mouse.call_addr.w.l >> 8;
                   16139:                        mem[0xfffd0 + 0x05] = mouse.call_addr.w.h & 0xff;
                   16140:                        mem[0xfffd0 + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.43  root     16141:                        break;
1.1.1.24  root     16142:                }
1.1.1.43  root     16143:                for(int i = 0; i < 8; i++) {
                   16144:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   16145:                                REG16(AX) = mouse.status_irq_alt;
                   16146:                                REG16(BX) = mouse.get_buttons();
                   16147:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   16148:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   16149:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   16150:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   16151:                                
                   16152:                                mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   16153:                                mem[0xfffd0 + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   16154:                                mem[0xfffd0 + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   16155:                                mem[0xfffd0 + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   16156:                                mem[0xfffd0 + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
                   16157:                                break;
                   16158:                        }
                   16159:                }
                   16160:                // invalid call addr :-(
                   16161:                mem[0xfffd0 + 0x02] = 0x90;     // nop
                   16162:                mem[0xfffd0 + 0x03] = 0x90;     // nop
                   16163:                mem[0xfffd0 + 0x04] = 0x90;     // nop
                   16164:                mem[0xfffd0 + 0x05] = 0x90;     // nop
                   16165:                mem[0xfffd0 + 0x06] = 0x90;     // nop
1.1.1.24  root     16166:                break;
                   16167:        case 0x6b:
                   16168:                // end of irq12 (mouse)
                   16169:                REG16(AX) = mouse_push_ax;
                   16170:                REG16(BX) = mouse_push_bx;
                   16171:                REG16(CX) = mouse_push_cx;
                   16172:                REG16(DX) = mouse_push_dx;
                   16173:                REG16(SI) = mouse_push_si;
                   16174:                REG16(DI) = mouse_push_di;
                   16175:                
                   16176:                // EOI
                   16177:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   16178:                        pic[0].isr &= ~(1 << 2); // master
                   16179:                }
                   16180:                pic_update();
                   16181:                break;
                   16182:        case 0x6c:
1.1.1.19  root     16183:                // dummy interrupt for case map routine pointed in the country info
                   16184:                if(REG8(AL) >= 0x80) {
                   16185:                        char tmp[2] = {0};
                   16186:                        tmp[0] = REG8(AL);
                   16187:                        my_strupr(tmp);
                   16188:                        REG8(AL) = tmp[0];
                   16189:                }
                   16190:                break;
1.1.1.27  root     16191:        case 0x6d:
                   16192:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   16193:                REG8(AL) = 0x86; // not supported
                   16194:                m_CF = 1;
                   16195:                break;
1.1.1.32  root     16196:        case 0x6e:
                   16197:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   16198:                {
                   16199:                        UINT16 code = REG16(AX);
                   16200:                        if(code & 0xf0) {
                   16201:                                code = (code & 7) | ((code & 0x10) >> 1);
                   16202:                        }
                   16203:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   16204:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   16205:                                        const char *message = NULL;
                   16206:                                        if(active_code_page == 932) {
                   16207:                                                message = param_error_table[i].message_japanese;
                   16208:                                        }
                   16209:                                        if(message == NULL) {
                   16210:                                                message = param_error_table[i].message_english;
                   16211:                                        }
                   16212:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   16213:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   16214:                                        
                   16215:                                        SREG(ES) = WORK_TOP >> 4;
                   16216:                                        i386_load_segment_descriptor(ES);
                   16217:                                        REG16(DI) = 0x0000;
                   16218:                                        break;
                   16219:                                }
                   16220:                        }
                   16221:                }
                   16222:                break;
1.1.1.8   root     16223:        case 0x70:
                   16224:        case 0x71:
                   16225:        case 0x72:
                   16226:        case 0x73:
                   16227:        case 0x74:
                   16228:        case 0x75:
                   16229:        case 0x76:
                   16230:        case 0x77:
                   16231:                // EOI
                   16232:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   16233:                        pic[0].isr &= ~(1 << 2); // master
                   16234:                }
                   16235:                pic_update();
                   16236:                break;
1.1       root     16237:        default:
1.1.1.22  root     16238: //             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     16239:                break;
                   16240:        }
                   16241:        
                   16242:        // update cursor position
                   16243:        if(cursor_moved) {
1.1.1.36  root     16244:                pcbios_update_cursor_position();
1.1       root     16245:                cursor_moved = false;
                   16246:        }
                   16247: }
                   16248: 
                   16249: // init
                   16250: 
                   16251: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   16252: {
                   16253:        // init file handler
                   16254:        memset(file_handler, 0, sizeof(file_handler));
                   16255:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   16256:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   16257:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     16258: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     16259:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     16260: #else
                   16261:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   16262: #endif
                   16263:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     16264:        }
1.1.1.21  root     16265:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.37  root     16266:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
1.1.1.21  root     16267:        }
1.1       root     16268:        _dup2(0, DUP_STDIN);
                   16269:        _dup2(1, DUP_STDOUT);
                   16270:        _dup2(2, DUP_STDERR);
1.1.1.21  root     16271:        _dup2(3, DUP_STDAUX);
                   16272:        _dup2(4, DUP_STDPRN);
1.1       root     16273:        
1.1.1.24  root     16274:        // init mouse
                   16275:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     16276:        mouse.enabled = true;   // from DOSBox
                   16277:        mouse.hidden = 1;       // hidden in default ???
                   16278:        mouse.old_hidden = 1;   // from DOSBox
                   16279:        mouse.max_position.x = 8 * (scr_width  - 1);
                   16280:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     16281:        mouse.mickey.x = 8;
                   16282:        mouse.mickey.y = 16;
                   16283:        
1.1.1.26  root     16284: #ifdef SUPPORT_XMS
                   16285:        // init xms
                   16286:        msdos_xms_init();
                   16287: #endif
                   16288:        
1.1       root     16289:        // init process
                   16290:        memset(process, 0, sizeof(process));
                   16291:        
1.1.1.13  root     16292:        // init dtainfo
                   16293:        msdos_dta_info_init();
                   16294:        
1.1       root     16295:        // init memory
                   16296:        memset(mem, 0, sizeof(mem));
                   16297:        
                   16298:        // bios data area
1.1.1.23  root     16299:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     16300:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   16301:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     16302:        CONSOLE_FONT_INFO cfi;
                   16303:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   16304:        
                   16305:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   16306:        text_vram_top_address = TEXT_VRAM_TOP;
                   16307:        text_vram_end_address = text_vram_top_address + regen;
                   16308:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   16309:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   16310:        
                   16311:        if(regen > 0x4000) {
                   16312:                regen = 0x8000;
                   16313:                vram_pages = 1;
                   16314:        } else if(regen > 0x2000) {
                   16315:                regen = 0x4000;
                   16316:                vram_pages = 2;
                   16317:        } else if(regen > 0x1000) {
                   16318:                regen = 0x2000;
                   16319:                vram_pages = 4;
                   16320:        } else {
                   16321:                regen = 0x1000;
                   16322:                vram_pages = 8;
                   16323:        }
1.1       root     16324:        
1.1.1.25  root     16325:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   16326:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     16327:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   16328:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     16329:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     16330:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   16331:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     16332: #ifdef EXT_BIOS_TOP
1.1.1.25  root     16333:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     16334: #endif
1.1.1.26  root     16335:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     16336:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     16337:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   16338:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     16339:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     16340:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   16341:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     16342:        *(UINT16 *)(mem + 0x44e) = 0;
                   16343:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     16344:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     16345:        *(UINT8  *)(mem + 0x460) = 7;
                   16346:        *(UINT8  *)(mem + 0x461) = 7;
                   16347:        *(UINT8  *)(mem + 0x462) = 0;
                   16348:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     16349:        *(UINT8  *)(mem + 0x465) = 0x09;
                   16350:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     16351:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     16352:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   16353:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     16354:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   16355:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   16356:        *(UINT8  *)(mem + 0x487) = 0x60;
                   16357:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     16358: #ifdef EXT_BIOS_TOP
1.1.1.25  root     16359:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     16360: #endif
1.1.1.14  root     16361:        
                   16362:        // initial screen
                   16363:        SMALL_RECT rect;
                   16364:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   16365:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   16366:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   16367:                for(int x = 0; x < scr_width; x++) {
                   16368:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   16369:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   16370:                }
                   16371:        }
1.1       root     16372:        
1.1.1.19  root     16373:        // init mcb
1.1       root     16374:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     16375:        
                   16376:        // iret table
                   16377:        // note: int 2eh vector should address the routine in command.com,
                   16378:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   16379:        // so move iret table into allocated memory block
                   16380:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33  root     16381:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19  root     16382:        IRET_TOP = seg << 4;
                   16383:        seg += IRET_SIZE >> 4;
1.1.1.25  root     16384:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     16385:        
                   16386:        // dummy xms/ems device
1.1.1.33  root     16387:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     16388:        XMS_TOP = seg << 4;
                   16389:        seg += XMS_SIZE >> 4;
                   16390:        
                   16391:        // environment
1.1.1.33  root     16392:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     16393:        int env_seg = seg;
                   16394:        int ofs = 0;
1.1.1.32  root     16395:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   16396:        char comspec_added = 0;
1.1.1.33  root     16397:        char lastdrive_added = 0;
1.1.1.32  root     16398:        char env_msdos_path[ENV_SIZE] = {0};
                   16399:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     16400:        char prompt_added = 0;
1.1.1.32  root     16401:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     16402:        char tz_added = 0;
1.1.1.32  root     16403:        char *path, *short_path;
                   16404:        
                   16405:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   16406:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16407:                        strcpy(env_append, short_path);
                   16408:                }
                   16409:        }
                   16410:        if((path = getenv("APPEND")) != NULL) {
                   16411:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16412:                        if(env_append[0] != '\0') {
                   16413:                                strcat(env_append, ";");
                   16414:                        }
                   16415:                        strcat(env_append, short_path);
                   16416:                }
                   16417:        }
                   16418:        
                   16419:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   16420:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16421:                        strcpy(comspec_path, short_path);
                   16422:                }
                   16423:        }
                   16424:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   16425:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16426:                        strcpy(comspec_path, short_path);
                   16427:                }
                   16428:        }
1.1       root     16429:        
1.1.1.28  root     16430:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     16431:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16432:                        strcpy(env_msdos_path, short_path);
                   16433:                        strcpy(env_path, short_path);
1.1.1.14  root     16434:                }
                   16435:        }
1.1.1.28  root     16436:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     16437:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16438:                        if(env_path[0] != '\0') {
                   16439:                                strcat(env_path, ";");
                   16440:                        }
                   16441:                        strcat(env_path, short_path);
1.1.1.9   root     16442:                }
                   16443:        }
1.1.1.32  root     16444:        
                   16445:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   16446:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     16447:        }
1.1.1.32  root     16448:        for(int i = 0; i < 4; i++) {
                   16449:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   16450:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   16451:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16452:                                strcpy(env_temp, short_path);
                   16453:                                break;
                   16454:                        }
                   16455:                }
1.1.1.24  root     16456:        }
1.1.1.32  root     16457:        
1.1.1.9   root     16458:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     16459:                // lower to upper
1.1.1.28  root     16460:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     16461:                strcpy(tmp, *p);
                   16462:                for(int i = 0;; i++) {
                   16463:                        if(tmp[i] == '=') {
                   16464:                                tmp[i] = '\0';
                   16465:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     16466:                                my_strupr(name);
1.1       root     16467:                                tmp[i] = '=';
                   16468:                                break;
                   16469:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     16470:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     16471:                        }
                   16472:                }
1.1.1.33  root     16473:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   16474:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   16475:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     16476:                        // ignore non standard environments
                   16477:                } else {
1.1.1.33  root     16478:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     16479:                                if(env_append[0] != '\0') {
                   16480:                                        sprintf(tmp, "APPEND=%s", env_append);
                   16481:                                } else {
                   16482:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   16483:                                }
                   16484:                                append_added = 1;
                   16485:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     16486:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     16487:                                comspec_added = 1;
1.1.1.33  root     16488:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   16489:                                char *env = getenv("MSDOS_LASTDRIVE");
                   16490:                                if(env != NULL) {
                   16491:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   16492:                                }
                   16493:                                lastdrive_added = 1;
                   16494:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     16495:                                if(env_msdos_path[0] != '\0') {
                   16496:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   16497:                                } else {
                   16498:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   16499:                                }
1.1.1.33  root     16500:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     16501:                                if(env_path[0] != '\0') {
                   16502:                                        sprintf(tmp, "PATH=%s", env_path);
                   16503:                                } else {
                   16504:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   16505:                                }
1.1.1.32  root     16506:                                path_added = 1;
1.1.1.33  root     16507:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   16508:                                prompt_added = 1;
1.1.1.28  root     16509:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   16510:                                if(env_temp[0] != '\0') {
                   16511:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   16512:                                } else {
                   16513:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   16514:                                }
1.1.1.32  root     16515:                                temp_added = 1;
1.1.1.33  root     16516:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     16517:                                if(env_temp[0] != '\0') {
                   16518:                                        sprintf(tmp, "TMP=%s", env_temp);
                   16519:                                } else {
                   16520:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     16521:                                }
1.1.1.32  root     16522:                                tmp_added = 1;
1.1.1.33  root     16523:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   16524:                                char *env = getenv("MSDOS_TZ");
                   16525:                                if(env != NULL) {
                   16526:                                        sprintf(tmp, "TZ=%s", env);
                   16527:                                }
                   16528:                                tz_added = 1;
1.1       root     16529:                        }
                   16530:                        int len = strlen(tmp);
1.1.1.14  root     16531:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     16532:                                fatalerror("too many environments\n");
                   16533:                        }
                   16534:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   16535:                        ofs += len + 1;
                   16536:                }
                   16537:        }
1.1.1.32  root     16538:        if(!append_added && env_append[0] != '\0') {
                   16539:                #define SET_ENV(name, value) { \
                   16540:                        char tmp[ENV_SIZE]; \
                   16541:                        sprintf(tmp, "%s=%s", name, value); \
                   16542:                        int len = strlen(tmp); \
                   16543:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   16544:                                fatalerror("too many environments\n"); \
                   16545:                        } \
                   16546:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   16547:                        ofs += len + 1; \
                   16548:                }
                   16549:                SET_ENV("APPEND", env_append);
                   16550:        }
                   16551:        if(!comspec_added) {
                   16552:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   16553:        }
1.1.1.33  root     16554:        if(!lastdrive_added) {
                   16555:                SET_ENV("LASTDRIVE", "Z");
                   16556:        }
1.1.1.32  root     16557:        if(!path_added) {
                   16558:                SET_ENV("PATH", env_path);
                   16559:        }
1.1.1.33  root     16560:        if(!prompt_added) {
                   16561:                SET_ENV("PROMPT", "$P$G");
                   16562:        }
1.1.1.32  root     16563:        if(!temp_added) {
                   16564:                SET_ENV("TEMP", env_temp);
                   16565:        }
                   16566:        if(!tmp_added) {
                   16567:                SET_ENV("TMP", env_temp);
                   16568:        }
1.1.1.33  root     16569:        if(!tz_added) {
                   16570:                TIME_ZONE_INFORMATION tzi;
                   16571:                HKEY hKey, hSubKey;
                   16572:                char tzi_std_name[64];
                   16573:                char tz_std[8] = "GMT";
                   16574:                char tz_dlt[8] = "GST";
                   16575:                char tz_value[32];
                   16576:                
                   16577:                // timezone name from GetTimeZoneInformation may not be english
                   16578:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   16579:                setlocale(LC_CTYPE, "");
                   16580:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   16581:                
                   16582:                // get english timezone name from registry
                   16583:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   16584:                        for(DWORD i = 0; !tz_added; i++) {
                   16585:                                char reg_name[256], sub_key[1024], std_name[256];
                   16586:                                DWORD size;
                   16587:                                FILETIME ftTime;
                   16588:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   16589:                                
                   16590:                                if(result == ERROR_SUCCESS) {
                   16591:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   16592:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   16593:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   16594:                                                        // search english timezone name from table
1.1.1.37  root     16595:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     16596:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   16597:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   16598:                                                                                if(tz_table[j].std != NULL) {
                   16599:                                                                                        strcpy(tz_std, tz_table[j].std);
                   16600:                                                                                }
                   16601:                                                                                if(tz_table[j].dlt != NULL) {
                   16602:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   16603:                                                                                }
                   16604:                                                                                tz_added = 1;
                   16605:                                                                                break;
                   16606:                                                                        }
                   16607:                                                                }
                   16608:                                                        }
                   16609:                                                }
                   16610:                                                RegCloseKey(hSubKey);
                   16611:                                        }
                   16612:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   16613:                                        break;
                   16614:                                }
                   16615:                        }
                   16616:                        RegCloseKey(hKey);
                   16617:                }
                   16618:                if((tzi.Bias % 60) != 0) {
                   16619:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   16620:                } else {
                   16621:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   16622:                }
                   16623:                if(daylight) {
                   16624:                        strcat(tz_value, tz_dlt);
                   16625:                }
                   16626:                SET_ENV("TZ", tz_value);
                   16627:        }
1.1       root     16628:        seg += (ENV_SIZE >> 4);
                   16629:        
                   16630:        // psp
1.1.1.33  root     16631:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     16632:        current_psp = seg;
1.1.1.35  root     16633:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   16634:        psp->parent_psp = current_psp;
1.1       root     16635:        seg += (PSP_SIZE >> 4);
                   16636:        
1.1.1.19  root     16637:        // first free mcb in conventional memory
1.1.1.33  root     16638:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     16639:        first_mcb = seg;
                   16640:        
1.1.1.19  root     16641:        // dummy mcb to link to umb
1.1.1.33  root     16642: #if 0
1.1.1.39  root     16643:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     16644: #else
1.1.1.39  root     16645:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     16646: #endif
1.1.1.19  root     16647:        
                   16648:        // first free mcb in upper memory block
1.1.1.8   root     16649:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     16650:        
1.1.1.29  root     16651: #ifdef SUPPORT_HMA
                   16652:        // first free mcb in high memory area
                   16653:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   16654: #endif
                   16655:        
1.1.1.26  root     16656:        // interrupt vector
                   16657:        for(int i = 0; i < 0x80; i++) {
                   16658:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   16659:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   16660:        }
1.1.1.35  root     16661:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffd:0018 irq0 (system timer)
1.1.1.26  root     16662:        *(UINT16 *)(mem + 4 * 0x08 + 2) = 0xfffd;
                   16663:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   16664:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   16665:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   16666:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
                   16667:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffd:0000 irq12 (mouse)
                   16668:        *(UINT16 *)(mem + 4 * 0x74 + 2) = 0xfffd;
                   16669:        
1.1.1.29  root     16670:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     16671:        static const struct {
                   16672:                UINT16 attributes;
                   16673:                char *dev_name;
                   16674:        } dummy_devices[] = {
                   16675:                {0x8013, "CON     "},
                   16676:                {0x8000, "AUX     "},
                   16677:                {0xa0c0, "PRN     "},
                   16678:                {0x8008, "CLOCK$  "},
                   16679:                {0x8000, "COM1    "},
                   16680:                {0xa0c0, "LPT1    "},
                   16681:                {0xa0c0, "LPT2    "},
                   16682:                {0xa0c0, "LPT3    "},
                   16683:                {0x8000, "COM2    "},
                   16684:                {0x8000, "COM3    "},
                   16685:                {0x8000, "COM4    "},
1.1.1.30  root     16686: //             {0xc000, "CONFIG$ "},
                   16687:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     16688:        };
                   16689:        static const UINT8 dummy_device_routine[] = {
                   16690:                // from NUL device of Windows 98 SE
                   16691:                // or word ptr ES:[BX+03],0100
                   16692:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   16693:                // retf
                   16694:                0xcb,
                   16695:        };
1.1.1.29  root     16696:        device_t *last = NULL;
1.1.1.32  root     16697:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     16698:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     16699:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   16700:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     16701:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     16702:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   16703:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     16704:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     16705:                last = device;
                   16706:        }
                   16707:        if(last != NULL) {
                   16708:                last->next_driver.w.l = 0;
                   16709:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     16710:        }
1.1.1.29  root     16711:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     16712:        
1.1.1.25  root     16713:        // dos info
                   16714:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   16715:        dos_info->magic_word = 1;
                   16716:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   16717:        dos_info->first_dpb.w.l = 0;
                   16718:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   16719:        dos_info->first_sft.w.l = 0;
                   16720:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     16721:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     16722:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     16723:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     16724:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   16725:        dos_info->max_sector_len = 512;
                   16726:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   16727:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   16728:        dos_info->cds.w.l = 0;
                   16729:        dos_info->cds.w.h = CDS_TOP >> 4;
                   16730:        dos_info->fcb_table.w.l = 0;
                   16731:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   16732:        dos_info->last_drive = 'Z' - 'A' + 1;
                   16733:        dos_info->buffers_x = 20;
                   16734:        dos_info->buffers_y = 0;
                   16735:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     16736:        dos_info->nul_device.next_driver.w.l = 22;
                   16737:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     16738:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     16739:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   16740:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     16741:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   16742:        dos_info->disk_buf_heads.w.l = 0;
                   16743:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     16744:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     16745:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   16746:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     16747:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     16748:        
                   16749:        char *env;
                   16750:        if((env = getenv("LASTDRIVE")) != NULL) {
                   16751:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   16752:                        dos_info->last_drive = env[0] - 'A' + 1;
                   16753:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   16754:                        dos_info->last_drive = env[0] - 'a' + 1;
                   16755:                }
                   16756:        }
                   16757:        if((env = getenv("windir")) != NULL) {
                   16758:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   16759:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   16760:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   16761:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   16762:                }
                   16763:        }
                   16764: #if defined(HAS_I386)
                   16765:        dos_info->i386_or_later = 1;
                   16766: #else
                   16767:        dos_info->i386_or_later = 0;
                   16768: #endif
                   16769:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   16770:        
1.1.1.27  root     16771:        // ems (int 67h) and xms
1.1.1.25  root     16772:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   16773:        xms_device->next_driver.w.l = 0xffff;
                   16774:        xms_device->next_driver.w.h = 0xffff;
                   16775:        xms_device->attributes = 0xc000;
1.1.1.29  root     16776:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   16777:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     16778:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   16779:        
1.1.1.26  root     16780:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   16781:        mem[XMS_TOP + 0x13] = 0x68;
                   16782:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     16783: #ifdef SUPPORT_XMS
                   16784:        if(support_xms) {
1.1.1.26  root     16785:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   16786:                mem[XMS_TOP + 0x16] = 0x69;
                   16787:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     16788:        } else
                   16789: #endif
1.1.1.26  root     16790:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     16791:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     16792:        
1.1.1.26  root     16793:        // irq12 routine (mouse)
1.1.1.24  root     16794:        mem[0xfffd0 + 0x00] = 0xcd;     // int 6ah (dummy)
                   16795:        mem[0xfffd0 + 0x01] = 0x6a;
                   16796:        mem[0xfffd0 + 0x02] = 0x9a;     // call far mouse
                   16797:        mem[0xfffd0 + 0x03] = 0xff;
                   16798:        mem[0xfffd0 + 0x04] = 0xff;
                   16799:        mem[0xfffd0 + 0x05] = 0xff;
                   16800:        mem[0xfffd0 + 0x06] = 0xff;
                   16801:        mem[0xfffd0 + 0x07] = 0xcd;     // int 6bh (dummy)
                   16802:        mem[0xfffd0 + 0x08] = 0x6b;
                   16803:        mem[0xfffd0 + 0x09] = 0xcf;     // iret
                   16804:        
1.1.1.27  root     16805:        // case map routine
                   16806:        mem[0xfffd0 + 0x0a] = 0xcd;     // int 6ch (dummy)
                   16807:        mem[0xfffd0 + 0x0b] = 0x6c;
                   16808:        mem[0xfffd0 + 0x0c] = 0xcb;     // retf
                   16809:        
                   16810:        // font read routine
                   16811:        mem[0xfffd0 + 0x0d] = 0xcd;     // int 6dh (dummy)
                   16812:        mem[0xfffd0 + 0x0e] = 0x6d;
                   16813:        mem[0xfffd0 + 0x0f] = 0xcb;     // retf
1.1.1.19  root     16814:        
1.1.1.32  root     16815:        // error message read routine
                   16816:        mem[0xfffd0 + 0x10] = 0xcd;     // int 6eh (dummy)
                   16817:        mem[0xfffd0 + 0x11] = 0x6e;
                   16818:        mem[0xfffd0 + 0x12] = 0xcb;     // retf
                   16819:        
1.1.1.35  root     16820:        // dummy loop to wait BIOS/DOS service is done
                   16821:        mem[0xfffd0 + 0x13] = 0xe6;     // out f7h, al
                   16822:        mem[0xfffd0 + 0x14] = 0xf7;
                   16823:        mem[0xfffd0 + 0x15] = 0x78;     // js/jns -4
                   16824:        mem[0xfffd0 + 0x16] = 0xfc;
                   16825:        mem[0xfffd0 + 0x17] = 0xcb;     // retf
                   16826:        
1.1.1.26  root     16827:        // irq0 routine (system time)
1.1.1.35  root     16828:        mem[0xfffd0 + 0x18] = 0xcd;     // int 1ch
                   16829:        mem[0xfffd0 + 0x19] = 0x1c;
                   16830:        mem[0xfffd0 + 0x1a] = 0xea;     // jmp far (IRET_TOP >> 4):0008
                   16831:        mem[0xfffd0 + 0x1b] = 0x08;
                   16832:        mem[0xfffd0 + 0x1c] = 0x00;
                   16833:        mem[0xfffd0 + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   16834:        mem[0xfffd0 + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
1.1.1.14  root     16835:        
1.1.1.26  root     16836:        // boot routine
1.1       root     16837:        mem[0xffff0] = 0xf4;    // halt
                   16838:        mem[0xffff1] = 0xcd;    // int 21h
                   16839:        mem[0xffff2] = 0x21;
                   16840:        mem[0xffff3] = 0xcb;    // retf
                   16841:        
1.1.1.24  root     16842:        mem[0xffff5] = '0';     // rom date
                   16843:        mem[0xffff6] = '2';
                   16844:        mem[0xffff7] = '/';
                   16845:        mem[0xffff8] = '2';
                   16846:        mem[0xffff9] = '2';
                   16847:        mem[0xffffa] = '/';
                   16848:        mem[0xffffb] = '0';
                   16849:        mem[0xffffc] = '6';
                   16850:        mem[0xffffe] = 0xfc;    // machine id
                   16851:        mem[0xfffff] = 0x00;
                   16852:        
1.1       root     16853:        // param block
                   16854:        // + 0: param block (22bytes)
                   16855:        // +24: fcb1/2 (20bytes)
                   16856:        // +44: command tail (128bytes)
                   16857:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   16858:        param->env_seg = 0;
                   16859:        param->cmd_line.w.l = 44;
                   16860:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   16861:        param->fcb1.w.l = 24;
                   16862:        param->fcb1.w.h = (WORK_TOP >> 4);
                   16863:        param->fcb2.w.l = 24;
                   16864:        param->fcb2.w.h = (WORK_TOP >> 4);
                   16865:        
                   16866:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   16867:        
                   16868:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   16869:        if(argc > 1) {
                   16870:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   16871:                for(int i = 2; i < argc; i++) {
                   16872:                        char tmp[128];
                   16873:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   16874:                        strcpy(cmd_line->cmd, tmp);
                   16875:                }
                   16876:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   16877:        } else {
                   16878:                cmd_line->len = 0;
                   16879:        }
                   16880:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   16881:        
                   16882:        // system file table
1.1.1.21  root     16883:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   16884:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     16885:        
1.1.1.19  root     16886:        // disk buffer header (from DOSBox)
                   16887:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   16888:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   16889:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   16890:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   16891:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   16892:        
1.1       root     16893:        // fcb table
                   16894:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     16895:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     16896:        
1.1.1.41  root     16897:        // drive parameter block
1.1.1.42  root     16898:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     16899:                // may be a floppy drive
1.1.1.44! root     16900:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
        !          16901:                sprintf(cds->path_name, "%c:\\", 'A' + i);
        !          16902:                cds->drive_attrib = 0x4000;     // physical drive
        !          16903:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
        !          16904:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
        !          16905:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
        !          16906:                cds->bs_offset = 2;
        !          16907:                
1.1.1.41  root     16908:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   16909:                dpb->drive_num = i;
                   16910:                dpb->unit_num = i;
1.1.1.43  root     16911:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   16912:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     16913:        }
                   16914:        for(int i = 2; i < 26; i++) {
1.1.1.44! root     16915:                msdos_cds_update(i);
1.1.1.42  root     16916:                UINT16 seg, ofs;
                   16917:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     16918:        }
                   16919:        
1.1.1.17  root     16920:        // nls stuff
                   16921:        msdos_nls_tables_init();
1.1       root     16922:        
                   16923:        // execute command
1.1.1.28  root     16924:        try {
                   16925:                if(msdos_process_exec(argv[0], param, 0)) {
                   16926:                        fatalerror("'%s' not found\n", argv[0]);
                   16927:                }
                   16928:        } catch(...) {
                   16929:                // we should not reach here :-(
                   16930:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     16931:        }
                   16932:        retval = 0;
                   16933:        return(0);
                   16934: }
                   16935: 
                   16936: #define remove_std_file(path) { \
                   16937:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   16938:        if(fd != -1) { \
                   16939:                _lseek(fd, 0, SEEK_END); \
                   16940:                int size = _tell(fd); \
                   16941:                _close(fd); \
                   16942:                if(size == 0) { \
                   16943:                        remove(path); \
                   16944:                } \
                   16945:        } \
                   16946: }
                   16947: 
                   16948: void msdos_finish()
                   16949: {
                   16950:        for(int i = 0; i < MAX_FILES; i++) {
                   16951:                if(file_handler[i].valid) {
                   16952:                        _close(i);
                   16953:                }
                   16954:        }
1.1.1.21  root     16955: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     16956:        remove_std_file("stdaux.txt");
1.1.1.21  root     16957: #endif
1.1.1.30  root     16958: #ifdef SUPPORT_XMS
                   16959:        msdos_xms_finish();
                   16960: #endif
1.1       root     16961:        msdos_dbcs_table_finish();
                   16962: }
                   16963: 
                   16964: /* ----------------------------------------------------------------------------
                   16965:        PC/AT hardware emulation
                   16966: ---------------------------------------------------------------------------- */
                   16967: 
                   16968: void hardware_init()
                   16969: {
1.1.1.3   root     16970:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     16971:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     16972:        m_IF = 1;
1.1.1.3   root     16973: #if defined(HAS_I386)
1.1       root     16974:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   16975:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     16976: #endif
                   16977:        i386_set_a20_line(0);
1.1.1.14  root     16978:        
1.1.1.19  root     16979:        ems_init();
1.1.1.25  root     16980:        dma_init();
1.1       root     16981:        pic_init();
1.1.1.25  root     16982:        pio_init();
1.1.1.8   root     16983: #ifdef PIT_ALWAYS_RUNNING
                   16984:        pit_init();
                   16985: #else
1.1       root     16986:        pit_active = 0;
                   16987: #endif
1.1.1.25  root     16988:        sio_init();
1.1.1.8   root     16989:        cmos_init();
                   16990:        kbd_init();
1.1       root     16991: }
                   16992: 
1.1.1.10  root     16993: void hardware_finish()
                   16994: {
                   16995: #if defined(HAS_I386)
                   16996:        vtlb_free(m_vtlb);
                   16997: #endif
1.1.1.19  root     16998:        ems_finish();
1.1.1.37  root     16999:        pio_finish();
1.1.1.25  root     17000:        sio_finish();
1.1.1.10  root     17001: }
                   17002: 
1.1.1.28  root     17003: void hardware_release()
                   17004: {
                   17005:        // release hardware resources when this program will be terminated abnormally
                   17006: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     17007:        if(fp_debug_log != NULL) {
                   17008:                fclose(fp_debug_log);
                   17009:                fp_debug_log = NULL;
1.1.1.28  root     17010:        }
                   17011: #endif
                   17012: #if defined(HAS_I386)
                   17013:        vtlb_free(m_vtlb);
                   17014: #endif
                   17015:        ems_release();
1.1.1.37  root     17016:        pio_release();
1.1.1.28  root     17017:        sio_release();
                   17018: }
                   17019: 
1.1       root     17020: void hardware_run()
                   17021: {
1.1.1.22  root     17022: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     17023:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     17024:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     17025: #endif
1.1.1.3   root     17026:        while(!m_halted) {
                   17027: #if defined(HAS_I386)
1.1       root     17028:                CPU_EXECUTE_CALL(i386);
1.1.1.14  root     17029:                if(m_eip != m_prev_eip) {
1.1.1.35  root     17030:                        idle_ops++;
                   17031:                }
1.1.1.14  root     17032: #else
1.1.1.35  root     17033:                CPU_EXECUTE_CALL(CPU_MODEL);
1.1.1.14  root     17034:                if(m_pc != m_prevpc) {
1.1.1.35  root     17035:                        idle_ops++;
1.1.1.14  root     17036:                }
1.1.1.35  root     17037: #endif
                   17038:                if(++update_ops == UPDATE_OPS) {
1.1       root     17039:                        hardware_update();
1.1.1.35  root     17040:                        update_ops = 0;
1.1       root     17041:                }
                   17042:        }
1.1.1.22  root     17043: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     17044:        if(fp_debug_log != NULL) {
                   17045:                fclose(fp_debug_log);
                   17046:                fp_debug_log = NULL;
1.1.1.28  root     17047:        }
1.1.1.22  root     17048: #endif
1.1       root     17049: }
                   17050: 
                   17051: void hardware_update()
                   17052: {
1.1.1.8   root     17053:        static UINT32 prev_time = 0;
                   17054:        UINT32 cur_time = timeGetTime();
                   17055:        
                   17056:        if(prev_time != cur_time) {
                   17057:                // update pit and raise irq0
                   17058: #ifndef PIT_ALWAYS_RUNNING
                   17059:                if(pit_active)
                   17060: #endif
                   17061:                {
                   17062:                        if(pit_run(0, cur_time)) {
                   17063:                                pic_req(0, 0, 1);
                   17064:                        }
                   17065:                        pit_run(1, cur_time);
                   17066:                        pit_run(2, cur_time);
                   17067:                }
1.1.1.24  root     17068:                
1.1.1.25  root     17069:                // update sio and raise irq4/3
1.1.1.29  root     17070:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     17071:                        sio_update(c);
                   17072:                }
                   17073:                
1.1.1.24  root     17074:                // update keyboard and mouse
1.1.1.14  root     17075:                static UINT32 prev_tick = 0;
                   17076:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     17077:                
1.1.1.14  root     17078:                if(prev_tick != cur_tick) {
                   17079:                        // update keyboard flags
                   17080:                        UINT8 state;
1.1.1.24  root     17081:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   17082:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   17083:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   17084:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   17085:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   17086:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   17087:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   17088:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     17089:                        mem[0x417] = state;
                   17090:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   17091:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   17092:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   17093:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     17094: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   17095: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     17096:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   17097:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   17098:                        mem[0x418] = state;
                   17099:                        
1.1.1.24  root     17100:                        // update console input if needed
1.1.1.34  root     17101:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     17102:                                update_console_input();
                   17103:                        }
                   17104:                        
                   17105:                        // raise irq1 if key is pressed/released
                   17106:                        if(key_changed) {
1.1.1.8   root     17107:                                pic_req(0, 1, 1);
1.1.1.24  root     17108:                                key_changed = false;
                   17109:                        }
                   17110:                        
                   17111:                        // raise irq12 if mouse status is changed
1.1.1.43  root     17112:                        if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
                   17113:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   17114:                                mouse.status_irq_alt = 0; // ???
1.1.1.24  root     17115:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     17116:                                pic_req(1, 4, 1);
                   17117:                        } else {
                   17118:                                for(int i = 0; i < 8; i++) {
                   17119:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17120:                                                mouse.status_irq = 0; // ???
                   17121:                                                mouse.status_irq_alt = 0;
                   17122:                                                for(int j = 0; j < 8; j++) {
                   17123:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   17124:                                                                mouse.status_irq_alt |=  (1 << j);
                   17125:                                                                mouse.status_alt     &= ~(1 << j);
                   17126:                                                        }
                   17127:                                                }
                   17128:                                                pic_req(1, 4, 1);
                   17129:                                                break;
                   17130:                                        }
                   17131:                                }
1.1.1.8   root     17132:                        }
1.1.1.24  root     17133:                        
1.1.1.14  root     17134:                        prev_tick = cur_tick;
1.1.1.8   root     17135:                }
1.1.1.24  root     17136:                
1.1.1.19  root     17137:                // update daily timer counter
                   17138:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     17139:                
1.1.1.8   root     17140:                prev_time = cur_time;
1.1       root     17141:        }
                   17142: }
                   17143: 
1.1.1.19  root     17144: // ems
                   17145: 
                   17146: void ems_init()
                   17147: {
                   17148:        memset(ems_handles, 0, sizeof(ems_handles));
                   17149:        memset(ems_pages, 0, sizeof(ems_pages));
                   17150:        free_ems_pages = MAX_EMS_PAGES;
                   17151: }
                   17152: 
                   17153: void ems_finish()
                   17154: {
1.1.1.28  root     17155:        ems_release();
                   17156: }
                   17157: 
                   17158: void ems_release()
                   17159: {
1.1.1.31  root     17160:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     17161:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     17162:                        free(ems_handles[i].buffer);
                   17163:                        ems_handles[i].buffer = NULL;
                   17164:                }
                   17165:        }
                   17166: }
                   17167: 
                   17168: void ems_allocate_pages(int handle, int pages)
                   17169: {
                   17170:        if(pages > 0) {
                   17171:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   17172:        } else {
                   17173:                ems_handles[handle].buffer = NULL;
                   17174:        }
                   17175:        ems_handles[handle].pages = pages;
                   17176:        ems_handles[handle].allocated = true;
                   17177:        free_ems_pages -= pages;
                   17178: }
                   17179: 
                   17180: void ems_reallocate_pages(int handle, int pages)
                   17181: {
                   17182:        if(ems_handles[handle].allocated) {
                   17183:                if(ems_handles[handle].pages != pages) {
                   17184:                        UINT8 *new_buffer = NULL;
                   17185:                        
                   17186:                        if(pages > 0) {
                   17187:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   17188:                        }
1.1.1.32  root     17189:                        if(ems_handles[handle].buffer != NULL) {
                   17190:                                if(new_buffer != NULL) {
1.1.1.19  root     17191:                                        if(pages > ems_handles[handle].pages) {
                   17192:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   17193:                                        } else {
                   17194:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   17195:                                        }
                   17196:                                }
                   17197:                                free(ems_handles[handle].buffer);
                   17198:                                ems_handles[handle].buffer = NULL;
                   17199:                        }
                   17200:                        free_ems_pages += ems_handles[handle].pages;
                   17201:                        
                   17202:                        ems_handles[handle].buffer = new_buffer;
                   17203:                        ems_handles[handle].pages = pages;
                   17204:                        free_ems_pages -= pages;
                   17205:                }
                   17206:        } else {
                   17207:                ems_allocate_pages(handle, pages);
                   17208:        }
                   17209: }
                   17210: 
                   17211: void ems_release_pages(int handle)
                   17212: {
                   17213:        if(ems_handles[handle].allocated) {
1.1.1.32  root     17214:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     17215:                        free(ems_handles[handle].buffer);
                   17216:                        ems_handles[handle].buffer = NULL;
                   17217:                }
                   17218:                free_ems_pages += ems_handles[handle].pages;
                   17219:                ems_handles[handle].allocated = false;
                   17220:        }
                   17221: }
                   17222: 
                   17223: void ems_map_page(int physical, int handle, int logical)
                   17224: {
                   17225:        if(ems_pages[physical].mapped) {
                   17226:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   17227:                        return;
                   17228:                }
                   17229:                ems_unmap_page(physical);
                   17230:        }
1.1.1.32  root     17231:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     17232:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   17233:        }
                   17234:        ems_pages[physical].handle = handle;
                   17235:        ems_pages[physical].page = logical;
                   17236:        ems_pages[physical].mapped = true;
                   17237: }
                   17238: 
                   17239: void ems_unmap_page(int physical)
                   17240: {
                   17241:        if(ems_pages[physical].mapped) {
                   17242:                int handle = ems_pages[physical].handle;
                   17243:                int logical = ems_pages[physical].page;
                   17244:                
1.1.1.32  root     17245:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     17246:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   17247:                }
                   17248:                ems_pages[physical].mapped = false;
                   17249:        }
                   17250: }
                   17251: 
1.1.1.25  root     17252: // dma
1.1       root     17253: 
1.1.1.25  root     17254: void dma_init()
1.1       root     17255: {
1.1.1.26  root     17256:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     17257:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     17258: //             for(int ch = 0; ch < 4; ch++) {
                   17259: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   17260: //             }
1.1.1.25  root     17261:                dma_reset(c);
                   17262:        }
1.1       root     17263: }
                   17264: 
1.1.1.25  root     17265: void dma_reset(int c)
1.1       root     17266: {
1.1.1.25  root     17267:        dma[c].low_high = false;
                   17268:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   17269:        dma[c].mask = 0xff;
                   17270: }
                   17271: 
                   17272: void dma_write(int c, UINT32 addr, UINT8 data)
                   17273: {
                   17274:        int ch = (addr >> 1) & 3;
                   17275:        UINT8 bit = 1 << (data & 3);
                   17276:        
                   17277:        switch(addr & 0x0f) {
                   17278:        case 0x00: case 0x02: case 0x04: case 0x06:
                   17279:                if(dma[c].low_high) {
                   17280:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     17281:                } else {
1.1.1.25  root     17282:                        dma[c].ch[ch].bareg.b.l = data;
                   17283:                }
                   17284:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   17285:                dma[c].low_high = !dma[c].low_high;
                   17286:                break;
                   17287:        case 0x01: case 0x03: case 0x05: case 0x07:
                   17288:                if(dma[c].low_high) {
                   17289:                        dma[c].ch[ch].bcreg.b.h = data;
                   17290:                } else {
                   17291:                        dma[c].ch[ch].bcreg.b.l = data;
                   17292:                }
                   17293:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   17294:                dma[c].low_high = !dma[c].low_high;
                   17295:                break;
                   17296:        case 0x08:
                   17297:                // command register
                   17298:                dma[c].cmd = data;
                   17299:                break;
                   17300:        case 0x09:
                   17301:                // dma[c].request register
                   17302:                if(data & 4) {
                   17303:                        if(!(dma[c].req & bit)) {
                   17304:                                dma[c].req |= bit;
                   17305: //                             dma_run(c, ch);
                   17306:                        }
                   17307:                } else {
                   17308:                        dma[c].req &= ~bit;
                   17309:                }
                   17310:                break;
                   17311:        case 0x0a:
                   17312:                // single mask register
                   17313:                if(data & 4) {
                   17314:                        dma[c].mask |= bit;
                   17315:                } else {
                   17316:                        dma[c].mask &= ~bit;
                   17317:                }
                   17318:                break;
                   17319:        case 0x0b:
                   17320:                // mode register
                   17321:                dma[c].ch[data & 3].mode = data;
                   17322:                break;
                   17323:        case 0x0c:
                   17324:                dma[c].low_high = false;
                   17325:                break;
                   17326:        case 0x0d:
                   17327:                // clear master
                   17328:                dma_reset(c);
                   17329:                break;
                   17330:        case 0x0e:
                   17331:                // clear mask register
                   17332:                dma[c].mask = 0;
                   17333:                break;
                   17334:        case 0x0f:
                   17335:                // all mask register
                   17336:                dma[c].mask = data & 0x0f;
                   17337:                break;
                   17338:        }
                   17339: }
                   17340: 
                   17341: UINT8 dma_read(int c, UINT32 addr)
                   17342: {
                   17343:        int ch = (addr >> 1) & 3;
                   17344:        UINT8 val = 0xff;
                   17345:        
                   17346:        switch(addr & 0x0f) {
                   17347:        case 0x00: case 0x02: case 0x04: case 0x06:
                   17348:                if(dma[c].low_high) {
                   17349:                        val = dma[c].ch[ch].areg.b.h;
                   17350:                } else {
                   17351:                        val = dma[c].ch[ch].areg.b.l;
                   17352:                }
                   17353:                dma[c].low_high = !dma[c].low_high;
                   17354:                return(val);
                   17355:        case 0x01: case 0x03: case 0x05: case 0x07:
                   17356:                if(dma[c].low_high) {
                   17357:                        val = dma[c].ch[ch].creg.b.h;
                   17358:                } else {
                   17359:                        val = dma[c].ch[ch].creg.b.l;
                   17360:                }
                   17361:                dma[c].low_high = !dma[c].low_high;
                   17362:                return(val);
                   17363:        case 0x08:
                   17364:                // status register
                   17365:                val = (dma[c].req << 4) | dma[c].tc;
                   17366:                dma[c].tc = 0;
                   17367:                return(val);
                   17368:        case 0x0d:
1.1.1.26  root     17369:                // temporary register (intel 82374 does not support)
1.1.1.25  root     17370:                return(dma[c].tmp & 0xff);
1.1.1.26  root     17371:        case 0x0f:
                   17372:                // mask register (intel 82374 does support)
                   17373:                return(dma[c].mask);
1.1.1.25  root     17374:        }
                   17375:        return(0xff);
                   17376: }
                   17377: 
                   17378: void dma_page_write(int c, int ch, UINT8 data)
                   17379: {
                   17380:        dma[c].ch[ch].pagereg = data;
                   17381: }
                   17382: 
                   17383: UINT8 dma_page_read(int c, int ch)
                   17384: {
                   17385:        return(dma[c].ch[ch].pagereg);
                   17386: }
                   17387: 
                   17388: void dma_run(int c, int ch)
                   17389: {
                   17390:        UINT8 bit = 1 << ch;
                   17391:        
                   17392:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   17393:                // execute dma
                   17394:                while(dma[c].req & bit) {
                   17395:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   17396:                                // memory -> memory
                   17397:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   17398:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   17399:                                
                   17400:                                if(c == 0) {
                   17401:                                        dma[c].tmp = read_byte(saddr);
                   17402:                                        write_byte(daddr, dma[c].tmp);
                   17403:                                } else {
                   17404:                                        dma[c].tmp = read_word(saddr << 1);
                   17405:                                        write_word(daddr << 1, dma[c].tmp);
                   17406:                                }
                   17407:                                if(!(dma[c].cmd & 0x02)) {
                   17408:                                        if(dma[c].ch[0].mode & 0x20) {
                   17409:                                                dma[c].ch[0].areg.w--;
                   17410:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   17411:                                                        dma[c].ch[0].pagereg--;
                   17412:                                                }
                   17413:                                        } else {
                   17414:                                                dma[c].ch[0].areg.w++;
                   17415:                                                if(dma[c].ch[0].areg.w == 0) {
                   17416:                                                        dma[c].ch[0].pagereg++;
                   17417:                                                }
                   17418:                                        }
                   17419:                                }
                   17420:                                if(dma[c].ch[1].mode & 0x20) {
                   17421:                                        dma[c].ch[1].areg.w--;
                   17422:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   17423:                                                dma[c].ch[1].pagereg--;
                   17424:                                        }
                   17425:                                } else {
                   17426:                                        dma[c].ch[1].areg.w++;
                   17427:                                        if(dma[c].ch[1].areg.w == 0) {
                   17428:                                                dma[c].ch[1].pagereg++;
                   17429:                                        }
                   17430:                                }
                   17431:                                
                   17432:                                // check dma condition
                   17433:                                if(dma[c].ch[0].creg.w-- == 0) {
                   17434:                                        if(dma[c].ch[0].mode & 0x10) {
                   17435:                                                // self initialize
                   17436:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   17437:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   17438:                                        } else {
                   17439: //                                             dma[c].mask |= bit;
                   17440:                                        }
                   17441:                                }
                   17442:                                if(dma[c].ch[1].creg.w-- == 0) {
                   17443:                                        // terminal count
                   17444:                                        if(dma[c].ch[1].mode & 0x10) {
                   17445:                                                // self initialize
                   17446:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   17447:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   17448:                                        } else {
                   17449:                                                dma[c].mask |= bit;
                   17450:                                        }
                   17451:                                        dma[c].req &= ~bit;
                   17452:                                        dma[c].tc |= bit;
                   17453:                                }
                   17454:                        } else {
                   17455:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   17456:                                
                   17457:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   17458:                                        // verify
                   17459:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   17460:                                        // io -> memory
                   17461:                                        if(c == 0) {
                   17462:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   17463:                                                write_byte(addr, dma[c].tmp);
                   17464:                                        } else {
                   17465:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   17466:                                                write_word(addr << 1, dma[c].tmp);
                   17467:                                        }
                   17468:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   17469:                                        // memory -> io
                   17470:                                        if(c == 0) {
                   17471:                                                dma[c].tmp = read_byte(addr);
                   17472:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   17473:                                        } else {
                   17474:                                                dma[c].tmp = read_word(addr << 1);
                   17475:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   17476:                                        }
                   17477:                                }
                   17478:                                if(dma[c].ch[ch].mode & 0x20) {
                   17479:                                        dma[c].ch[ch].areg.w--;
                   17480:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   17481:                                                dma[c].ch[ch].pagereg--;
                   17482:                                        }
                   17483:                                } else {
                   17484:                                        dma[c].ch[ch].areg.w++;
                   17485:                                        if(dma[c].ch[ch].areg.w == 0) {
                   17486:                                                dma[c].ch[ch].pagereg++;
                   17487:                                        }
                   17488:                                }
                   17489:                                
                   17490:                                // check dma condition
                   17491:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   17492:                                        // terminal count
                   17493:                                        if(dma[c].ch[ch].mode & 0x10) {
                   17494:                                                // self initialize
                   17495:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   17496:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   17497:                                        } else {
                   17498:                                                dma[c].mask |= bit;
                   17499:                                        }
                   17500:                                        dma[c].req &= ~bit;
                   17501:                                        dma[c].tc |= bit;
                   17502:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   17503:                                        // single mode
                   17504:                                        break;
                   17505:                                }
                   17506:                        }
                   17507:                }
                   17508:        }
                   17509: }
                   17510: 
                   17511: // pic
                   17512: 
                   17513: void pic_init()
                   17514: {
                   17515:        memset(pic, 0, sizeof(pic));
                   17516:        pic[0].imr = pic[1].imr = 0xff;
                   17517:        
                   17518:        // from bochs bios
                   17519:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   17520:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   17521:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   17522:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   17523:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   17524:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   17525:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   17526:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   17527:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   17528: }
                   17529: 
                   17530: void pic_write(int c, UINT32 addr, UINT8 data)
                   17531: {
                   17532:        if(addr & 1) {
                   17533:                if(pic[c].icw2_r) {
                   17534:                        // icw2
                   17535:                        pic[c].icw2 = data;
                   17536:                        pic[c].icw2_r = 0;
                   17537:                } else if(pic[c].icw3_r) {
                   17538:                        // icw3
                   17539:                        pic[c].icw3 = data;
                   17540:                        pic[c].icw3_r = 0;
                   17541:                } else if(pic[c].icw4_r) {
                   17542:                        // icw4
                   17543:                        pic[c].icw4 = data;
                   17544:                        pic[c].icw4_r = 0;
                   17545:                } else {
                   17546:                        // ocw1
1.1       root     17547:                        pic[c].imr = data;
                   17548:                }
                   17549:        } else {
                   17550:                if(data & 0x10) {
                   17551:                        // icw1
                   17552:                        pic[c].icw1 = data;
                   17553:                        pic[c].icw2_r = 1;
                   17554:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   17555:                        pic[c].icw4_r = data & 1;
                   17556:                        pic[c].irr = 0;
                   17557:                        pic[c].isr = 0;
                   17558:                        pic[c].imr = 0;
                   17559:                        pic[c].prio = 0;
                   17560:                        if(!(pic[c].icw1 & 1)) {
                   17561:                                pic[c].icw4 = 0;
                   17562:                        }
                   17563:                        pic[c].ocw3 = 0;
                   17564:                } else if(data & 8) {
                   17565:                        // ocw3
                   17566:                        if(!(data & 2)) {
                   17567:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   17568:                        }
                   17569:                        if(!(data & 0x40)) {
                   17570:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   17571:                        }
                   17572:                        pic[c].ocw3 = data;
                   17573:                } else {
                   17574:                        // ocw2
                   17575:                        int level = 0;
                   17576:                        if(data & 0x40) {
                   17577:                                level = data & 7;
                   17578:                        } else {
                   17579:                                if(!pic[c].isr) {
                   17580:                                        return;
                   17581:                                }
                   17582:                                level = pic[c].prio;
                   17583:                                while(!(pic[c].isr & (1 << level))) {
                   17584:                                        level = (level + 1) & 7;
                   17585:                                }
                   17586:                        }
                   17587:                        if(data & 0x80) {
                   17588:                                pic[c].prio = (level + 1) & 7;
                   17589:                        }
                   17590:                        if(data & 0x20) {
                   17591:                                pic[c].isr &= ~(1 << level);
                   17592:                        }
                   17593:                }
                   17594:        }
                   17595:        pic_update();
                   17596: }
                   17597: 
                   17598: UINT8 pic_read(int c, UINT32 addr)
                   17599: {
                   17600:        if(addr & 1) {
                   17601:                return(pic[c].imr);
                   17602:        } else {
                   17603:                // polling mode is not supported...
                   17604:                //if(pic[c].ocw3 & 4) {
                   17605:                //      return ???;
                   17606:                //}
                   17607:                if(pic[c].ocw3 & 1) {
                   17608:                        return(pic[c].isr);
                   17609:                } else {
                   17610:                        return(pic[c].irr);
                   17611:                }
                   17612:        }
                   17613: }
                   17614: 
                   17615: void pic_req(int c, int level, int signal)
                   17616: {
                   17617:        if(signal) {
                   17618:                pic[c].irr |= (1 << level);
                   17619:        } else {
                   17620:                pic[c].irr &= ~(1 << level);
                   17621:        }
                   17622:        pic_update();
                   17623: }
                   17624: 
                   17625: int pic_ack()
                   17626: {
                   17627:        // ack (INTA=L)
                   17628:        pic[pic_req_chip].isr |= pic_req_bit;
                   17629:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   17630:        if(pic_req_chip > 0) {
                   17631:                // update isr and irr of master
                   17632:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   17633:                pic[pic_req_chip - 1].isr |= slave;
                   17634:                pic[pic_req_chip - 1].irr &= ~slave;
                   17635:        }
                   17636:        //if(pic[pic_req_chip].icw4 & 1) {
                   17637:                // 8086 mode
                   17638:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   17639:        //} else {
                   17640:        //      // 8080 mode
                   17641:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   17642:        //      if(pic[pic_req_chip].icw1 & 4) {
                   17643:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   17644:        //      } else {
                   17645:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   17646:        //      }
                   17647:        //      vector = 0xcd | (addr << 8);
                   17648:        //}
                   17649:        if(pic[pic_req_chip].icw4 & 2) {
                   17650:                // auto eoi
                   17651:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   17652:        }
                   17653:        return(vector);
                   17654: }
                   17655: 
                   17656: void pic_update()
                   17657: {
                   17658:        for(int c = 0; c < 2; c++) {
                   17659:                UINT8 irr = pic[c].irr;
                   17660:                if(c + 1 < 2) {
                   17661:                        // this is master
                   17662:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   17663:                                // request from slave
                   17664:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   17665:                        }
                   17666:                }
                   17667:                irr &= (~pic[c].imr);
                   17668:                if(!irr) {
                   17669:                        break;
                   17670:                }
                   17671:                if(!(pic[c].ocw3 & 0x20)) {
                   17672:                        irr |= pic[c].isr;
                   17673:                }
                   17674:                int level = pic[c].prio;
                   17675:                UINT8 bit = 1 << level;
                   17676:                while(!(irr & bit)) {
                   17677:                        level = (level + 1) & 7;
                   17678:                        bit = 1 << level;
                   17679:                }
                   17680:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   17681:                        // check slave
                   17682:                        continue;
                   17683:                }
                   17684:                if(pic[c].isr & bit) {
                   17685:                        break;
                   17686:                }
                   17687:                // interrupt request
                   17688:                pic_req_chip = c;
                   17689:                pic_req_level = level;
                   17690:                pic_req_bit = bit;
1.1.1.3   root     17691:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     17692:                return;
                   17693:        }
1.1.1.3   root     17694:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     17695: }
1.1       root     17696: 
1.1.1.25  root     17697: // pio
                   17698: 
                   17699: void pio_init()
                   17700: {
1.1.1.38  root     17701: //     bool conv_mode = (GetConsoleCP() == 932);
                   17702:        
1.1.1.26  root     17703:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     17704:        
1.1.1.25  root     17705:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     17706:                pio[c].stat = 0xdf;
1.1.1.25  root     17707:                pio[c].ctrl = 0x0c;
1.1.1.38  root     17708: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     17709:        }
                   17710: }
                   17711: 
1.1.1.37  root     17712: void pio_finish()
                   17713: {
                   17714:        pio_release();
                   17715: }
                   17716: 
                   17717: void pio_release()
                   17718: {
                   17719:        for(int c = 0; c < 2; c++) {
                   17720:                if(pio[c].fp != NULL) {
1.1.1.38  root     17721:                        if(pio[c].jis_mode) {
                   17722:                                fputc(0x1c, pio[c].fp);
                   17723:                                fputc(0x2e, pio[c].fp);
                   17724:                        }
1.1.1.37  root     17725:                        fclose(pio[c].fp);
                   17726:                        pio[c].fp = NULL;
                   17727:                }
                   17728:        }
                   17729: }
                   17730: 
1.1.1.25  root     17731: void pio_write(int c, UINT32 addr, UINT8 data)
                   17732: {
                   17733:        switch(addr & 3) {
                   17734:        case 0:
                   17735:                pio[c].data = data;
                   17736:                break;
                   17737:        case 2:
1.1.1.37  root     17738:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   17739:                        // strobe H -> L
                   17740:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   17741:                                // auto feed
                   17742:                                printer_out(c, 0x0d);
                   17743:                                printer_out(c, 0x0a);
                   17744:                        } else {
                   17745:                                printer_out(c, pio[c].data);
                   17746:                        }
                   17747:                        pio[c].stat &= ~0x40; // set ack
                   17748:                }
1.1.1.25  root     17749:                pio[c].ctrl = data;
                   17750:                break;
                   17751:        }
                   17752: }
                   17753: 
                   17754: UINT8 pio_read(int c, UINT32 addr)
                   17755: {
                   17756:        switch(addr & 3) {
                   17757:        case 0:
1.1.1.37  root     17758:                if(pio[c].ctrl & 0x20) {
                   17759:                        // input mode
                   17760:                        return(0xff);
                   17761:                }
1.1.1.25  root     17762:                return(pio[c].data);
                   17763:        case 1:
1.1.1.37  root     17764:                {
                   17765:                        UINT8 stat = pio[c].stat;
                   17766:                        pio[c].stat |= 0x40; // clear ack
                   17767:                        return(stat);
                   17768:                }
1.1.1.25  root     17769:        case 2:
                   17770:                return(pio[c].ctrl);
                   17771:        }
                   17772:        return(0xff);
                   17773: }
                   17774: 
1.1.1.37  root     17775: void printer_out(int c, UINT8 data)
                   17776: {
                   17777:        SYSTEMTIME time;
1.1.1.38  root     17778:        bool jis_mode = false;
1.1.1.37  root     17779:        
                   17780:        GetLocalTime(&time);
                   17781:        
                   17782:        if(pio[c].fp != NULL) {
                   17783:                // if at least 1000ms passed from last written, close the current file
                   17784:                FILETIME ftime1;
                   17785:                FILETIME ftime2;
                   17786:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   17787:                SystemTimeToFileTime(&time, &ftime2);
                   17788:                INT64 *time1 = (INT64 *)&ftime1;
                   17789:                INT64 *time2 = (INT64 *)&ftime2;
                   17790:                INT64 msec = (*time2 - *time1) / 10000;
                   17791:                
                   17792:                if(msec >= 1000) {
1.1.1.38  root     17793:                        if(pio[c].jis_mode) {
                   17794:                                fputc(0x1c, pio[c].fp);
                   17795:                                fputc(0x2e, pio[c].fp);
                   17796:                                jis_mode = true;
                   17797:                        }
1.1.1.37  root     17798:                        fclose(pio[c].fp);
                   17799:                        pio[c].fp = NULL;
                   17800:                }
                   17801:        }
                   17802:        if(pio[c].fp == NULL) {
                   17803:                // create a new file in the temp folder
                   17804:                char file_name[MAX_PATH];
                   17805:                
                   17806:                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);
                   17807:                if(GetTempPath(MAX_PATH, pio[c].path)) {
                   17808:                        strcat(pio[c].path, file_name);
                   17809:                } else {
                   17810:                        strcpy(pio[c].path, file_name);
                   17811:                }
1.1.1.38  root     17812:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     17813:        }
                   17814:        if(pio[c].fp != NULL) {
1.1.1.38  root     17815:                if(jis_mode) {
                   17816:                        fputc(0x1c, pio[c].fp);
                   17817:                        fputc(0x26, pio[c].fp);
                   17818:                }
1.1.1.37  root     17819:                fputc(data, pio[c].fp);
1.1.1.38  root     17820:                
                   17821:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   17822:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   17823:                        UINT8 buffer[4];
                   17824:                        fseek(pio[c].fp, 0, SEEK_SET);
                   17825:                        fread(buffer, 4, 1, pio[c].fp);
                   17826:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   17827:                                fclose(pio[c].fp);
                   17828:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   17829:                        }
                   17830:                }
1.1.1.37  root     17831:                pio[c].time = time;
                   17832:        }
                   17833: }
                   17834: 
1.1       root     17835: // pit
                   17836: 
1.1.1.22  root     17837: #define PIT_FREQ 1193182ULL
1.1       root     17838: #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)
                   17839: 
                   17840: void pit_init()
                   17841: {
1.1.1.8   root     17842:        memset(pit, 0, sizeof(pit));
1.1       root     17843:        for(int ch = 0; ch < 3; ch++) {
                   17844:                pit[ch].count = 0x10000;
                   17845:                pit[ch].ctrl_reg = 0x34;
                   17846:                pit[ch].mode = 3;
                   17847:        }
                   17848:        
                   17849:        // from bochs bios
                   17850:        pit_write(3, 0x34);
                   17851:        pit_write(0, 0x00);
                   17852:        pit_write(0, 0x00);
                   17853: }
                   17854: 
                   17855: void pit_write(int ch, UINT8 val)
                   17856: {
1.1.1.8   root     17857: #ifndef PIT_ALWAYS_RUNNING
1.1       root     17858:        if(!pit_active) {
                   17859:                pit_active = 1;
                   17860:                pit_init();
                   17861:        }
1.1.1.8   root     17862: #endif
1.1       root     17863:        switch(ch) {
                   17864:        case 0:
                   17865:        case 1:
                   17866:        case 2:
                   17867:                // write count register
                   17868:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   17869:                        if(pit[ch].ctrl_reg & 0x10) {
                   17870:                                pit[ch].low_write = 1;
                   17871:                        }
                   17872:                        if(pit[ch].ctrl_reg & 0x20) {
                   17873:                                pit[ch].high_write = 1;
                   17874:                        }
                   17875:                }
                   17876:                if(pit[ch].low_write) {
                   17877:                        pit[ch].count_reg = val;
                   17878:                        pit[ch].low_write = 0;
                   17879:                } else if(pit[ch].high_write) {
                   17880:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   17881:                                pit[ch].count_reg = val << 8;
                   17882:                        } else {
                   17883:                                pit[ch].count_reg |= val << 8;
                   17884:                        }
                   17885:                        pit[ch].high_write = 0;
                   17886:                }
                   17887:                // start count
1.1.1.8   root     17888:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   17889:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   17890:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   17891:                                pit[ch].prev_time = timeGetTime();
                   17892:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     17893:                        }
                   17894:                }
                   17895:                break;
                   17896:        case 3: // ctrl reg
                   17897:                if((val & 0xc0) == 0xc0) {
                   17898:                        // i8254 read-back command
                   17899:                        for(ch = 0; ch < 3; ch++) {
                   17900:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   17901:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   17902:                                        pit[ch].status_latched = 1;
                   17903:                                }
                   17904:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   17905:                                        pit_latch_count(ch);
                   17906:                                }
                   17907:                        }
                   17908:                        break;
                   17909:                }
                   17910:                ch = (val >> 6) & 3;
                   17911:                if(val & 0x30) {
1.1.1.35  root     17912:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     17913:                        pit[ch].mode = modes[(val >> 1) & 7];
                   17914:                        pit[ch].count_latched = 0;
                   17915:                        pit[ch].low_read = pit[ch].high_read = 0;
                   17916:                        pit[ch].low_write = pit[ch].high_write = 0;
                   17917:                        pit[ch].ctrl_reg = val;
                   17918:                        // stop count
1.1.1.8   root     17919:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     17920:                        pit[ch].count_reg = 0;
                   17921:                } else if(!pit[ch].count_latched) {
                   17922:                        pit_latch_count(ch);
                   17923:                }
                   17924:                break;
                   17925:        }
                   17926: }
                   17927: 
                   17928: UINT8 pit_read(int ch)
                   17929: {
1.1.1.8   root     17930: #ifndef PIT_ALWAYS_RUNNING
1.1       root     17931:        if(!pit_active) {
                   17932:                pit_active = 1;
                   17933:                pit_init();
                   17934:        }
1.1.1.8   root     17935: #endif
1.1       root     17936:        switch(ch) {
                   17937:        case 0:
                   17938:        case 1:
                   17939:        case 2:
                   17940:                if(pit[ch].status_latched) {
                   17941:                        pit[ch].status_latched = 0;
                   17942:                        return(pit[ch].status);
                   17943:                }
                   17944:                // if not latched, through current count
                   17945:                if(!pit[ch].count_latched) {
                   17946:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   17947:                                pit_latch_count(ch);
                   17948:                        }
                   17949:                }
                   17950:                // return latched count
                   17951:                if(pit[ch].low_read) {
                   17952:                        pit[ch].low_read = 0;
                   17953:                        if(!pit[ch].high_read) {
                   17954:                                pit[ch].count_latched = 0;
                   17955:                        }
                   17956:                        return(pit[ch].latch & 0xff);
                   17957:                } else if(pit[ch].high_read) {
                   17958:                        pit[ch].high_read = 0;
                   17959:                        pit[ch].count_latched = 0;
                   17960:                        return((pit[ch].latch >> 8) & 0xff);
                   17961:                }
                   17962:        }
                   17963:        return(0xff);
                   17964: }
                   17965: 
1.1.1.8   root     17966: int pit_run(int ch, UINT32 cur_time)
1.1       root     17967: {
1.1.1.8   root     17968:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     17969:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     17970:                pit[ch].prev_time = pit[ch].expired_time;
                   17971:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   17972:                if(cur_time >= pit[ch].expired_time) {
                   17973:                        pit[ch].prev_time = cur_time;
                   17974:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     17975:                }
1.1.1.8   root     17976:                return(1);
1.1       root     17977:        }
1.1.1.8   root     17978:        return(0);
1.1       root     17979: }
                   17980: 
                   17981: void pit_latch_count(int ch)
                   17982: {
1.1.1.8   root     17983:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     17984:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     17985:                pit_run(ch, cur_time);
                   17986:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     17987:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   17988:                
                   17989:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   17990:                        // decrement counter in 1msec period
                   17991:                        if(pit[ch].next_latch == 0) {
                   17992:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   17993:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   17994:                        }
                   17995:                        if(pit[ch].latch > pit[ch].next_latch) {
                   17996:                                pit[ch].latch--;
                   17997:                        }
                   17998:                } else {
                   17999:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   18000:                        pit[ch].next_latch = 0;
                   18001:                }
1.1.1.8   root     18002:        } else {
                   18003:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     18004:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     18005:        }
                   18006:        pit[ch].count_latched = 1;
                   18007:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   18008:                // lower byte
                   18009:                pit[ch].low_read = 1;
                   18010:                pit[ch].high_read = 0;
                   18011:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   18012:                // upper byte
                   18013:                pit[ch].low_read = 0;
                   18014:                pit[ch].high_read = 1;
                   18015:        } else {
                   18016:                // lower -> upper
1.1.1.14  root     18017:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     18018:        }
                   18019: }
                   18020: 
1.1.1.8   root     18021: int pit_get_expired_time(int ch)
1.1       root     18022: {
1.1.1.22  root     18023:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   18024:        UINT64 val = pit[ch].accum >> 10;
                   18025:        pit[ch].accum -= val << 10;
                   18026:        return((val != 0) ? val : 1);
1.1.1.8   root     18027: }
                   18028: 
1.1.1.25  root     18029: // sio
                   18030: 
                   18031: void sio_init()
                   18032: {
1.1.1.26  root     18033:        memset(sio, 0, sizeof(sio));
                   18034:        memset(sio_mt, 0, sizeof(sio_mt));
                   18035:        
1.1.1.29  root     18036:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     18037:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   18038:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   18039:                
                   18040:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   18041:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     18042:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   18043:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     18044:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   18045:                sio[c].irq_identify = 0x01;     // no pending irq
                   18046:                
                   18047:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   18048:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   18049:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   18050:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   18051:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   18052:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   18053:                
1.1.1.26  root     18054:                if(sio_port_number[c] != 0) {
1.1.1.25  root     18055:                        sio[c].channel = c;
                   18056:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   18057:                }
                   18058:        }
                   18059: }
                   18060: 
                   18061: void sio_finish()
                   18062: {
1.1.1.29  root     18063:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     18064:                if(sio_mt[c].hThread != NULL) {
                   18065:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   18066:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     18067:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     18068:                }
                   18069:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   18070:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   18071:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   18072:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   18073:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   18074:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     18075:        }
                   18076:        sio_release();
                   18077: }
                   18078: 
                   18079: void sio_release()
                   18080: {
1.1.1.29  root     18081:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     18082:                // sio_thread() may access the resources :-(
1.1.1.32  root     18083:                bool running = (sio_mt[c].hThread != NULL);
                   18084:                
                   18085:                if(running) {
                   18086:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   18087:                }
                   18088:                if(sio[c].send_buffer != NULL) {
                   18089:                        sio[c].send_buffer->release();
                   18090:                        delete sio[c].send_buffer;
                   18091:                        sio[c].send_buffer = NULL;
                   18092:                }
                   18093:                if(running) {
                   18094:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   18095:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   18096:                }
                   18097:                if(sio[c].recv_buffer != NULL) {
                   18098:                        sio[c].recv_buffer->release();
                   18099:                        delete sio[c].recv_buffer;
                   18100:                        sio[c].recv_buffer = NULL;
                   18101:                }
                   18102:                if(running) {
                   18103:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     18104:                }
1.1.1.25  root     18105:        }
                   18106: }
                   18107: 
                   18108: void sio_write(int c, UINT32 addr, UINT8 data)
                   18109: {
                   18110:        switch(addr & 7) {
                   18111:        case 0:
                   18112:                if(sio[c].selector & 0x80) {
                   18113:                        if(sio[c].divisor.b.l != data) {
                   18114:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   18115:                                sio[c].divisor.b.l = data;
                   18116:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   18117:                        }
                   18118:                } else {
                   18119:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     18120:                        if(sio[c].send_buffer != NULL) {
                   18121:                                sio[c].send_buffer->write(data);
                   18122:                        }
1.1.1.25  root     18123:                        // transmitter holding/shift registers are not empty
                   18124:                        sio[c].line_stat_buf &= ~0x60;
                   18125:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   18126:                        
                   18127:                        if(sio[c].irq_enable & 0x02) {
                   18128:                                sio_update_irq(c);
                   18129:                        }
                   18130:                }
                   18131:                break;
                   18132:        case 1:
                   18133:                if(sio[c].selector & 0x80) {
                   18134:                        if(sio[c].divisor.b.h != data) {
                   18135:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   18136:                                sio[c].divisor.b.h = data;
                   18137:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   18138:                        }
                   18139:                } else {
                   18140:                        if(sio[c].irq_enable != data) {
                   18141:                                sio[c].irq_enable = data;
                   18142:                                sio_update_irq(c);
                   18143:                        }
                   18144:                }
                   18145:                break;
                   18146:        case 3:
                   18147:                {
                   18148:                        UINT8 line_ctrl = data & 0x3f;
                   18149:                        bool set_brk = ((data & 0x40) != 0);
                   18150:                        
                   18151:                        if(sio[c].line_ctrl != line_ctrl) {
                   18152:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   18153:                                sio[c].line_ctrl = line_ctrl;
                   18154:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   18155:                        }
                   18156:                        if(sio[c].set_brk != set_brk) {
                   18157:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   18158:                                sio[c].set_brk = set_brk;
                   18159:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   18160:                        }
                   18161:                }
                   18162:                sio[c].selector = data;
                   18163:                break;
                   18164:        case 4:
                   18165:                {
                   18166:                        bool set_dtr = ((data & 0x01) != 0);
                   18167:                        bool set_rts = ((data & 0x02) != 0);
                   18168:                        
                   18169:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     18170: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     18171:                                sio[c].set_dtr = set_dtr;
                   18172:                                sio[c].set_rts = set_rts;
1.1.1.26  root     18173: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   18174:                                
                   18175:                                bool state_changed = false;
                   18176:                                
                   18177:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   18178:                                if(set_dtr) {
                   18179:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   18180:                                } else {
                   18181:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   18182:                                }
                   18183:                                if(set_rts) {
                   18184:                                        sio[c].modem_stat |= 0x10;      // cts on
                   18185:                                } else {
                   18186:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   18187:                                }
                   18188:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   18189:                                        if(!(sio[c].modem_stat & 0x02)) {
                   18190:                                                if(sio[c].irq_enable & 0x08) {
                   18191:                                                        state_changed = true;
                   18192:                                                }
                   18193:                                                sio[c].modem_stat |= 0x02;
                   18194:                                        }
                   18195:                                }
                   18196:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   18197:                                        if(!(sio[c].modem_stat & 0x01)) {
                   18198:                                                if(sio[c].irq_enable & 0x08) {
                   18199:                                                        state_changed = true;
                   18200:                                                }
                   18201:                                                sio[c].modem_stat |= 0x01;
                   18202:                                        }
                   18203:                                }
                   18204:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   18205:                                
                   18206:                                if(state_changed) {
                   18207:                                        sio_update_irq(c);
                   18208:                                }
1.1.1.25  root     18209:                        }
                   18210:                }
                   18211:                sio[c].modem_ctrl = data;
                   18212:                break;
                   18213:        case 7:
                   18214:                sio[c].scratch = data;
                   18215:                break;
                   18216:        }
                   18217: }
                   18218: 
                   18219: UINT8 sio_read(int c, UINT32 addr)
                   18220: {
                   18221:        switch(addr & 7) {
                   18222:        case 0:
                   18223:                if(sio[c].selector & 0x80) {
                   18224:                        return(sio[c].divisor.b.l);
                   18225:                } else {
                   18226:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     18227:                        UINT8 data = 0;
                   18228:                        if(sio[c].recv_buffer != NULL) {
                   18229:                                data = sio[c].recv_buffer->read();
                   18230:                        }
1.1.1.25  root     18231:                        // data is not ready
                   18232:                        sio[c].line_stat_buf &= ~0x01;
                   18233:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   18234:                        
                   18235:                        if(sio[c].irq_enable & 0x01) {
                   18236:                                sio_update_irq(c);
                   18237:                        }
                   18238:                        return(data);
                   18239:                }
                   18240:        case 1:
                   18241:                if(sio[c].selector & 0x80) {
                   18242:                        return(sio[c].divisor.b.h);
                   18243:                } else {
                   18244:                        return(sio[c].irq_enable);
                   18245:                }
                   18246:        case 2:
                   18247:                return(sio[c].irq_identify);
                   18248:        case 3:
                   18249:                return(sio[c].selector);
                   18250:        case 4:
                   18251:                return(sio[c].modem_ctrl);
                   18252:        case 5:
                   18253:                {
                   18254:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   18255:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   18256:                        sio[c].line_stat_err = 0x00;
                   18257:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   18258:                        
                   18259:                        bool state_changed = false;
                   18260:                        
                   18261:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   18262:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     18263:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     18264:                                        // transmitter holding register will be empty first
                   18265:                                        if(sio[c].irq_enable & 0x02) {
                   18266:                                                state_changed = true;
                   18267:                                        }
                   18268:                                        sio[c].line_stat_buf |= 0x20;
                   18269:                                }
                   18270:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   18271:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   18272:                                // transmitter shift register will be empty later
                   18273:                                sio[c].line_stat_buf |= 0x40;
                   18274:                        }
                   18275:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   18276:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     18277:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     18278:                                        // data is ready
                   18279:                                        if(sio[c].irq_enable & 0x01) {
                   18280:                                                state_changed = true;
                   18281:                                        }
                   18282:                                        sio[c].line_stat_buf |= 0x01;
                   18283:                                }
                   18284:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   18285:                        }
                   18286:                        if(state_changed) {
                   18287:                                sio_update_irq(c);
                   18288:                        }
                   18289:                        return(val);
                   18290:                }
                   18291:        case 6:
                   18292:                {
                   18293:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   18294:                        UINT8 val = sio[c].modem_stat;
                   18295:                        sio[c].modem_stat &= 0xf0;
                   18296:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   18297:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   18298:                        
                   18299:                        if(sio[c].modem_ctrl & 0x10) {
                   18300:                                // loop-back
                   18301:                                val &= 0x0f;
                   18302:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   18303:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   18304:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   18305:                        }
                   18306:                        return(val);
                   18307:                }
                   18308:        case 7:
                   18309:                return(sio[c].scratch);
                   18310:        }
                   18311:        return(0xff);
                   18312: }
                   18313: 
                   18314: void sio_update(int c)
                   18315: {
                   18316:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   18317:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     18318:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     18319:                        // transmitter holding/shift registers will be empty
                   18320:                        sio[c].line_stat_buf |= 0x60;
                   18321:                }
                   18322:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   18323:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   18324:                // transmitter shift register will be empty
                   18325:                sio[c].line_stat_buf |= 0x40;
                   18326:        }
                   18327:        if(!(sio[c].line_stat_buf & 0x01)) {
                   18328:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     18329:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     18330:                        // data is ready
                   18331:                        sio[c].line_stat_buf |= 0x01;
                   18332:                }
                   18333:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   18334:        }
                   18335:        sio_update_irq(c);
                   18336: }
                   18337: 
                   18338: void sio_update_irq(int c)
                   18339: {
                   18340:        int level = -1;
                   18341:        
                   18342:        if(sio[c].irq_enable & 0x08) {
                   18343:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   18344:                if((sio[c].modem_stat & 0x0f) != 0) {
                   18345:                        level = 0;
                   18346:                }
                   18347:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   18348:        }
                   18349:        if(sio[c].irq_enable & 0x02) {
                   18350:                if(sio[c].line_stat_buf & 0x20) {
                   18351:                        level = 1;
                   18352:                }
                   18353:        }
                   18354:        if(sio[c].irq_enable & 0x01) {
                   18355:                if(sio[c].line_stat_buf & 0x01) {
                   18356:                        level = 2;
                   18357:                }
                   18358:        }
                   18359:        if(sio[c].irq_enable & 0x04) {
                   18360:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   18361:                if(sio[c].line_stat_err != 0) {
                   18362:                        level = 3;
                   18363:                }
                   18364:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   18365:        }
1.1.1.29  root     18366:        
                   18367:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     18368:        if(level != -1) {
                   18369:                sio[c].irq_identify = level << 1;
1.1.1.29  root     18370:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     18371:        } else {
                   18372:                sio[c].irq_identify = 1;
1.1.1.29  root     18373:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     18374:        }
                   18375: }
                   18376: 
                   18377: DWORD WINAPI sio_thread(void *lpx)
                   18378: {
                   18379:        volatile sio_t *p = (sio_t *)lpx;
                   18380:        sio_mt_t *q = &sio_mt[p->channel];
                   18381:        
                   18382:        char name[] = "COM1";
1.1.1.26  root     18383:        name[3] = '0' + sio_port_number[p->channel];
                   18384:        HANDLE hComm = NULL;
                   18385:        COMMPROP commProp;
                   18386:        DCB dcb;
                   18387:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   18388:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   18389:        
                   18390:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   18391:                if(GetCommProperties(hComm, &commProp)) {
                   18392:                        dwSettableBaud = commProp.dwSettableBaud;
                   18393:                }
1.1.1.25  root     18394:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     18395: //             EscapeCommFunction(hComm, SETRTS);
                   18396: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     18397:                
                   18398:                while(!m_halted) {
                   18399:                        // setup comm port
                   18400:                        bool comm_state_changed = false;
                   18401:                        
                   18402:                        EnterCriticalSection(&q->csLineCtrl);
                   18403:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   18404:                                p->prev_divisor = p->divisor.w;
                   18405:                                p->prev_line_ctrl = p->line_ctrl;
                   18406:                                comm_state_changed = true;
                   18407:                        }
                   18408:                        LeaveCriticalSection(&q->csLineCtrl);
                   18409:                        
                   18410:                        if(comm_state_changed) {
1.1.1.26  root     18411:                                if(GetCommState(hComm, &dcb)) {
                   18412: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   18413:                                        DWORD baud = 115200 / p->prev_divisor;
                   18414:                                        dcb.BaudRate = 9600; // default
                   18415:                                        
                   18416:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   18417:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   18418:                                        // 134.5bps is not supported ???
                   18419: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   18420:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   18421:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   18422:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   18423:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   18424:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   18425:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   18426:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   18427:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   18428:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   18429: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   18430: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   18431: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   18432:                                        
                   18433:                                        switch(p->prev_line_ctrl & 0x03) {
                   18434:                                        case 0x00: dcb.ByteSize = 5; break;
                   18435:                                        case 0x01: dcb.ByteSize = 6; break;
                   18436:                                        case 0x02: dcb.ByteSize = 7; break;
                   18437:                                        case 0x03: dcb.ByteSize = 8; break;
                   18438:                                        }
                   18439:                                        switch(p->prev_line_ctrl & 0x04) {
                   18440:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   18441:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   18442:                                        }
                   18443:                                        switch(p->prev_line_ctrl & 0x38) {
                   18444:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   18445:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   18446:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   18447:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   18448:                                        default:   dcb.Parity = NOPARITY;    break;
                   18449:                                        }
                   18450:                                        dcb.fBinary = TRUE;
                   18451:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   18452:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   18453:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   18454:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   18455:                                        dcb.fTXContinueOnXoff = TRUE;
                   18456:                                        dcb.fOutX = dcb.fInX = FALSE;
                   18457:                                        dcb.fErrorChar = FALSE;
                   18458:                                        dcb.fNull = FALSE;
                   18459:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   18460:                                        dcb.fAbortOnError = FALSE;
                   18461:                                        
                   18462:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     18463:                                }
                   18464:                                
                   18465:                                // check again to apply all comm state changes
                   18466:                                Sleep(10);
                   18467:                                continue;
                   18468:                        }
                   18469:                        
                   18470:                        // set comm pins
                   18471:                        bool change_brk = false;
1.1.1.26  root     18472: //                     bool change_rts = false;
                   18473: //                     bool change_dtr = false;
1.1.1.25  root     18474:                        
                   18475:                        EnterCriticalSection(&q->csModemCtrl);
                   18476:                        if(p->prev_set_brk != p->set_brk) {
                   18477:                                p->prev_set_brk = p->set_brk;
                   18478:                                change_brk = true;
                   18479:                        }
1.1.1.26  root     18480: //                     if(p->prev_set_rts != p->set_rts) {
                   18481: //                             p->prev_set_rts = p->set_rts;
                   18482: //                             change_rts = true;
                   18483: //                     }
                   18484: //                     if(p->prev_set_dtr != p->set_dtr) {
                   18485: //                             p->prev_set_dtr = p->set_dtr;
                   18486: //                             change_dtr = true;
                   18487: //                     }
1.1.1.25  root     18488:                        LeaveCriticalSection(&q->csModemCtrl);
                   18489:                        
                   18490:                        if(change_brk) {
1.1.1.26  root     18491:                                static UINT32 clear_time = 0;
                   18492:                                if(p->prev_set_brk) {
                   18493:                                        EscapeCommFunction(hComm, SETBREAK);
                   18494:                                        clear_time = timeGetTime() + 200;
                   18495:                                } else {
                   18496:                                        // keep break for at least 200msec
                   18497:                                        UINT32 cur_time = timeGetTime();
                   18498:                                        if(clear_time > cur_time) {
                   18499:                                                Sleep(clear_time - cur_time);
                   18500:                                        }
                   18501:                                        EscapeCommFunction(hComm, CLRBREAK);
                   18502:                                }
1.1.1.25  root     18503:                        }
1.1.1.26  root     18504: //                     if(change_rts) {
                   18505: //                             if(p->prev_set_rts) {
                   18506: //                                     EscapeCommFunction(hComm, SETRTS);
                   18507: //                             } else {
                   18508: //                                     EscapeCommFunction(hComm, CLRRTS);
                   18509: //                             }
                   18510: //                     }
                   18511: //                     if(change_dtr) {
                   18512: //                             if(p->prev_set_dtr) {
                   18513: //                                     EscapeCommFunction(hComm, SETDTR);
                   18514: //                             } else {
                   18515: //                                     EscapeCommFunction(hComm, CLRDTR);
                   18516: //                             }
                   18517: //                     }
1.1.1.25  root     18518:                        
                   18519:                        // get comm pins
                   18520:                        DWORD dwModemStat = 0;
                   18521:                        
                   18522:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   18523:                                EnterCriticalSection(&q->csModemStat);
                   18524:                                if(dwModemStat & MS_RLSD_ON) {
                   18525:                                        p->modem_stat |= 0x80;
                   18526:                                } else {
                   18527:                                        p->modem_stat &= ~0x80;
                   18528:                                }
                   18529:                                if(dwModemStat & MS_RING_ON) {
                   18530:                                        p->modem_stat |= 0x40;
                   18531:                                } else {
                   18532:                                        p->modem_stat &= ~0x40;
                   18533:                                }
1.1.1.26  root     18534: //                             if(dwModemStat & MS_DSR_ON) {
                   18535: //                                     p->modem_stat |= 0x20;
                   18536: //                             } else {
                   18537: //                                     p->modem_stat &= ~0x20;
                   18538: //                             }
                   18539: //                             if(dwModemStat & MS_CTS_ON) {
                   18540: //                                     p->modem_stat |= 0x10;
                   18541: //                             } else {
                   18542: //                                     p->modem_stat &= ~0x10;
                   18543: //                             }
1.1.1.25  root     18544:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   18545:                                        p->modem_stat |= 0x08;
                   18546:                                }
                   18547:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   18548:                                        p->modem_stat |= 0x04;
                   18549:                                }
1.1.1.26  root     18550: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   18551: //                                     p->modem_stat |= 0x02;
                   18552: //                             }
                   18553: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   18554: //                                     p->modem_stat |= 0x01;
                   18555: //                             }
1.1.1.25  root     18556:                                LeaveCriticalSection(&q->csModemStat);
                   18557:                        }
                   18558:                        
                   18559:                        // send data
                   18560:                        DWORD dwSend = 0;
                   18561:                        
                   18562:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     18563:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     18564:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   18565:                        }
                   18566:                        LeaveCriticalSection(&q->csSendData);
                   18567:                        
                   18568:                        if(dwSend != 0) {
                   18569:                                DWORD dwWritten = 0;
                   18570:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   18571:                        }
                   18572:                        
                   18573:                        // get line status and recv data
                   18574:                        DWORD dwLineStat = 0;
                   18575:                        COMSTAT comStat;
                   18576:                        
                   18577:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   18578:                                EnterCriticalSection(&q->csLineStat);
                   18579:                                if(dwLineStat & CE_BREAK) {
                   18580:                                        p->line_stat_err |= 0x10;
                   18581:                                }
                   18582:                                if(dwLineStat & CE_FRAME) {
                   18583:                                        p->line_stat_err |= 0x08;
                   18584:                                }
                   18585:                                if(dwLineStat & CE_RXPARITY) {
                   18586:                                        p->line_stat_err |= 0x04;
                   18587:                                }
                   18588:                                if(dwLineStat & CE_OVERRUN) {
                   18589:                                        p->line_stat_err |= 0x02;
                   18590:                                }
                   18591:                                LeaveCriticalSection(&q->csLineStat);
                   18592:                                
                   18593:                                if(comStat.cbInQue != 0) {
                   18594:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     18595:                                        DWORD dwRecv = 0;
                   18596:                                        if(p->recv_buffer != NULL) {
                   18597:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   18598:                                        }
1.1.1.25  root     18599:                                        LeaveCriticalSection(&q->csRecvData);
                   18600:                                        
                   18601:                                        if(dwRecv != 0) {
                   18602:                                                DWORD dwRead = 0;
                   18603:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   18604:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     18605:                                                        if(p->recv_buffer != NULL) {
                   18606:                                                                for(int i = 0; i < dwRead; i++) {
                   18607:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   18608:                                                                }
1.1.1.25  root     18609:                                                        }
                   18610:                                                        LeaveCriticalSection(&q->csRecvData);
                   18611:                                                }
                   18612:                                        }
                   18613:                                }
                   18614:                        }
                   18615:                        Sleep(10);
                   18616:                }
                   18617:                CloseHandle(hComm);
                   18618:        }
                   18619:        return 0;
                   18620: }
                   18621: 
1.1.1.8   root     18622: // cmos
                   18623: 
                   18624: void cmos_init()
                   18625: {
                   18626:        memset(cmos, 0, sizeof(cmos));
                   18627:        cmos_addr = 0;
1.1       root     18628:        
1.1.1.8   root     18629:        // from DOSBox
                   18630:        cmos_write(0x0a, 0x26);
                   18631:        cmos_write(0x0b, 0x02);
                   18632:        cmos_write(0x0d, 0x80);
1.1       root     18633: }
                   18634: 
1.1.1.8   root     18635: void cmos_write(int addr, UINT8 val)
1.1       root     18636: {
1.1.1.8   root     18637:        cmos[addr & 0x7f] = val;
                   18638: }
                   18639: 
                   18640: #define CMOS_GET_TIME() { \
                   18641:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   18642:        if(prev_sec != cur_sec) { \
                   18643:                GetLocalTime(&time); \
                   18644:                prev_sec = cur_sec; \
                   18645:        } \
1.1       root     18646: }
1.1.1.8   root     18647: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     18648: 
1.1.1.8   root     18649: UINT8 cmos_read(int addr)
1.1       root     18650: {
1.1.1.8   root     18651:        static SYSTEMTIME time;
                   18652:        static UINT32 prev_sec = 0;
1.1       root     18653:        
1.1.1.8   root     18654:        switch(addr & 0x7f) {
                   18655:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   18656:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   18657:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   18658:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   18659:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   18660:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   18661:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   18662: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   18663:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   18664:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   18665:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   18666:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   18667:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   18668:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   18669:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   18670:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     18671:        }
1.1.1.8   root     18672:        return(cmos[addr & 0x7f]);
1.1       root     18673: }
                   18674: 
1.1.1.7   root     18675: // kbd (a20)
                   18676: 
                   18677: void kbd_init()
                   18678: {
1.1.1.8   root     18679:        kbd_data = kbd_command = 0;
1.1.1.7   root     18680:        kbd_status = 0x18;
                   18681: }
                   18682: 
                   18683: UINT8 kbd_read_data()
                   18684: {
1.1.1.8   root     18685:        kbd_status &= ~1;
1.1.1.7   root     18686:        return(kbd_data);
                   18687: }
                   18688: 
                   18689: void kbd_write_data(UINT8 val)
                   18690: {
                   18691:        switch(kbd_command) {
                   18692:        case 0xd1:
                   18693:                i386_set_a20_line((val >> 1) & 1);
                   18694:                break;
                   18695:        }
                   18696:        kbd_command = 0;
1.1.1.8   root     18697:        kbd_status &= ~8;
1.1.1.7   root     18698: }
                   18699: 
                   18700: UINT8 kbd_read_status()
                   18701: {
                   18702:        return(kbd_status);
                   18703: }
                   18704: 
                   18705: void kbd_write_command(UINT8 val)
                   18706: {
                   18707:        switch(val) {
                   18708:        case 0xd0:
                   18709:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     18710:                kbd_status |= 1;
1.1.1.7   root     18711:                break;
                   18712:        case 0xdd:
                   18713:                i386_set_a20_line(0);
                   18714:                break;
                   18715:        case 0xdf:
                   18716:                i386_set_a20_line(1);
                   18717:                break;
1.1.1.26  root     18718:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   18719:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     18720:                if(!(val & 1)) {
1.1.1.8   root     18721:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     18722:                                // reset pic
                   18723:                                pic_init();
                   18724:                                pic[0].irr = pic[1].irr = 0x00;
                   18725:                                pic[0].imr = pic[1].imr = 0xff;
                   18726:                        }
                   18727:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     18728:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   18729:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   18730:                        i386_jmp_far(selector, address);
1.1.1.7   root     18731:                }
                   18732:                i386_set_a20_line((val >> 1) & 1);
                   18733:                break;
                   18734:        }
                   18735:        kbd_command = val;
1.1.1.8   root     18736:        kbd_status |= 8;
1.1.1.7   root     18737: }
                   18738: 
1.1.1.9   root     18739: // vga
                   18740: 
                   18741: UINT8 vga_read_status()
                   18742: {
                   18743:        // 60hz
                   18744:        static const int period[3] = {16, 17, 17};
                   18745:        static int index = 0;
                   18746:        UINT32 time = timeGetTime() % period[index];
                   18747:        
                   18748:        index = (index + 1) % 3;
1.1.1.14  root     18749:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     18750: }
                   18751: 
1.1       root     18752: // i/o bus
                   18753: 
1.1.1.29  root     18754: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   18755: //#define SW1US_PATCH
                   18756: 
1.1.1.25  root     18757: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     18758: #ifdef USE_DEBUGGER
1.1.1.25  root     18759: {
1.1.1.33  root     18760:        if(now_debugging) {
                   18761:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   18762:                        if(in_break_point.table[i].status == 1) {
                   18763:                                if(addr == in_break_point.table[i].addr) {
                   18764:                                        in_break_point.hit = i + 1;
                   18765:                                        now_suspended = true;
                   18766:                                        break;
                   18767:                                }
                   18768:                        }
                   18769:                }
1.1.1.25  root     18770:        }
1.1.1.33  root     18771:        return(debugger_read_io_byte(addr));
1.1.1.25  root     18772: }
1.1.1.33  root     18773: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     18774: #endif
1.1       root     18775: {
1.1.1.33  root     18776:        UINT8 val = 0xff;
                   18777:        
1.1       root     18778:        switch(addr) {
1.1.1.29  root     18779: #ifdef SW1US_PATCH
                   18780:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   18781:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     18782:                val = sio_read(0, addr - 1);
                   18783:                break;
1.1.1.29  root     18784: #else
1.1.1.25  root     18785:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   18786:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     18787:                val = dma_read(0, addr);
                   18788:                break;
1.1.1.29  root     18789: #endif
1.1.1.25  root     18790:        case 0x20: case 0x21:
1.1.1.33  root     18791:                val = pic_read(0, addr);
                   18792:                break;
1.1.1.25  root     18793:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     18794:                val = pit_read(addr & 0x03);
                   18795:                break;
1.1.1.7   root     18796:        case 0x60:
1.1.1.33  root     18797:                val = kbd_read_data();
                   18798:                break;
1.1.1.9   root     18799:        case 0x61:
1.1.1.33  root     18800:                val = system_port;
                   18801:                break;
1.1.1.7   root     18802:        case 0x64:
1.1.1.33  root     18803:                val = kbd_read_status();
                   18804:                break;
1.1       root     18805:        case 0x71:
1.1.1.33  root     18806:                val = cmos_read(cmos_addr);
                   18807:                break;
1.1.1.25  root     18808:        case 0x81:
1.1.1.33  root     18809:                val = dma_page_read(0, 2);
                   18810:                break;
1.1.1.25  root     18811:        case 0x82:
1.1.1.33  root     18812:                val = dma_page_read(0, 3);
                   18813:                break;
1.1.1.25  root     18814:        case 0x83:
1.1.1.33  root     18815:                val = dma_page_read(0, 1);
                   18816:                break;
1.1.1.25  root     18817:        case 0x87:
1.1.1.33  root     18818:                val = dma_page_read(0, 0);
                   18819:                break;
1.1.1.25  root     18820:        case 0x89:
1.1.1.33  root     18821:                val = dma_page_read(1, 2);
                   18822:                break;
1.1.1.25  root     18823:        case 0x8a:
1.1.1.33  root     18824:                val = dma_page_read(1, 3);
                   18825:                break;
1.1.1.25  root     18826:        case 0x8b:
1.1.1.33  root     18827:                val = dma_page_read(1, 1);
                   18828:                break;
1.1.1.25  root     18829:        case 0x8f:
1.1.1.33  root     18830:                val = dma_page_read(1, 0);
                   18831:                break;
1.1       root     18832:        case 0x92:
1.1.1.33  root     18833:                val = (m_a20_mask >> 19) & 2;
                   18834:                break;
1.1.1.25  root     18835:        case 0xa0: case 0xa1:
1.1.1.33  root     18836:                val = pic_read(1, addr);
                   18837:                break;
1.1.1.25  root     18838:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   18839:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     18840:                val = dma_read(1, (addr - 0xc0) >> 1);
                   18841:                break;
1.1.1.37  root     18842:        case 0x278: case 0x279: case 0x27a:
                   18843:                val = pio_read(1, addr);
                   18844:                break;
1.1.1.29  root     18845:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     18846:                val = sio_read(3, addr);
                   18847:                break;
1.1.1.25  root     18848:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     18849:                val = sio_read(1, addr);
                   18850:                break;
1.1.1.25  root     18851:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     18852:                val = pio_read(0, addr);
                   18853:                break;
1.1.1.25  root     18854:        case 0x3ba: case 0x3da:
1.1.1.33  root     18855:                val = vga_read_status();
                   18856:                break;
1.1.1.37  root     18857:        case 0x3bc: case 0x3bd: case 0x3be:
                   18858:                val = pio_read(2, addr);
                   18859:                break;
1.1.1.29  root     18860:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     18861:                val = sio_read(2, addr);
                   18862:                break;
1.1.1.25  root     18863:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     18864:                val = sio_read(0, addr);
                   18865:                break;
1.1       root     18866:        default:
1.1.1.33  root     18867: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     18868:                break;
                   18869:        }
1.1.1.33  root     18870: #ifdef ENABLE_DEBUG_IOPORT
                   18871:        if(fp_debug_log != NULL) {
                   18872:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   18873:        }
                   18874: #endif
                   18875:        return(val);
1.1       root     18876: }
                   18877: 
                   18878: UINT16 read_io_word(offs_t addr)
                   18879: {
                   18880:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   18881: }
                   18882: 
1.1.1.33  root     18883: #ifdef USE_DEBUGGER
                   18884: UINT16 debugger_read_io_word(offs_t addr)
                   18885: {
                   18886:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   18887: }
                   18888: #endif
                   18889: 
1.1       root     18890: UINT32 read_io_dword(offs_t addr)
                   18891: {
                   18892:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   18893: }
                   18894: 
1.1.1.33  root     18895: #ifdef USE_DEBUGGER
                   18896: UINT32 debugger_read_io_dword(offs_t addr)
                   18897: {
                   18898:        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));
                   18899: }
                   18900: #endif
                   18901: 
1.1       root     18902: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     18903: #ifdef USE_DEBUGGER
                   18904: {
                   18905:        if(now_debugging) {
                   18906:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   18907:                        if(out_break_point.table[i].status == 1) {
                   18908:                                if(addr == out_break_point.table[i].addr) {
                   18909:                                        out_break_point.hit = i + 1;
                   18910:                                        now_suspended = true;
                   18911:                                        break;
                   18912:                                }
                   18913:                        }
                   18914:                }
                   18915:        }
                   18916:        debugger_write_io_byte(addr, val);
                   18917: }
                   18918: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   18919: #endif
1.1       root     18920: {
1.1.1.25  root     18921: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     18922:        if(fp_debug_log != NULL) {
1.1.1.43  root     18923: #ifdef USE_SERVICE_THREAD
                   18924:                if(addr != 0xf7)
                   18925: #endif
1.1.1.33  root     18926:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     18927:        }
                   18928: #endif
1.1       root     18929:        switch(addr) {
1.1.1.29  root     18930: #ifdef SW1US_PATCH
                   18931:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   18932:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   18933:                sio_write(0, addr - 1, val);
                   18934:                break;
                   18935: #else
1.1.1.25  root     18936:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   18937:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   18938:                dma_write(0, addr, val);
                   18939:                break;
1.1.1.29  root     18940: #endif
1.1.1.25  root     18941:        case 0x20: case 0x21:
1.1       root     18942:                pic_write(0, addr, val);
                   18943:                break;
1.1.1.25  root     18944:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     18945:                pit_write(addr & 0x03, val);
                   18946:                break;
1.1.1.7   root     18947:        case 0x60:
                   18948:                kbd_write_data(val);
                   18949:                break;
1.1.1.9   root     18950:        case 0x61:
                   18951:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   18952:                        // beep on
                   18953: //                     MessageBeep(-1);
                   18954:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   18955:                        // beep off
                   18956:                }
                   18957:                system_port = val;
                   18958:                break;
1.1       root     18959:        case 0x64:
1.1.1.7   root     18960:                kbd_write_command(val);
1.1       root     18961:                break;
                   18962:        case 0x70:
                   18963:                cmos_addr = val;
                   18964:                break;
                   18965:        case 0x71:
1.1.1.8   root     18966:                cmos_write(cmos_addr, val);
1.1       root     18967:                break;
1.1.1.25  root     18968:        case 0x81:
                   18969:                dma_page_write(0, 2, val);
                   18970:        case 0x82:
                   18971:                dma_page_write(0, 3, val);
                   18972:        case 0x83:
                   18973:                dma_page_write(0, 1, val);
                   18974:        case 0x87:
                   18975:                dma_page_write(0, 0, val);
                   18976:        case 0x89:
                   18977:                dma_page_write(1, 2, val);
                   18978:        case 0x8a:
                   18979:                dma_page_write(1, 3, val);
                   18980:        case 0x8b:
                   18981:                dma_page_write(1, 1, val);
                   18982:        case 0x8f:
                   18983:                dma_page_write(1, 0, val);
1.1       root     18984:        case 0x92:
1.1.1.7   root     18985:                i386_set_a20_line((val >> 1) & 1);
1.1       root     18986:                break;
1.1.1.25  root     18987:        case 0xa0: case 0xa1:
1.1       root     18988:                pic_write(1, addr, val);
                   18989:                break;
1.1.1.25  root     18990:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   18991:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     18992:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     18993:                break;
1.1.1.35  root     18994: #ifdef USE_SERVICE_THREAD
                   18995:        case 0xf7:
                   18996:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     18997:                if(in_service && cursor_moved) {
                   18998:                        // update cursor position before service is done
                   18999:                        pcbios_update_cursor_position();
                   19000:                        cursor_moved = false;
                   19001:                }
1.1.1.35  root     19002:                finish_service_loop();
                   19003:                break;
                   19004: #endif
1.1.1.37  root     19005:        case 0x278: case 0x279: case 0x27a:
                   19006:                pio_write(1, addr, val);
                   19007:                break;
1.1.1.29  root     19008:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   19009:                sio_write(3, addr, val);
                   19010:                break;
1.1.1.25  root     19011:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   19012:                sio_write(1, addr, val);
                   19013:                break;
                   19014:        case 0x378: case 0x379: case 0x37a:
                   19015:                pio_write(0, addr, val);
                   19016:                break;
1.1.1.37  root     19017:        case 0x3bc: case 0x3bd: case 0x3be:
                   19018:                pio_write(2, addr, val);
                   19019:                break;
1.1.1.29  root     19020:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   19021:                sio_write(2, addr, val);
                   19022:                break;
1.1.1.25  root     19023:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   19024:                sio_write(0, addr, val);
                   19025:                break;
1.1       root     19026:        default:
1.1.1.33  root     19027: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     19028:                break;
                   19029:        }
                   19030: }
                   19031: 
                   19032: void write_io_word(offs_t addr, UINT16 val)
                   19033: {
                   19034:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   19035:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   19036: }
                   19037: 
1.1.1.33  root     19038: #ifdef USE_DEBUGGER
                   19039: void debugger_write_io_word(offs_t addr, UINT16 val)
                   19040: {
                   19041:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   19042:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   19043: }
                   19044: #endif
                   19045: 
1.1       root     19046: void write_io_dword(offs_t addr, UINT32 val)
                   19047: {
                   19048:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   19049:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   19050:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   19051:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   19052: }
1.1.1.33  root     19053: 
                   19054: #ifdef USE_DEBUGGER
                   19055: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   19056: {
                   19057:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   19058:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   19059:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   19060:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   19061: }
                   19062: #endif

unix.superglobalmegacorp.com

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