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

1.1       root        1: /*
                      2:        MS-DOS Player for Win32 console
                      3: 
                      4:        Author : Takeda.Toshiya
                      5:        Date   : 2009.11.09-
                      6: */
                      7: 
                      8: #include "msdos.h"
                      9: 
1.1.1.28  root       10: void exit_handler();
                     11: 
1.1       root       12: #define fatalerror(...) { \
                     13:        fprintf(stderr, __VA_ARGS__); \
1.1.1.28  root       14:        exit_handler(); \
1.1       root       15:        exit(1); \
                     16: }
                     17: #define error(...) fprintf(stderr, "error: " __VA_ARGS__)
1.1.1.22  root       18: #define nolog(...)
                     19: 
1.1.1.33  root       20: //#define ENABLE_DEBUG_LOG
                     21: #ifdef ENABLE_DEBUG_LOG
1.1.1.22  root       22:        #define EXPORT_DEBUG_TO_FILE
                     23:        #define ENABLE_DEBUG_SYSCALL
                     24:        #define ENABLE_DEBUG_UNIMPLEMENTED
1.1.1.25  root       25:        #define ENABLE_DEBUG_IOPORT
1.1.1.22  root       26:        
                     27:        #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root       28:                FILE* fp_debug_log = NULL;
1.1.1.22  root       29:        #else
1.1.1.33  root       30:                #define fp_debug_log stderr
1.1.1.22  root       31:        #endif
                     32:        #ifdef ENABLE_DEBUG_UNIMPLEMENTED
                     33:                #define unimplemented_10h fatalerror
1.1.1.42  root       34:                #define unimplemented_13h fatalerror
1.1.1.25  root       35:                #define unimplemented_14h fatalerror
1.1.1.22  root       36:                #define unimplemented_15h fatalerror
                     37:                #define unimplemented_16h fatalerror
1.1.1.37  root       38:                #define unimplemented_17h fatalerror
1.1.1.22  root       39:                #define unimplemented_1ah fatalerror
                     40:                #define unimplemented_21h fatalerror
                     41:                #define unimplemented_2fh fatalerror
1.1.1.24  root       42:                #define unimplemented_33h fatalerror
1.1.1.22  root       43:                #define unimplemented_67h fatalerror
                     44:                #define unimplemented_xms fatalerror
                     45:        #endif
                     46: #endif
                     47: #ifndef unimplemented_10h
                     48:        #define unimplemented_10h nolog
                     49: #endif
1.1.1.42  root       50: #ifndef unimplemented_13h
                     51:        #define unimplemented_13h nolog
                     52: #endif
1.1.1.25  root       53: #ifndef unimplemented_14h
                     54:        #define unimplemented_14h nolog
                     55: #endif
1.1.1.22  root       56: #ifndef unimplemented_15h
                     57:        #define unimplemented_15h nolog
                     58: #endif
                     59: #ifndef unimplemented_16h
                     60:        #define unimplemented_16h nolog
                     61: #endif
1.1.1.37  root       62: #ifndef unimplemented_17h
                     63:        #define unimplemented_17h nolog
                     64: #endif
1.1.1.22  root       65: #ifndef unimplemented_1ah
                     66:        #define unimplemented_1ah nolog
                     67: #endif
                     68: #ifndef unimplemented_21h
                     69:        #define unimplemented_21h nolog
                     70: #endif
                     71: #ifndef unimplemented_2fh
                     72:        #define unimplemented_2fh nolog
                     73: #endif
1.1.1.24  root       74: #ifndef unimplemented_33h
                     75:        #define unimplemented_33h nolog
                     76: #endif
1.1.1.22  root       77: #ifndef unimplemented_67h
                     78:        #define unimplemented_67h nolog
                     79: #endif
                     80: #ifndef unimplemented_xms
                     81:        #define unimplemented_xms nolog
                     82: #endif
                     83: 
1.1.1.32  root       84: #define array_length(array) (sizeof(array) / sizeof(array[0]))
1.1.1.22  root       85: #define my_strchr(str, chr) (char *)_mbschr((unsigned char *)(str), (unsigned int)(chr))
                     86: #define my_strtok(tok, del) (char *)_mbstok((unsigned char *)(tok), (const unsigned char *)(del))
                     87: #define my_strupr(str) (char *)_mbsupr((unsigned char *)(str))
1.1       root       88: 
1.1.1.12  root       89: #if defined(__MINGW32__)
                     90: extern "C" int _CRT_glob = 0;
                     91: #endif
                     92: 
                     93: /*
                     94:        kludge for "more-standardized" C++
                     95: */
                     96: #if !defined(_MSC_VER)
                     97: inline int kludge_min(int a, int b) { return (a<b ? a:b); }
                     98: inline int kludge_max(int a, int b) { return (a>b ? a:b); }
                     99: #define min(a,b) kludge_min(a,b)
                    100: #define max(a,b) kludge_max(a,b)
1.1.1.14  root      101: #elif _MSC_VER >= 1400
                    102: void ignore_invalid_parameters(const wchar_t *, const wchar_t *, const wchar_t *, unsigned int, uintptr_t)
                    103: {
                    104: }
                    105: #endif
                    106: 
1.1.1.35  root      107: #define USE_VRAM_THREAD
1.1.1.14  root      108: 
1.1.1.35  root      109: #ifdef USE_VRAM_THREAD
1.1.1.14  root      110: static CRITICAL_SECTION vram_crit_sect;
                    111: #else
                    112: #define vram_flush()
1.1.1.12  root      113: #endif
                    114: 
1.1.1.14  root      115: #define VIDEO_REGEN *(UINT16 *)(mem + 0x44c)
                    116: #define SCR_BUF(y,x) scr_buf[(y) * scr_buf_size.X + (x)]
                    117: 
                    118: void change_console_size(int width, int height);
                    119: void clear_scr_buffer(WORD attr);
                    120: 
                    121: static UINT32 vram_length_char = 0, vram_length_attr = 0;
                    122: static UINT32 vram_last_length_char = 0, vram_last_length_attr = 0;
                    123: static COORD vram_coord_char, vram_coord_attr;
                    124: 
1.1.1.28  root      125: char temp_file_path[MAX_PATH];
                    126: bool temp_file_created = false;
                    127: 
1.1.1.14  root      128: bool ignore_illegal_insn = false;
                    129: bool limit_max_memory = false;
                    130: bool no_windows = false;
                    131: bool stay_busy = false;
1.1.1.19  root      132: bool support_ems = false;
                    133: #ifdef SUPPORT_XMS
                    134: bool support_xms = false;
                    135: #endif
1.1.1.29  root      136: int sio_port_number[4] = {0, 0, 0, 0};
1.1.1.14  root      137: 
                    138: BOOL is_vista_or_later;
                    139: 
1.1.1.35  root      140: #define UPDATE_OPS 16384
                    141: #define REQUEST_HARDWRE_UPDATE() { \
                    142:        update_ops = UPDATE_OPS - 1; \
                    143: }
                    144: UINT32 update_ops = 0;
                    145: UINT32 idle_ops = 0;
                    146: 
1.1.1.14  root      147: inline void maybe_idle()
                    148: {
                    149:        // if it appears to be in a tight loop, assume waiting for input
                    150:        // allow for one updated video character, for a spinning cursor
1.1.1.35  root      151:        if(!stay_busy && idle_ops < 1024 && vram_length_char <= 1 && vram_length_attr <= 1) {
1.1.1.14  root      152:                Sleep(10);
1.1.1.35  root      153:                REQUEST_HARDWRE_UPDATE();
1.1.1.14  root      154:        }
1.1.1.35  root      155:        idle_ops = 0;
1.1.1.14  root      156: }
1.1.1.12  root      157: 
1.1       root      158: /* ----------------------------------------------------------------------------
1.1.1.3   root      159:        MAME i86/i386
1.1       root      160: ---------------------------------------------------------------------------- */
                    161: 
1.1.1.10  root      162: #ifndef __BIG_ENDIAN__
1.1       root      163: #define LSB_FIRST
1.1.1.10  root      164: #endif
1.1       root      165: 
                    166: #ifndef INLINE
                    167: #define INLINE inline
                    168: #endif
                    169: #define U64(v) UINT64(v)
                    170: 
                    171: //#define logerror(...) fprintf(stderr, __VA_ARGS__)
                    172: #define logerror(...)
                    173: //#define popmessage(...) fprintf(stderr, __VA_ARGS__)
                    174: #define popmessage(...)
                    175: 
                    176: /*****************************************************************************/
1.1.1.10  root      177: /* src/emu/devcpu.h */
                    178: 
                    179: // CPU interface functions
                    180: #define CPU_INIT_NAME(name)                    cpu_init_##name
                    181: #define CPU_INIT(name)                         void CPU_INIT_NAME(name)()
                    182: #define CPU_INIT_CALL(name)                    CPU_INIT_NAME(name)()
                    183: 
                    184: #define CPU_RESET_NAME(name)                   cpu_reset_##name
                    185: #define CPU_RESET(name)                                void CPU_RESET_NAME(name)()
                    186: #define CPU_RESET_CALL(name)                   CPU_RESET_NAME(name)()
                    187: 
                    188: #define CPU_EXECUTE_NAME(name)                 cpu_execute_##name
                    189: #define CPU_EXECUTE(name)                      void CPU_EXECUTE_NAME(name)()
                    190: #define CPU_EXECUTE_CALL(name)                 CPU_EXECUTE_NAME(name)()
                    191: 
                    192: #define CPU_TRANSLATE_NAME(name)               cpu_translate_##name
                    193: #define CPU_TRANSLATE(name)                    int CPU_TRANSLATE_NAME(name)(address_spacenum space, int intention, offs_t *address)
                    194: #define CPU_TRANSLATE_CALL(name)               CPU_TRANSLATE_NAME(name)(space, intention, address)
                    195: 
                    196: #define CPU_DISASSEMBLE_NAME(name)             cpu_disassemble_##name
                    197: #define CPU_DISASSEMBLE(name)                  int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
                    198: #define CPU_DISASSEMBLE_CALL(name)             CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
                    199: 
1.1.1.14  root      200: #define CPU_MODEL_STR(name)                    #name
                    201: #define CPU_MODEL_NAME(name)                   CPU_MODEL_STR(name)
                    202: 
1.1.1.10  root      203: /*****************************************************************************/
                    204: /* src/emu/didisasm.h */
                    205: 
                    206: // Disassembler constants
                    207: const UINT32 DASMFLAG_SUPPORTED     = 0x80000000;   // are disassembly flags supported?
                    208: const UINT32 DASMFLAG_STEP_OUT      = 0x40000000;   // this instruction should be the end of a step out sequence
                    209: const UINT32 DASMFLAG_STEP_OVER     = 0x20000000;   // this instruction should be stepped over by setting a breakpoint afterwards
                    210: const UINT32 DASMFLAG_OVERINSTMASK  = 0x18000000;   // number of extra instructions to skip when stepping over
                    211: const UINT32 DASMFLAG_OVERINSTSHIFT = 27;           // bits to shift after masking to get the value
                    212: const UINT32 DASMFLAG_LENGTHMASK    = 0x0000ffff;   // the low 16-bits contain the actual length
                    213: 
                    214: /*****************************************************************************/
1.1       root      215: /* src/emu/diexec.h */
                    216: 
                    217: // I/O line states
                    218: enum line_state
                    219: {
                    220:        CLEAR_LINE = 0,                         // clear (a fired or held) line
                    221:        ASSERT_LINE,                            // assert an interrupt immediately
                    222:        HOLD_LINE,                              // hold interrupt line until acknowledged
                    223:        PULSE_LINE                              // pulse interrupt line instantaneously (only for NMI, RESET)
                    224: };
                    225: 
                    226: // I/O line definitions
                    227: enum
                    228: {
                    229:        INPUT_LINE_IRQ = 0,
                    230:        INPUT_LINE_NMI
                    231: };
                    232: 
                    233: /*****************************************************************************/
1.1.1.10  root      234: /* src/emu/dimemory.h */
1.1       root      235: 
1.1.1.10  root      236: // Translation intentions
                    237: const int TRANSLATE_TYPE_MASK       = 0x03;     // read write or fetch
                    238: const int TRANSLATE_USER_MASK       = 0x04;     // user mode or fully privileged
                    239: const int TRANSLATE_DEBUG_MASK      = 0x08;     // debug mode (no side effects)
                    240: 
                    241: const int TRANSLATE_READ            = 0;        // translate for read
                    242: const int TRANSLATE_WRITE           = 1;        // translate for write
                    243: const int TRANSLATE_FETCH           = 2;        // translate for instruction fetch
                    244: const int TRANSLATE_READ_USER       = (TRANSLATE_READ | TRANSLATE_USER_MASK);
                    245: const int TRANSLATE_WRITE_USER      = (TRANSLATE_WRITE | TRANSLATE_USER_MASK);
                    246: const int TRANSLATE_FETCH_USER      = (TRANSLATE_FETCH | TRANSLATE_USER_MASK);
                    247: const int TRANSLATE_READ_DEBUG      = (TRANSLATE_READ | TRANSLATE_DEBUG_MASK);
                    248: const int TRANSLATE_WRITE_DEBUG     = (TRANSLATE_WRITE | TRANSLATE_DEBUG_MASK);
                    249: const int TRANSLATE_FETCH_DEBUG     = (TRANSLATE_FETCH | TRANSLATE_DEBUG_MASK);
1.1       root      250: 
1.1.1.10  root      251: /*****************************************************************************/
                    252: /* src/emu/emucore.h */
1.1       root      253: 
1.1.1.10  root      254: // constants for expression endianness
                    255: enum endianness_t
                    256: {
                    257:        ENDIANNESS_LITTLE,
                    258:        ENDIANNESS_BIG
                    259: };
1.1       root      260: 
1.1.1.10  root      261: // declare native endianness to be one or the other
                    262: #ifdef LSB_FIRST
                    263: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
                    264: #else
                    265: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
                    266: #endif
                    267: 
                    268: // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
                    269: #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
                    270: 
                    271: // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
                    272: #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
                    273: 
                    274: // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
                    275: #define ENDIAN_VALUE_NE_NNE(endian,leval,beval)        (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
1.1       root      276: 
                    277: /*****************************************************************************/
                    278: /* src/emu/memory.h */
                    279: 
1.1.1.10  root      280: // address spaces
                    281: enum address_spacenum
                    282: {
                    283:        AS_0,                           // first address space
                    284:        AS_1,                           // second address space
                    285:        AS_2,                           // third address space
                    286:        AS_3,                           // fourth address space
                    287:        ADDRESS_SPACES,                 // maximum number of address spaces
                    288: 
                    289:        // alternate address space names for common use
                    290:        AS_PROGRAM = AS_0,              // program address space
                    291:        AS_DATA = AS_1,                 // data address space
                    292:        AS_IO = AS_2                    // I/O address space
                    293: };
                    294: 
1.1       root      295: // offsets and addresses are 32-bit (for now...)
1.1.1.30  root      296: //typedef UINT32       offs_t;
1.1       root      297: 
                    298: // read accessors
                    299: UINT8 read_byte(offs_t byteaddress)
1.1.1.33  root      300: #ifdef USE_DEBUGGER
                    301: {
                    302:        if(now_debugging) {
                    303:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    304:                        if(rd_break_point.table[i].status == 1) {
                    305:                                if(byteaddress == rd_break_point.table[i].addr) {
                    306:                                        rd_break_point.hit = i + 1;
                    307:                                        now_suspended = true;
                    308:                                        break;
                    309:                                }
                    310:                        }
                    311:                }
                    312:        }
                    313:        return(debugger_read_byte(byteaddress));
                    314: }
                    315: UINT8 debugger_read_byte(offs_t byteaddress)
                    316: #endif
1.1       root      317: {
1.1.1.4   root      318: #if defined(HAS_I386)
1.1       root      319:        if(byteaddress < MAX_MEM) {
                    320:                return mem[byteaddress];
1.1.1.3   root      321: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    322: //             return read_byte(byteaddress & 0xfffff);
1.1       root      323:        }
                    324:        return 0;
1.1.1.4   root      325: #else
                    326:        return mem[byteaddress];
                    327: #endif
1.1       root      328: }
                    329: 
                    330: UINT16 read_word(offs_t byteaddress)
1.1.1.33  root      331: #ifdef USE_DEBUGGER
                    332: {
                    333:        if(now_debugging) {
                    334:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    335:                        if(rd_break_point.table[i].status == 1) {
                    336:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 2) {
                    337:                                        rd_break_point.hit = i + 1;
                    338:                                        now_suspended = true;
                    339:                                        break;
                    340:                                }
                    341:                        }
                    342:                }
                    343:        }
                    344:        return(debugger_read_word(byteaddress));
                    345: }
                    346: UINT16 debugger_read_word(offs_t byteaddress)
                    347: #endif
1.1       root      348: {
1.1.1.14  root      349:        if(byteaddress == 0x41c) {
                    350:                // pointer to first free slot in keyboard buffer
                    351:                // XXX: the buffer itself doesn't actually exist in DOS memory
1.1.1.35  root      352:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                    353: #ifdef USE_SERVICE_THREAD
                    354:                        EnterCriticalSection(&key_buf_crit_sect);
                    355: #endif
1.1.1.41  root      356:                        int count = min(key_buf_char->count(), 15);
                    357:                        // write to top of key buffer
                    358:                        for(int i = 0; i < count; i++) {
                    359:                                mem[0x41e + 2 * i + 0] = key_buf_char->read_not_remove(i);
                    360:                                mem[0x41e + 2 * i + 1] = key_buf_scan->read_not_remove(i);
                    361:                        }
1.1.1.35  root      362: #ifdef USE_SERVICE_THREAD
                    363:                        LeaveCriticalSection(&key_buf_crit_sect);
                    364: #endif
                    365:                        if(count == 0) {
1.1.1.32  root      366:                                maybe_idle();
                    367:                        }
1.1.1.41  root      368:                        return (UINT16)(0x1e + 2 * count);
1.1.1.14  root      369:                }
1.1.1.41  root      370:                return 0x1e;
1.1.1.14  root      371:        }
1.1.1.4   root      372: #if defined(HAS_I386)
1.1       root      373:        if(byteaddress < MAX_MEM - 1) {
                    374:                return *(UINT16 *)(mem + byteaddress);
1.1.1.3   root      375: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    376: //             return read_word(byteaddress & 0xfffff);
1.1       root      377:        }
                    378:        return 0;
1.1.1.4   root      379: #else
                    380:        return *(UINT16 *)(mem + byteaddress);
                    381: #endif
1.1       root      382: }
                    383: 
                    384: UINT32 read_dword(offs_t byteaddress)
1.1.1.33  root      385: #ifdef USE_DEBUGGER
                    386: {
                    387:        if(now_debugging) {
                    388:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    389:                        if(rd_break_point.table[i].status == 1) {
                    390:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 4) {
                    391:                                        rd_break_point.hit = i + 1;
                    392:                                        now_suspended = true;
                    393:                                        break;
                    394:                                }
                    395:                        }
                    396:                }
                    397:        }
                    398:        return(debugger_read_dword(byteaddress));
                    399: }
                    400: UINT32 debugger_read_dword(offs_t byteaddress)
                    401: #endif
1.1       root      402: {
1.1.1.4   root      403: #if defined(HAS_I386)
1.1       root      404:        if(byteaddress < MAX_MEM - 3) {
                    405:                return *(UINT32 *)(mem + byteaddress);
1.1.1.3   root      406: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    407: //             return read_dword(byteaddress & 0xfffff);
1.1       root      408:        }
                    409:        return 0;
1.1.1.4   root      410: #else
                    411:        return *(UINT32 *)(mem + byteaddress);
                    412: #endif
1.1       root      413: }
                    414: 
                    415: // write accessors
1.1.1.35  root      416: #ifdef USE_VRAM_THREAD
1.1.1.14  root      417: void vram_flush_char()
                    418: {
                    419:        if(vram_length_char != 0) {
                    420:                DWORD num;
1.1.1.23  root      421:                WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, vram_length_char, vram_coord_char, &num);
1.1.1.14  root      422:                vram_length_char = vram_last_length_char = 0;
                    423:        }
                    424: }
                    425: 
                    426: void vram_flush_attr()
                    427: {
                    428:        if(vram_length_attr != 0) {
                    429:                DWORD num;
1.1.1.23  root      430:                WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, vram_length_attr, vram_coord_attr, &num);
1.1.1.14  root      431:                vram_length_attr = vram_last_length_attr = 0;
                    432:        }
                    433: }
                    434: 
                    435: void vram_flush()
                    436: {
                    437:        if(vram_length_char != 0 || vram_length_attr != 0) {
                    438:                EnterCriticalSection(&vram_crit_sect);
                    439:                vram_flush_char();
                    440:                vram_flush_attr();
                    441:                LeaveCriticalSection(&vram_crit_sect);
                    442:        }
                    443: }
                    444: #endif
                    445: 
                    446: void write_text_vram_char(offs_t offset, UINT8 data)
1.1.1.8   root      447: {
1.1.1.35  root      448: #ifdef USE_VRAM_THREAD
1.1.1.14  root      449:        static offs_t first_offset_char, last_offset_char;
                    450:        
                    451:        if(vram_length_char != 0) {
                    452:                if(offset <= last_offset_char && offset >= first_offset_char) {
                    453:                        scr_char[(offset - first_offset_char) >> 1] = data;
                    454:                        return;
                    455:                }
                    456:                if(offset != last_offset_char + 2) {
                    457:                        vram_flush_char();
                    458:                }
                    459:        }
                    460:        if(vram_length_char == 0) {
                    461:                first_offset_char = offset;
                    462:                vram_coord_char.X = (offset >> 1) % scr_width;
                    463:                vram_coord_char.Y = (offset >> 1) / scr_width + scr_top;
                    464:        }
                    465:        scr_char[vram_length_char++] = data;
                    466:        last_offset_char = offset;
                    467: #else
1.1.1.8   root      468:        COORD co;
                    469:        DWORD num;
                    470:        
1.1.1.14  root      471:        co.X = (offset >> 1) % scr_width;
                    472:        co.Y = (offset >> 1) / scr_width;
                    473:        scr_char[0] = data;
1.1.1.23  root      474:        WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, 1, co, &num);
1.1.1.14  root      475: #endif
                    476: }
                    477: 
                    478: void write_text_vram_attr(offs_t offset, UINT8 data)
                    479: {
1.1.1.35  root      480: #ifdef USE_VRAM_THREAD
1.1.1.14  root      481:        static offs_t first_offset_attr, last_offset_attr;
                    482:        
                    483:        if(vram_length_attr != 0) {
                    484:                if(offset <= last_offset_attr && offset >= first_offset_attr) {
                    485:                        scr_attr[(offset - first_offset_attr) >> 1] = data;
                    486:                        return;
                    487:                }
                    488:                if(offset != last_offset_attr + 2) {
                    489:                        vram_flush_attr();
                    490:                }
                    491:        }
                    492:        if(vram_length_attr == 0) {
                    493:                first_offset_attr = offset;
                    494:                vram_coord_attr.X = (offset >> 1) % scr_width;
                    495:                vram_coord_attr.Y = (offset >> 1) / scr_width + scr_top;
                    496:        }
                    497:        scr_attr[vram_length_attr++] = data;
                    498:        last_offset_attr = offset;
                    499: #else
                    500:        COORD co;
                    501:        DWORD num;
1.1.1.8   root      502:        
1.1.1.14  root      503:        co.X = (offset >> 1) % scr_width;
                    504:        co.Y = (offset >> 1) / scr_width;
                    505:        scr_attr[0] = data;
1.1.1.23  root      506:        WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, 1, co, &num);
1.1.1.14  root      507: #endif
                    508: }
                    509: 
                    510: void write_text_vram_byte(offs_t offset, UINT8 data)
                    511: {
1.1.1.35  root      512: #ifdef USE_VRAM_THREAD
1.1.1.14  root      513:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      514: #endif
1.1.1.8   root      515:        if(offset & 1) {
1.1.1.14  root      516:                write_text_vram_attr(offset, data);
1.1.1.8   root      517:        } else {
1.1.1.14  root      518:                write_text_vram_char(offset, data);
1.1.1.8   root      519:        }
1.1.1.35  root      520: #ifdef USE_VRAM_THREAD
1.1.1.14  root      521:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      522: #endif
1.1.1.8   root      523: }
                    524: 
                    525: void write_text_vram_word(offs_t offset, UINT16 data)
                    526: {
1.1.1.35  root      527: #ifdef USE_VRAM_THREAD
1.1.1.14  root      528:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      529: #endif
1.1.1.8   root      530:        if(offset & 1) {
1.1.1.14  root      531:                write_text_vram_attr(offset    , (data     ) & 0xff);
                    532:                write_text_vram_char(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      533:        } else {
1.1.1.14  root      534:                write_text_vram_char(offset    , (data     ) & 0xff);
                    535:                write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      536:        }
1.1.1.35  root      537: #ifdef USE_VRAM_THREAD
1.1.1.14  root      538:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      539: #endif
1.1.1.8   root      540: }
                    541: 
                    542: void write_text_vram_dword(offs_t offset, UINT32 data)
                    543: {
1.1.1.35  root      544: #ifdef USE_VRAM_THREAD
1.1.1.14  root      545:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      546: #endif
1.1.1.8   root      547:        if(offset & 1) {
1.1.1.14  root      548:                write_text_vram_attr(offset    , (data      ) & 0xff);
                    549:                write_text_vram_char(offset + 1, (data >>  8) & 0xff);
                    550:                write_text_vram_attr(offset + 2, (data >> 16) & 0xff);
                    551:                write_text_vram_char(offset + 3, (data >> 24) & 0xff);
                    552:        } else {
                    553:                write_text_vram_char(offset    , (data      ) & 0xff);
                    554:                write_text_vram_attr(offset + 1, (data >>  8) & 0xff);
                    555:                write_text_vram_char(offset + 2, (data >> 16) & 0xff);
                    556:                write_text_vram_attr(offset + 3, (data >> 24) & 0xff);
1.1.1.8   root      557:        }
1.1.1.35  root      558: #ifdef USE_VRAM_THREAD
1.1.1.14  root      559:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      560: #endif
1.1.1.8   root      561: }
                    562: 
1.1       root      563: void write_byte(offs_t byteaddress, UINT8 data)
1.1.1.33  root      564: #ifdef USE_DEBUGGER
                    565: {
                    566:        if(now_debugging) {
                    567:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    568:                        if(wr_break_point.table[i].status == 1) {
                    569:                                if(byteaddress == wr_break_point.table[i].addr) {
                    570:                                        wr_break_point.hit = i + 1;
                    571:                                        now_suspended = true;
                    572:                                        break;
                    573:                                }
                    574:                        }
                    575:                }
                    576:        }
                    577:        debugger_write_byte(byteaddress, data);
                    578: }
                    579: void debugger_write_byte(offs_t byteaddress, UINT8 data)
                    580: #endif
1.1       root      581: {
1.1.1.8   root      582:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      583:                mem[byteaddress] = data;
1.1.1.8   root      584:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      585:                if(!restore_console_on_exit) {
                    586:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      587:                }
1.1.1.8   root      588:                write_text_vram_byte(byteaddress - text_vram_top_address, data);
                    589:                mem[byteaddress] = data;
                    590:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    591:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    592:                        write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1       root      593:                }
                    594:                mem[byteaddress] = data;
1.1.1.4   root      595: #if defined(HAS_I386)
1.1.1.3   root      596:        } else if(byteaddress < MAX_MEM) {
1.1.1.4   root      597: #else
                    598:        } else {
                    599: #endif
1.1.1.3   root      600:                mem[byteaddress] = data;
1.1       root      601:        }
                    602: }
                    603: 
                    604: void write_word(offs_t byteaddress, UINT16 data)
1.1.1.33  root      605: #ifdef USE_DEBUGGER
                    606: {
                    607:        if(now_debugging) {
                    608:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    609:                        if(wr_break_point.table[i].status == 1) {
                    610:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 2) {
                    611:                                        wr_break_point.hit = i + 1;
                    612:                                        now_suspended = true;
                    613:                                        break;
                    614:                                }
                    615:                        }
                    616:                }
                    617:        }
                    618:        debugger_write_word(byteaddress, data);
                    619: }
                    620: void debugger_write_word(offs_t byteaddress, UINT16 data)
                    621: #endif
1.1       root      622: {
1.1.1.8   root      623:        if(byteaddress < MEMORY_END) {
1.1.1.14  root      624:                if(byteaddress == 0x450 + mem[0x462] * 2) {
                    625:                        COORD co;
                    626:                        co.X = data & 0xff;
                    627:                        co.Y = (data >> 8) + scr_top;
1.1.1.23  root      628:                        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), co);
1.1.1.14  root      629:                }
1.1.1.3   root      630:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8   root      631:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      632:                if(!restore_console_on_exit) {
                    633:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      634:                }
1.1.1.8   root      635:                write_text_vram_word(byteaddress - text_vram_top_address, data);
                    636:                *(UINT16 *)(mem + byteaddress) = data;
                    637:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    638:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    639:                        write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1       root      640:                }
                    641:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4   root      642: #if defined(HAS_I386)
1.1.1.3   root      643:        } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4   root      644: #else
                    645:        } else {
                    646: #endif
1.1.1.3   root      647:                *(UINT16 *)(mem + byteaddress) = data;
1.1       root      648:        }
                    649: }
                    650: 
                    651: void write_dword(offs_t byteaddress, UINT32 data)
1.1.1.33  root      652: #ifdef USE_DEBUGGER
                    653: {
                    654:        if(now_debugging) {
                    655:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    656:                        if(wr_break_point.table[i].status == 1) {
                    657:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 4) {
                    658:                                        wr_break_point.hit = i + 1;
                    659:                                        now_suspended = true;
                    660:                                        break;
                    661:                                }
                    662:                        }
                    663:                }
                    664:        }
                    665:        debugger_write_dword(byteaddress, data);
                    666: }
                    667: void debugger_write_dword(offs_t byteaddress, UINT32 data)
                    668: #endif
1.1       root      669: {
1.1.1.8   root      670:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      671:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8   root      672:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      673:                if(!restore_console_on_exit) {
                    674:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      675:                }
1.1.1.8   root      676:                write_text_vram_dword(byteaddress - text_vram_top_address, data);
                    677:                *(UINT32 *)(mem + byteaddress) = data;
                    678:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    679:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    680:                        write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1       root      681:                }
                    682:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4   root      683: #if defined(HAS_I386)
1.1.1.3   root      684:        } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4   root      685: #else
                    686:        } else {
                    687: #endif
1.1.1.3   root      688:                *(UINT32 *)(mem + byteaddress) = data;
1.1       root      689:        }
                    690: }
                    691: 
                    692: #define read_decrypted_byte read_byte
                    693: #define read_decrypted_word read_word
                    694: #define read_decrypted_dword read_dword
                    695: 
1.1.1.3   root      696: #define read_raw_byte read_byte
                    697: #define write_raw_byte write_byte
                    698: 
                    699: #define read_word_unaligned read_word
                    700: #define write_word_unaligned write_word
                    701: 
                    702: #define read_io_word_unaligned read_io_word
                    703: #define write_io_word_unaligned write_io_word
                    704: 
1.1       root      705: UINT8 read_io_byte(offs_t byteaddress);
                    706: UINT16 read_io_word(offs_t byteaddress);
                    707: UINT32 read_io_dword(offs_t byteaddress);
                    708: 
                    709: void write_io_byte(offs_t byteaddress, UINT8 data);
                    710: void write_io_word(offs_t byteaddress, UINT16 data);
                    711: void write_io_dword(offs_t byteaddress, UINT32 data);
                    712: 
                    713: /*****************************************************************************/
                    714: /* src/osd/osdcomm.h */
                    715: 
                    716: /* Highly useful macro for compile-time knowledge of an array size */
                    717: #define ARRAY_LENGTH(x)     (sizeof(x) / sizeof(x[0]))
                    718: 
1.1.1.3   root      719: #if defined(HAS_I386)
1.1.1.10  root      720:        static CPU_TRANSLATE(i386);
                    721:        #include "mame/lib/softfloat/softfloat.c"
                    722:        #include "mame/emu/cpu/i386/i386.c"
1.1.1.12  root      723:        #include "mame/emu/cpu/vtlb.c"
1.1.1.3   root      724: #elif defined(HAS_I286)
1.1.1.10  root      725:        #include "mame/emu/cpu/i86/i286.c"
1.1.1.3   root      726: #else
1.1.1.10  root      727:        #include "mame/emu/cpu/i86/i86.c"
1.1.1.3   root      728: #endif
1.1.1.33  root      729: #ifdef USE_DEBUGGER
1.1.1.10  root      730:        #include "mame/emu/cpu/i386/i386dasm.c"
1.1       root      731: #endif
                    732: 
1.1.1.3   root      733: #if defined(HAS_I386)
                    734:        #define SREG(x)                         m_sreg[x].selector
                    735:        #define SREG_BASE(x)                    m_sreg[x].base
                    736:        int cpu_type, cpu_step;
                    737: #else
                    738:        #define REG8(x)                         m_regs.b[x]
                    739:        #define REG16(x)                        m_regs.w[x]
                    740:        #define SREG(x)                         m_sregs[x]
                    741:        #define SREG_BASE(x)                    m_base[x]
1.1.1.33  root      742:        #define m_eip                           (m_pc - m_base[CS])
1.1.1.3   root      743:        #define m_CF                            m_CarryVal
                    744:        #define m_a20_mask                      AMASK
                    745:        #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
                    746:        #if defined(HAS_I286)
                    747:                #define i386_set_a20_line(x)    i80286_set_a20_line(x)
                    748:        #else
                    749:                #define i386_set_a20_line(x)
                    750:        #endif
                    751:        #define i386_set_irq_line(x, y)         set_irq_line(x, y)
                    752: #endif
1.1       root      753: 
                    754: void i386_jmp_far(UINT16 selector, UINT32 address)
                    755: {
1.1.1.3   root      756: #if defined(HAS_I386)
1.1       root      757:        if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3   root      758:                i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1       root      759:        } else {
1.1.1.3   root      760:                SREG(CS) = selector;
                    761:                m_performed_intersegment_jump = 1;
                    762:                i386_load_segment_descriptor(CS);
                    763:                m_eip = address;
                    764:                CHANGE_PC(m_eip);
1.1       root      765:        }
1.1.1.3   root      766: #elif defined(HAS_I286)
                    767:        i80286_code_descriptor(selector, address, 1);
                    768: #else
                    769:        SREG(CS) = selector;
                    770:        i386_load_segment_descriptor(CS);
                    771:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    772: #endif
1.1       root      773: }
                    774: 
1.1.1.35  root      775: #ifdef USE_SERVICE_THREAD
1.1.1.24  root      776: void i386_call_far(UINT16 selector, UINT32 address)
                    777: {
                    778: #if defined(HAS_I386)
                    779:        if(PROTECTED_MODE && !V8086_MODE) {
                    780:                i386_protected_mode_call(selector, address, 1, m_operand_size);
                    781:        } else {
                    782:                PUSH16(SREG(CS));
                    783:                PUSH16(m_eip);
                    784:                SREG(CS) = selector;
                    785:                m_performed_intersegment_jump = 1;
                    786:                i386_load_segment_descriptor(CS);
                    787:                m_eip = address;
                    788:                CHANGE_PC(m_eip);
                    789:        }
                    790: #else
                    791:        UINT16 ip = m_pc - SREG_BASE(CS);
                    792:        UINT16 cs = SREG(CS);
                    793: #if defined(HAS_I286)
                    794:        i80286_code_descriptor(selector, address, 2);
                    795: #else
                    796:        SREG(CS) = selector;
                    797:        i386_load_segment_descriptor(CS);
                    798:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    799: #endif
                    800:        PUSH(cs);
                    801:        PUSH(ip);
                    802:        CHANGE_PC(m_pc);
                    803: #endif
                    804: }
1.1.1.35  root      805: #endif
1.1.1.24  root      806: 
1.1.1.29  root      807: UINT16 i386_read_stack()
                    808: {
                    809: #if defined(HAS_I386)
                    810:        UINT32 ea, new_esp;
                    811:        if( STACK_32BIT ) {
                    812:                new_esp = REG32(ESP) + 2;
                    813:                ea = i386_translate(SS, new_esp - 2, 0);
                    814:        } else {
                    815:                new_esp = REG16(SP) + 2;
                    816:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    817:        }
                    818:        return READ16(ea);
                    819: #else
                    820:        UINT16 sp = m_regs.w[SP] + 2;
                    821:        return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
                    822: #endif
                    823: }
                    824: 
1.1       root      825: /* ----------------------------------------------------------------------------
1.1.1.33  root      826:        debugger
                    827: ---------------------------------------------------------------------------- */
                    828: 
                    829: #ifdef USE_DEBUGGER
                    830: #define TELNET_BLUE      0x0004 // text color contains blue.
                    831: #define TELNET_GREEN     0x0002 // text color contains green.
                    832: #define TELNET_RED       0x0001 // text color contains red.
                    833: #define TELNET_INTENSITY 0x0008 // text color is intensified.
                    834: 
                    835: int svr_socket = 0;
                    836: int cli_socket = 0;
                    837: 
                    838: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error);
                    839: 
                    840: void debugger_init()
                    841: {
                    842:        now_debugging = false;
                    843:        now_going = false;
                    844:        now_suspended = false;
                    845:        force_suspend = false;
                    846:        
                    847:        memset(&break_point, 0, sizeof(break_point_t));
                    848:        memset(&rd_break_point, 0, sizeof(break_point_t));
                    849:        memset(&wr_break_point, 0, sizeof(break_point_t));
                    850:        memset(&in_break_point, 0, sizeof(break_point_t));
                    851:        memset(&out_break_point, 0, sizeof(break_point_t));
                    852:        memset(&int_break_point, 0, sizeof(int_break_point_t));
                    853: }
                    854: 
1.1.1.45  root      855: void telnet_send(const char *string)
1.1.1.33  root      856: {
                    857:        char buffer[8192], *ptr;
                    858:        strcpy(buffer, string);
                    859:        while((ptr = strstr(buffer, "\n")) != NULL) {
                    860:                char tmp[8192];
                    861:                *ptr = '\0';
                    862:                sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
                    863:                strcpy(buffer, tmp);
                    864:        }
                    865:        
                    866:        int len = strlen(buffer), res;
                    867:        ptr = buffer;
                    868:        while(len > 0) {
                    869:                if((res = send(cli_socket, ptr, len, 0)) > 0) {
                    870:                        len -= res;
                    871:                        ptr += res;
                    872:                }
                    873:        }
                    874: }
                    875: 
                    876: void telnet_command(const char *format, ...)
                    877: {
                    878:        char buffer[1024];
                    879:        va_list ap;
                    880:        va_start(ap, format);
                    881:        vsprintf(buffer, format, ap);
                    882:        va_end(ap);
                    883:        
                    884:        telnet_send(buffer);
                    885: }
                    886: 
                    887: void telnet_printf(const char *format, ...)
                    888: {
                    889:        char buffer[1024];
                    890:        va_list ap;
                    891:        va_start(ap, format);
                    892:        vsprintf(buffer, format, ap);
                    893:        va_end(ap);
                    894:        
                    895:        if(fp_debugger != NULL) {
                    896:                fprintf(fp_debugger, "%s", buffer);
                    897:        }
                    898:        telnet_send(buffer);
                    899: }
                    900: 
                    901: bool telnet_gets(char *str, int n)
                    902: {
                    903:        char buffer[1024];
                    904:        int ptr = 0;
                    905:        
                    906:        telnet_command("\033[12l"); // local echo on
                    907:        telnet_command("\033[2l");  // key unlock
                    908:        
                    909:        while(!m_halted) {
                    910:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    911:                
                    912:                if(len > 0 && buffer[0] != 0xff) {
                    913:                        for(int i = 0; i < len; i++) {
                    914:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                    915:                                        str[ptr] = 0;
                    916:                                        telnet_command("\033[2h");  // key lock
                    917:                                        telnet_command("\033[12h"); // local echo off
                    918:                                        return(!m_halted);
                    919:                                } else if(buffer[i] == 0x08) {
                    920:                                        if(ptr > 0) {
                    921:                                                telnet_command("\033[0K"); // erase from cursor position
                    922:                                                ptr--;
                    923:                                        } else {
                    924:                                                telnet_command("\033[1C"); // move cursor forward
                    925:                                        }
                    926:                                } else if(ptr < n - 1) {
1.1.1.37  root      927:                                        if(buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
1.1.1.33  root      928:                                                str[ptr++] = buffer[i];
                    929:                                        }
                    930:                                } else {
                    931:                                        telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
                    932:                                }
                    933:                        }
                    934:                } else if(len == -1) {
                    935:                        if(WSAGetLastError() != WSAEWOULDBLOCK) {
                    936:                                return(false);
                    937:                        }
                    938:                } else if(len == 0) {
                    939:                        return(false);
                    940:                }
                    941:                Sleep(10);
                    942:        }
                    943:        return(!m_halted);
                    944: }
                    945: 
                    946: bool telnet_kbhit()
                    947: {
                    948:        char buffer[1024];
                    949:        
                    950:        if(!m_halted) {
                    951:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    952:                
                    953:                if(len > 0) {
                    954:                        for(int i = 0; i < len; i++) {
                    955:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                    956:                                        return(true);
                    957:                                }
                    958:                        }
                    959:                } else if(len == 0) {
                    960:                        return(true); // disconnected
                    961:                }
                    962:        }
                    963:        return(false);
                    964: }
                    965: 
                    966: bool telnet_disconnected()
                    967: {
                    968:        char buffer[1024];
                    969:        int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    970:        
                    971:        if(len == 0) {
                    972:                return(true);
                    973:        } else if(len == -1) {
                    974:                if(WSAGetLastError() != WSAEWOULDBLOCK) {
                    975:                        return(true);
                    976:                }
                    977:        }
                    978:        return(false);
                    979: }
                    980: 
                    981: void telnet_set_color(int color)
                    982: {
                    983:        telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
                    984: }
                    985: 
                    986: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
                    987: {
                    988: //     UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
                    989:        UINT8 ops[16];
                    990:        for(int i = 0; i < 16; i++) {
                    991:                ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
                    992:        }
                    993:        UINT8 *oprom = ops;
                    994:        
                    995: #if defined(HAS_I386)
                    996:        if(m_operand_size) {
                    997:                return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
                    998:        } else
                    999: #endif
                   1000:        return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
                   1001: }
                   1002: 
                   1003: void debugger_regs_info(char *buffer)
                   1004: {
                   1005: #if defined(HAS_I386)
                   1006:        UINT32 flags = get_flags();
                   1007: #else
                   1008:        UINT32 flags = CompressFlags();
                   1009: #endif
                   1010: #if defined(HAS_I386)
                   1011:        if(m_operand_size) {
                   1012:                sprintf(buffer, "EAX=%08X  EBX=%08X  ECX=%08X  EDX=%08X\nESP=%08X  EBP=%08X  ESI=%08X  EDI=%08X\nEIP=%08X  DS=%04X  ES=%04X  SS=%04X  CS=%04X  FLAG=[%c %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c]\n",
                   1013:                REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SP), REG16(BP), REG16(SI), REG16(DI), m_eip, SREG(DS), SREG(ES), SREG(SS), SREG(CS),
                   1014:                PROTECTED_MODE ? "PE" : "--",
                   1015:                (flags & 0x40000) ? 'A' : '-',
                   1016:                (flags & 0x20000) ? 'V' : '-',
                   1017:                (flags & 0x10000) ? 'R' : '-',
                   1018:                (flags & 0x04000) ? 'N' : '-',
                   1019:                (flags & 0x02000) ? '1' : '0',
                   1020:                (flags & 0x01000) ? '1' : '0',
                   1021:                (flags & 0x00800) ? 'O' : '-',
                   1022:                (flags & 0x00400) ? 'D' : '-',
                   1023:                (flags & 0x00200) ? 'I' : '-',
                   1024:                (flags & 0x00100) ? 'T' : '-',
                   1025:                (flags & 0x00080) ? 'S' : '-',
                   1026:                (flags & 0x00040) ? 'Z' : '-',
                   1027:                (flags & 0x00010) ? 'A' : '-',
                   1028:                (flags & 0x00004) ? 'P' : '-',
                   1029:                (flags & 0x00001) ? 'C' : '-');
                   1030:        } else {
                   1031: #endif
                   1032:                sprintf(buffer, "AX=%04X  BX=%04X  CX=%04X  DX=%04X  SP=%04X  BP=%04X  SI=%04X  DI=%04X\nIP=%04X  DS=%04X  ES=%04X  SS=%04X  CS=%04X  FLAG=[%c %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c]\n",
                   1033:                REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SP), REG16(BP), REG16(SI), REG16(DI), m_eip, SREG(DS), SREG(ES), SREG(SS), SREG(CS),
                   1034: #if defined(HAS_I386)
                   1035:                PROTECTED_MODE ? "PE" : "--",
                   1036: #else
                   1037:                "--",
                   1038: #endif
                   1039:                (flags & 0x40000) ? 'A' : '-',
                   1040:                (flags & 0x20000) ? 'V' : '-',
                   1041:                (flags & 0x10000) ? 'R' : '-',
                   1042:                (flags & 0x04000) ? 'N' : '-',
                   1043:                (flags & 0x02000) ? '1' : '0',
                   1044:                (flags & 0x01000) ? '1' : '0',
                   1045:                (flags & 0x00800) ? 'O' : '-',
                   1046:                (flags & 0x00400) ? 'D' : '-',
                   1047:                (flags & 0x00200) ? 'I' : '-',
                   1048:                (flags & 0x00100) ? 'T' : '-',
                   1049:                (flags & 0x00080) ? 'S' : '-',
                   1050:                (flags & 0x00040) ? 'Z' : '-',
                   1051:                (flags & 0x00010) ? 'A' : '-',
                   1052:                (flags & 0x00004) ? 'P' : '-',
                   1053:                (flags & 0x00001) ? 'C' : '-');
                   1054: #if defined(HAS_I386)
                   1055:        }
                   1056: #endif
                   1057: }
                   1058: 
                   1059: void debugger_process_info(char *buffer)
                   1060: {
                   1061:        UINT16 psp_seg = current_psp;
                   1062:        process_t *process;
                   1063:        bool check[0x10000] = {0};
                   1064:        
                   1065:        buffer[0] = '\0';
                   1066:        
                   1067:        while(!check[psp_seg] && (process  = msdos_process_info_get(psp_seg, false)) != NULL) {
                   1068:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1069:                char *file = process->module_path, *s;
                   1070:                char tmp[8192];
                   1071:                
                   1072:                while((s = strstr(file, "\\")) != NULL) {
                   1073:                        file = s + 1;
                   1074:                }
                   1075:                sprintf(tmp, "PSP=%04X  ENV=%04X  RETURN=%04X:%04X  PROGRAM=%s\n", psp_seg, psp->env_seg, psp->int_22h.w.h, psp->int_22h.w.l, my_strupr(file));
                   1076:                strcat(tmp, buffer);
                   1077:                strcpy(buffer, tmp);
                   1078:                
                   1079:                check[psp_seg] = true;
                   1080:                psp_seg = psp->parent_psp;
                   1081:        }
                   1082: }
                   1083: 
                   1084: UINT32 debugger_get_val(const char *str)
                   1085: {
                   1086:        char tmp[1024];
                   1087:        
                   1088:        if(str == NULL || strlen(str) == 0) {
                   1089:                return(0);
                   1090:        }
                   1091:        strcpy(tmp, str);
                   1092:        
                   1093:        if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
                   1094:                // ank
                   1095:                return(tmp[1] & 0xff);
                   1096:        } else if(tmp[0] == '%') {
                   1097:                // decimal
                   1098:                return(strtoul(tmp + 1, NULL, 10));
                   1099:        }
                   1100:        return(strtoul(tmp, NULL, 16));
                   1101: }
                   1102: 
                   1103: UINT32 debugger_get_seg(const char *str, UINT32 val)
                   1104: {
                   1105:        char tmp[1024], *s;
                   1106:        
                   1107:        if(str == NULL || strlen(str) == 0) {
                   1108:                return(val);
                   1109:        }
                   1110:        strcpy(tmp, str);
                   1111:        
                   1112:        if((s = strstr(tmp, ":")) != NULL) {
                   1113:                // 0000:0000
                   1114:                *s = '\0';
                   1115:                return(debugger_get_val(tmp));
                   1116:        }
                   1117:        return(val);
                   1118: }
                   1119: 
                   1120: UINT32 debugger_get_ofs(const char *str)
                   1121: {
                   1122:        char tmp[1024], *s;
                   1123:        
                   1124:        if(str == NULL || strlen(str) == 0) {
                   1125:                return(0);
                   1126:        }
                   1127:        strcpy(tmp, str);
                   1128:        
                   1129:        if((s = strstr(tmp, ":")) != NULL) {
                   1130:                // 0000:0000
                   1131:                return(debugger_get_val(s + 1));
                   1132:        }
                   1133:        return(debugger_get_val(tmp));
                   1134: }
                   1135: 
                   1136: void debugger_main()
                   1137: {
                   1138:        telnet_command("\033[20h"); // cr-lf
                   1139:        
                   1140:        force_suspend = true;
                   1141:        now_going = false;
                   1142:        now_debugging = true;
                   1143:        Sleep(100);
                   1144:        
                   1145:        if(!m_halted && !now_suspended) {
                   1146:                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1147:                telnet_printf("waiting until cpu is suspended...\n");
                   1148:        }
                   1149:        while(!m_halted && !now_suspended) {
                   1150:                if(telnet_disconnected()) {
                   1151:                        break;
                   1152:                }
                   1153:                Sleep(10);
                   1154:        }
                   1155:        
                   1156:        char buffer[8192];
                   1157:        
                   1158:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1159:        debugger_process_info(buffer);
                   1160:        telnet_printf("%s", buffer);
                   1161:        debugger_regs_info(buffer);
                   1162:        telnet_printf("%s", buffer);
                   1163:        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1164:        telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
                   1165:        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1166:        debugger_dasm(buffer, SREG(CS), m_eip);
                   1167:        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1168:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1169:        
                   1170:        #define MAX_COMMAND_LEN 64
                   1171:        
                   1172:        char command[MAX_COMMAND_LEN + 1];
                   1173:        char prev_command[MAX_COMMAND_LEN + 1] = {0};
                   1174:        
                   1175:        UINT32 data_seg = SREG(DS);
                   1176:        UINT32 data_ofs = 0;
                   1177:        UINT32 dasm_seg = SREG(CS);
                   1178:        UINT32 dasm_ofs = m_eip;
                   1179:        
                   1180:        while(!m_halted) {
                   1181:                telnet_printf("- ");
                   1182:                command[0] = '\0';
                   1183:                
                   1184:                if(fi_debugger != NULL) {
                   1185:                        while(command[0] == '\0') {
                   1186:                                if(fgets(command, sizeof(command), fi_debugger) == NULL) {
                   1187:                                        break;
                   1188:                                }
                   1189:                                while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
                   1190:                                        command[strlen(command) - 1] = '\0';
                   1191:                                }
                   1192:                        }
                   1193:                        if(command[0] != '\0') {
                   1194:                                telnet_command("%s\n", command);
                   1195:                        }
                   1196:                }
                   1197:                if(command[0] == '\0') {
                   1198:                        if(!telnet_gets(command, sizeof(command))) {
                   1199:                                break;
                   1200:                        }
                   1201:                }
                   1202:                if(command[0] == '\0') {
                   1203:                        strcpy(command, prev_command);
                   1204:                } else {
                   1205:                        strcpy(prev_command, command);
                   1206:                }
                   1207:                if(fp_debugger != NULL) {
                   1208:                        fprintf(fp_debugger, "%s\n", command);
                   1209:                }
                   1210:                
                   1211:                if(!m_halted && command[0] != 0) {
                   1212:                        char *params[32], *token = NULL;
                   1213:                        int num = 0;
                   1214:                        
                   1215:                        if((token = strtok(command, " ")) != NULL) {
                   1216:                                params[num++] = token;
                   1217:                                while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
                   1218:                                        params[num++] = token;
                   1219:                                }
                   1220:                        }
                   1221:                        if(stricmp(params[0], "D") == 0) {
                   1222:                                if(num <= 3) {
                   1223:                                        if(num >= 2) {
                   1224:                                                data_seg = debugger_get_seg(params[1], data_seg);
                   1225:                                                data_ofs = debugger_get_ofs(params[1]);
                   1226:                                        }
                   1227:                                        UINT32 end_seg = data_seg;
                   1228:                                        UINT32 end_ofs = data_ofs + 8 * 16 - 1;
                   1229:                                        if(num == 3) {
                   1230:                                                end_seg = debugger_get_seg(params[2], data_seg);
                   1231:                                                end_ofs = debugger_get_ofs(params[2]);
                   1232:                                        }
                   1233:                                        UINT64 start_addr = (data_seg << 4) + data_ofs;
                   1234:                                        UINT64 end_addr = (end_seg << 4) + end_ofs;
1.1.1.37  root     1235: //                                     bool is_sjis = false;
1.1.1.33  root     1236:                                        
                   1237:                                        for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
                   1238:                                                if((addr & 0x0f) == 0) {
                   1239:                                                        if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
                   1240:                                                                data_seg += 0x1000;
                   1241:                                                                data_ofs -= 0x10000;
                   1242:                                                        }
                   1243:                                                        telnet_printf("%06X:%04X ", data_seg, data_ofs);
                   1244:                                                        memset(buffer, 0, sizeof(buffer));
                   1245:                                                }
                   1246:                                                if(addr < start_addr || addr > end_addr) {
                   1247:                                                        telnet_printf("   ");
                   1248:                                                        buffer[addr & 0x0f] = ' ';
                   1249:                                                } else {
                   1250:                                                        UINT8 data = debugger_read_byte(addr & ADDR_MASK);
                   1251:                                                        telnet_printf(" %02X", data);
1.1.1.37  root     1252: //                                                     if(is_sjis) {
1.1.1.33  root     1253: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1254: //                                                             is_sjis = false;
1.1.1.33  root     1255: //                                                     } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
                   1256: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1257: //                                                             is_sjis = true;
1.1.1.33  root     1258: //                                                     } else
                   1259:                                                        if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
                   1260:                                                                buffer[addr & 0x0f] = data;
                   1261:                                                        } else {
                   1262:                                                                buffer[addr & 0x0f] = '.';
                   1263:                                                        }
                   1264:                                                }
                   1265:                                                if((addr & 0x0f) == 0x0f) {
                   1266:                                                        telnet_printf("  %s\n", buffer);
                   1267:                                                }
                   1268:                                        }
                   1269:                                        if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
                   1270:                                                data_seg += 0x1000;
                   1271:                                                data_ofs -= 0x10000;
                   1272:                                        }
                   1273:                                        prev_command[1] = '\0'; // remove parameters to dump continuously
                   1274:                                } else {
                   1275:                                        telnet_printf("invalid parameter number\n");
                   1276:                                }
                   1277:                        } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 0) {
                   1278:                                if(num >= 3) {
                   1279:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1280:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1281:                                        for(int i = 2, j = 0; i < num; i++, j++) {
                   1282:                                                debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
                   1283:                                        }
                   1284:                                } else {
                   1285:                                        telnet_printf("invalid parameter number\n");
                   1286:                                }
                   1287:                        } else if(stricmp(params[0], "EW") == 0) {
                   1288:                                if(num >= 3) {
                   1289:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1290:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1291:                                        for(int i = 2, j = 0; i < num; i++, j += 2) {
                   1292:                                                debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
                   1293:                                        }
                   1294:                                } else {
                   1295:                                        telnet_printf("invalid parameter number\n");
                   1296:                                }
                   1297:                        } else if(stricmp(params[0], "ED") == 0) {
                   1298:                                if(num >= 3) {
                   1299:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1300:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1301:                                        for(int i = 2, j = 0; i < num; i++, j += 4) {
                   1302:                                                debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
                   1303:                                        }
                   1304:                                } else {
                   1305:                                        telnet_printf("invalid parameter number\n");
                   1306:                                }
                   1307:                        } else if(stricmp(params[0], "EA") == 0) {
                   1308:                                if(num >= 3) {
                   1309:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1310:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1311:                                        strcpy(buffer, prev_command);
                   1312:                                        if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
                   1313:                                                int len = strlen(token);
                   1314:                                                for(int i = 0; i < len; i++) {
                   1315:                                                        debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
                   1316:                                                }
                   1317:                                        } else {
                   1318:                                                telnet_printf("invalid parameter\n");
                   1319:                                        }
                   1320:                                } else {
                   1321:                                        telnet_printf("invalid parameter number\n");
                   1322:                                }
                   1323:                        } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
                   1324:                                if(num == 2) {
                   1325:                                        telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
                   1326:                                } else {
                   1327:                                        telnet_printf("invalid parameter number\n");
                   1328:                                }
                   1329:                        } else if(stricmp(params[0], "IW") == 0) {
                   1330:                                if(num == 2) {
                   1331:                                        telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
                   1332:                                } else {
                   1333:                                        telnet_printf("invalid parameter number\n");
                   1334:                                }
                   1335:                        } else if(stricmp(params[0], "ID") == 0) {
                   1336:                                if(num == 2) {
                   1337:                                        telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
                   1338:                                } else {
                   1339:                                        telnet_printf("invalid parameter number\n");
                   1340:                                }
                   1341:                        } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
                   1342:                                if(num == 3) {
                   1343:                                        debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
                   1344:                                } else {
                   1345:                                        telnet_printf("invalid parameter number\n");
                   1346:                                }
                   1347:                        } else if(stricmp(params[0], "OW") == 0) {
                   1348:                                if(num == 3) {
                   1349:                                        debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
                   1350:                                } else {
                   1351:                                        telnet_printf("invalid parameter number\n");
                   1352:                                }
                   1353:                        } else if(stricmp(params[0], "OD") == 0) {
                   1354:                                if(num == 3) {
                   1355:                                        debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
                   1356:                                } else {
                   1357:                                        telnet_printf("invalid parameter number\n");
                   1358:                                }
                   1359:                        } else if(stricmp(params[0], "R") == 0) {
                   1360:                                if(num == 1) {
                   1361:                                        debugger_regs_info(buffer);
                   1362:                                        telnet_printf("%s", buffer);
                   1363:                                } else if(num == 3) {
                   1364: #if defined(HAS_I386)
                   1365:                                        if(stricmp(params[1], "EAX") == 0) {
                   1366:                                                REG32(EAX) = debugger_get_val(params[2]);
                   1367:                                        } else if(stricmp(params[1], "EBX") == 0) {
                   1368:                                                REG32(EBX) = debugger_get_val(params[2]);
                   1369:                                        } else if(stricmp(params[1], "ECX") == 0) {
                   1370:                                                REG32(ECX) = debugger_get_val(params[2]);
                   1371:                                        } else if(stricmp(params[1], "EDX") == 0) {
                   1372:                                                REG32(EDX) = debugger_get_val(params[2]);
                   1373:                                        } else if(stricmp(params[1], "ESP") == 0) {
                   1374:                                                REG32(ESP) = debugger_get_val(params[2]);
                   1375:                                        } else if(stricmp(params[1], "EBP") == 0) {
                   1376:                                                REG32(EBP) = debugger_get_val(params[2]);
                   1377:                                        } else if(stricmp(params[1], "ESI") == 0) {
                   1378:                                                REG32(ESI) = debugger_get_val(params[2]);
                   1379:                                        } else if(stricmp(params[1], "EDI") == 0) {
                   1380:                                                REG32(EDI) = debugger_get_val(params[2]);
                   1381:                                        } else
                   1382: #endif
                   1383:                                        if(stricmp(params[1], "AX") == 0) {
                   1384:                                                REG16(AX) = debugger_get_val(params[2]);
                   1385:                                        } else if(stricmp(params[1], "BX") == 0) {
                   1386:                                                REG16(BX) = debugger_get_val(params[2]);
                   1387:                                        } else if(stricmp(params[1], "CX") == 0) {
                   1388:                                                REG16(CX) = debugger_get_val(params[2]);
                   1389:                                        } else if(stricmp(params[1], "DX") == 0) {
                   1390:                                                REG16(DX) = debugger_get_val(params[2]);
                   1391:                                        } else if(stricmp(params[1], "SP") == 0) {
                   1392:                                                REG16(SP) = debugger_get_val(params[2]);
                   1393:                                        } else if(stricmp(params[1], "BP") == 0) {
                   1394:                                                REG16(BP) = debugger_get_val(params[2]);
                   1395:                                        } else if(stricmp(params[1], "SI") == 0) {
                   1396:                                                REG16(SI) = debugger_get_val(params[2]);
                   1397:                                        } else if(stricmp(params[1], "DI") == 0) {
                   1398:                                                REG16(DI) = debugger_get_val(params[2]);
                   1399:                                        } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
                   1400: #if defined(HAS_I386)
                   1401:                                                if(m_operand_size) {
                   1402:                                                        m_eip = debugger_get_val(params[2]);
                   1403:                                                } else {
                   1404:                                                        m_eip = debugger_get_val(params[2]) & 0xffff;
                   1405:                                                }
                   1406:                                                CHANGE_PC(m_eip);
                   1407: #else
                   1408:                                                m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
                   1409:                                                CHANGE_PC(m_pc);
                   1410: #endif
                   1411:                                        } else if(stricmp(params[1], "AL") == 0) {
                   1412:                                                REG8(AL) = debugger_get_val(params[2]);
                   1413:                                        } else if(stricmp(params[1], "AH") == 0) {
                   1414:                                                REG8(AH) = debugger_get_val(params[2]);
                   1415:                                        } else if(stricmp(params[1], "BL") == 0) {
                   1416:                                                REG8(BL) = debugger_get_val(params[2]);
                   1417:                                        } else if(stricmp(params[1], "BH") == 0) {
                   1418:                                                REG8(BH) = debugger_get_val(params[2]);
                   1419:                                        } else if(stricmp(params[1], "CL") == 0) {
                   1420:                                                REG8(CL) = debugger_get_val(params[2]);
                   1421:                                        } else if(stricmp(params[1], "CH") == 0) {
                   1422:                                                REG8(CH) = debugger_get_val(params[2]);
                   1423:                                        } else if(stricmp(params[1], "DL") == 0) {
                   1424:                                                REG8(DL) = debugger_get_val(params[2]);
                   1425:                                        } else if(stricmp(params[1], "DH") == 0) {
                   1426:                                                REG8(DH) = debugger_get_val(params[2]);
                   1427:                                        } else {
                   1428:                                                telnet_printf("unknown register %s\n", params[1]);
                   1429:                                        }
                   1430:                                } else {
                   1431:                                        telnet_printf("invalid parameter number\n");
                   1432:                                }
                   1433:                        } else if(_tcsicmp(params[0], "S") == 0) {
                   1434:                                if(num >= 4) {
                   1435:                                        UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
                   1436:                                        UINT32 cur_ofs = debugger_get_ofs(params[1]);
                   1437:                                        UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
                   1438:                                        UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1439:                                        UINT8 list[32];
                   1440:                                        
                   1441:                                        for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1442:                                                list[j] = debugger_get_val(params[i]);
                   1443:                                        }
                   1444:                                        while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
                   1445:                                                bool found = true;
                   1446:                                                for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1447:                                                        if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
                   1448:                                                                found = false;
                   1449:                                                                break;
                   1450:                                                        }
                   1451:                                                }
                   1452:                                                if(found) {
                   1453:                                                        telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
                   1454:                                                }
                   1455:                                                if((cur_ofs += 1) > 0xffff) {
                   1456:                                                        cur_seg += 0x1000;
                   1457:                                                        cur_ofs -= 0x10000;
                   1458:                                                }
                   1459:                                        }
                   1460:                                } else {
                   1461:                                        telnet_printf("invalid parameter number\n");
                   1462:                                }
                   1463:                        } else if(stricmp(params[0], "U") == 0) {
                   1464:                                if(num <= 3) {
                   1465:                                        if(num >= 2) {
                   1466:                                                dasm_seg = debugger_get_seg(params[1], dasm_seg);
                   1467:                                                dasm_ofs = debugger_get_ofs(params[1]);
                   1468:                                        }
                   1469:                                        if(num == 3) {
                   1470:                                                UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
                   1471:                                                UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1472:                                                
                   1473:                                                while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
                   1474:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1475:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1476:                                                        for(int i = 0; i < len; i++) {
                   1477:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1478:                                                        }
                   1479:                                                        for(int i = len; i < 8; i++) {
                   1480:                                                                telnet_printf("  ");
                   1481:                                                        }
                   1482:                                                        telnet_printf("  %s\n", buffer);
                   1483:                                                        if((dasm_ofs += len) > 0xffff) {
                   1484:                                                                dasm_seg += 0x1000;
                   1485:                                                                dasm_ofs -= 0x10000;
                   1486:                                                        }
                   1487:                                                }
                   1488:                                        } else {
                   1489:                                                for(int i = 0; i < 16; i++) {
                   1490:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1491:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1492:                                                        for(int i = 0; i < len; i++) {
                   1493:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1494:                                                        }
                   1495:                                                        for(int i = len; i < 8; i++) {
                   1496:                                                                telnet_printf("  ");
                   1497:                                                        }
                   1498:                                                        telnet_printf("  %s\n", buffer);
                   1499:                                                        if((dasm_ofs += len) > 0xffff) {
                   1500:                                                                dasm_seg += 0x1000;
                   1501:                                                                dasm_ofs -= 0x10000;
                   1502:                                                        }
                   1503:                                                }
                   1504:                                        }
                   1505:                                        prev_command[1] = '\0'; // remove parameters to disassemble continuously
                   1506:                                } else {
                   1507:                                        telnet_printf("invalid parameter number\n");
                   1508:                                }
                   1509:                        } else if(stricmp(params[0], "H") == 0) {
                   1510:                                if(num == 3) {
                   1511:                                        UINT32 l = debugger_get_val(params[1]);
                   1512:                                        UINT32 r = debugger_get_val(params[2]);
                   1513:                                        telnet_printf("%08X  %08X\n", l + r, l - r);
                   1514:                                } else {
                   1515:                                        telnet_printf("invalid parameter number\n");
                   1516:                                }
                   1517:                        } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
                   1518:                                break_point_t *break_point_ptr;
                   1519:                                #define GET_BREAK_POINT_PTR() { \
                   1520:                                        if(params[0][0] == 'R') { \
                   1521:                                                break_point_ptr = &rd_break_point; \
                   1522:                                        } else if(params[0][0] == 'W') { \
                   1523:                                                break_point_ptr = &wr_break_point; \
                   1524:                                        } else if(params[0][0] == 'I') { \
                   1525:                                                break_point_ptr = &in_break_point; \
                   1526:                                        } else if(params[0][0] == 'O') { \
                   1527:                                                break_point_ptr = &out_break_point; \
                   1528:                                        } else { \
                   1529:                                                break_point_ptr = &break_point; \
                   1530:                                        } \
                   1531:                                }
                   1532:                                GET_BREAK_POINT_PTR();
                   1533:                                if(num == 2) {
                   1534:                                        UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1535:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1536:                                        bool found = false;
                   1537:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1538:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
                   1539:                                                        break_point_ptr->table[i].addr = (seg << 4) + ofs;
                   1540:                                                        break_point_ptr->table[i].seg = seg;
                   1541:                                                        break_point_ptr->table[i].ofs = ofs;
                   1542:                                                        break_point_ptr->table[i].status = 1;
                   1543:                                                        found = true;
                   1544:                                                }
                   1545:                                        }
                   1546:                                        if(!found) {
                   1547:                                                telnet_printf("too many break points\n");
                   1548:                                        }
                   1549:                                } else {
                   1550:                                        telnet_printf("invalid parameter number\n");
                   1551:                                }
                   1552:                        } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
                   1553:                                break_point_t *break_point_ptr;
                   1554:                                GET_BREAK_POINT_PTR();
                   1555:                                if(num == 2) {
                   1556:                                        UINT32 addr = debugger_get_val(params[1]);
                   1557:                                        bool found = false;
                   1558:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1559:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
                   1560:                                                        break_point_ptr->table[i].addr = addr;
                   1561:                                                        break_point_ptr->table[i].status = 1;
                   1562:                                                        found = true;
                   1563:                                                }
                   1564:                                        }
                   1565:                                        if(!found) {
                   1566:                                                telnet_printf("too many break points\n");
                   1567:                                        }
                   1568:                                } else {
                   1569:                                        telnet_printf("invalid parameter number\n");
                   1570:                                }
                   1571:                        } else if(stricmp(params[0], "BC") == 0 || stricmp(params[0], "RBC") == 0 || stricmp(params[0], "WBC") == 0 || stricmp(params[0], "IBC") == 0 || stricmp(params[0], "OBC") == 0) {
                   1572:                                break_point_t *break_point_ptr;
                   1573:                                GET_BREAK_POINT_PTR();
                   1574:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1575:                                        memset(break_point_ptr, 0, sizeof(break_point_t));
                   1576:                                } else if(num >= 2) {
                   1577:                                        for(int i = 1; i < num; i++) {
                   1578:                                                int index = debugger_get_val(params[i]);
                   1579:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1580:                                                        telnet_printf("invalid index %x\n", index);
                   1581:                                                } else {
                   1582:                                                        break_point_ptr->table[index - 1].addr = 0;
                   1583:                                                        break_point_ptr->table[index - 1].seg = 0;
                   1584:                                                        break_point_ptr->table[index - 1].ofs = 0;
                   1585:                                                        break_point_ptr->table[index - 1].status = 0;
                   1586:                                                }
                   1587:                                        }
                   1588:                                } else {
                   1589:                                        telnet_printf("invalid parameter number\n");
                   1590:                                }
                   1591:                        } else if(stricmp(params[0], "BD") == 0 || stricmp(params[0], "RBD") == 0 || stricmp(params[0], "WBD") == 0 || stricmp(params[0], "IBD") == 0 || stricmp(params[0], "OBD") == 0 ||
                   1592:                                  stricmp(params[0], "BE") == 0 || stricmp(params[0], "RBE") == 0 || stricmp(params[0], "WBE") == 0 || stricmp(params[0], "IBE") == 0 || stricmp(params[0], "OBE") == 0) {
                   1593:                                break_point_t *break_point_ptr;
                   1594:                                GET_BREAK_POINT_PTR();
                   1595:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1596:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1597:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1598:                                                if(break_point_ptr->table[i].status != 0) {
                   1599:                                                        break_point_ptr->table[i].status = enabled ? 1 : -1;
                   1600:                                                }
                   1601:                                        }
                   1602:                                } else if(num >= 2) {
                   1603:                                        for(int i = 1; i < num; i++) {
                   1604:                                                int index = debugger_get_val(params[i]);
                   1605:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1606:                                                        telnet_printf("invalid index %x\n", index);
                   1607:                                                } else if(break_point_ptr->table[index - 1].status == 0) {
                   1608:                                                        telnet_printf("break point %x is null\n", index);
                   1609:                                                } else {
                   1610:                                                        break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
                   1611:                                                }
                   1612:                                        }
                   1613:                                } else {
                   1614:                                        telnet_printf("invalid parameter number\n");
                   1615:                                }
                   1616:                        } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
                   1617:                                break_point_t *break_point_ptr;
                   1618:                                GET_BREAK_POINT_PTR();
                   1619:                                if(num == 1) {
                   1620:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1621:                                                if(break_point_ptr->table[i].status) {
                   1622:                                                        telnet_printf("%d %c %04X:%04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].seg, break_point_ptr->table[i].ofs);
                   1623:                                                }
                   1624:                                        }
                   1625:                                } else {
                   1626:                                        telnet_printf("invalid parameter number\n");
                   1627:                                }
                   1628:                        } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
                   1629:                                break_point_t *break_point_ptr;
                   1630:                                GET_BREAK_POINT_PTR();
                   1631:                                if(num == 1) {
                   1632:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1633:                                                if(break_point_ptr->table[i].status) {
                   1634:                                                        telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
                   1635:                                                }
                   1636:                                        }
                   1637:                                } else {
                   1638:                                        telnet_printf("invalid parameter number\n");
                   1639:                                }
                   1640:                        } else if(stricmp(params[0], "INTBP") == 0) {
                   1641:                                if(num >= 2 && num <= 4) {
                   1642:                                        int int_num = debugger_get_val(params[1]);
                   1643:                                        UINT8 ah = 0, ah_registered = 0;
                   1644:                                        UINT8 al = 0, al_registered = 0;
                   1645:                                        if(num >= 3) {
                   1646:                                                ah = debugger_get_val(params[2]);
                   1647:                                                ah_registered = 1;
                   1648:                                        }
                   1649:                                        if(num == 4) {
                   1650:                                                al = debugger_get_val(params[3]);
                   1651:                                                al_registered = 1;
                   1652:                                        }
                   1653:                                        bool found = false;
                   1654:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1655:                                                if(int_break_point.table[i].status == 0 || (
                   1656:                                                   int_break_point.table[i].int_num == int_num &&
                   1657:                                                   int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
                   1658:                                                   int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
                   1659:                                                        int_break_point.table[i].int_num = int_num;
                   1660:                                                        int_break_point.table[i].ah = ah;
                   1661:                                                        int_break_point.table[i].ah_registered = ah_registered;
                   1662:                                                        int_break_point.table[i].al = al;
                   1663:                                                        int_break_point.table[i].al_registered = al_registered;
                   1664:                                                        int_break_point.table[i].status = 1;
                   1665:                                                        found = true;
                   1666:                                                }
                   1667:                                        }
                   1668:                                        if(!found) {
                   1669:                                                telnet_printf("too many break points\n");
                   1670:                                        }
                   1671:                                } else {
                   1672:                                        telnet_printf("invalid parameter number\n");
                   1673:                                }
                   1674:                        } else if(stricmp(params[0], "INTBC") == 0) {
                   1675:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1676:                                        memset(&int_break_point, 0, sizeof(int_break_point_t));
                   1677:                                } else if(num >= 2) {
                   1678:                                        for(int i = 1; i < num; i++) {
                   1679:                                                int index = debugger_get_val(params[i]);
                   1680:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1681:                                                        telnet_printf("invalid index %x\n", index);
                   1682:                                                } else {
                   1683:                                                        int_break_point.table[index - 1].int_num = 0;
                   1684:                                                        int_break_point.table[index - 1].ah = 0;
                   1685:                                                        int_break_point.table[index - 1].ah_registered = 0;
                   1686:                                                        int_break_point.table[index - 1].al = 0;
                   1687:                                                        int_break_point.table[index - 1].al_registered = 0;
                   1688:                                                        int_break_point.table[index - 1].status = 0;
                   1689:                                                }
                   1690:                                        }
                   1691:                                } else {
                   1692:                                        telnet_printf("invalid parameter number\n");
                   1693:                                }
                   1694:                        } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
                   1695:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1696:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1697:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1698:                                                if(int_break_point.table[i].status != 0) {
                   1699:                                                        int_break_point.table[i].status = enabled ? 1 : -1;
                   1700:                                                }
                   1701:                                        }
                   1702:                                } else if(num >= 2) {
                   1703:                                        for(int i = 1; i < num; i++) {
                   1704:                                                int index = debugger_get_val(params[i]);
                   1705:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1706:                                                        telnet_printf("invalid index %x\n", index);
                   1707:                                                } else if(int_break_point.table[index - 1].status == 0) {
                   1708:                                                        telnet_printf("break point %x is null\n", index);
                   1709:                                                } else {
                   1710:                                                        int_break_point.table[index - 1].status = enabled ? 1 : -1;
                   1711:                                                }
                   1712:                                        }
                   1713:                                } else {
                   1714:                                        telnet_printf("invalid parameter number\n");
                   1715:                                }
                   1716:                        } else if(stricmp(params[0], "INTBL") == 0) {
                   1717:                                if(num == 1) {
                   1718:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1719:                                                if(int_break_point.table[i].status) {
                   1720:                                                        telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
                   1721:                                                        if(int_break_point.table[i].ah_registered) {
                   1722:                                                                telnet_printf(" %02X", int_break_point.table[i].ah);
                   1723:                                                        }
                   1724:                                                        if(int_break_point.table[i].al_registered) {
                   1725:                                                                telnet_printf(" %02X", int_break_point.table[i].al);
                   1726:                                                        }
                   1727:                                                        telnet_printf("\n");
                   1728:                                                }
                   1729:                                        }
                   1730:                                } else {
                   1731:                                        telnet_printf("invalid parameter number\n");
                   1732:                                }
                   1733:                        } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
                   1734:                                if(num == 1 || num == 2) {
                   1735:                                        break_point_t break_point_stored;
                   1736:                                        bool break_points_stored = false;
                   1737:                                        
                   1738:                                        if(stricmp(params[0], "P") == 0) {
                   1739:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1740:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1741:                                                break_points_stored = true;
                   1742:                                                
                   1743:                                                break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
                   1744:                                                break_point.table[0].status = 1;
                   1745:                                        } else if(num >= 2) {
                   1746:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1747:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1748:                                                break_points_stored = true;
                   1749:                                                
                   1750:                                                UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1751:                                                UINT32 ofs = debugger_get_ofs(params[1]);
                   1752:                                                break_point.table[0].addr = (seg << 4) + ofs;
                   1753:                                                break_point.table[0].seg = seg;
                   1754:                                                break_point.table[0].ofs = ofs;
                   1755:                                                break_point.table[0].status = 1;
                   1756:                                        }
                   1757:                                        break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1758:                                        now_going = true;
                   1759:                                        now_suspended = false;
                   1760:                                        
                   1761:                                        telnet_command("\033[2l"); // key unlock
                   1762:                                        while(!m_halted && !now_suspended) {
                   1763:                                                if(telnet_kbhit()) {
                   1764:                                                        break;
                   1765:                                                }
                   1766:                                                Sleep(10);
                   1767:                                        }
                   1768:                                        now_going = false;
                   1769:                                        telnet_command("\033[2h"); // key lock
                   1770:                                        
                   1771:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1772:                                                Sleep(100);
                   1773:                                                if(!m_halted && !now_suspended) {
                   1774:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1775:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1776:                                                }
                   1777:                                        }
                   1778:                                        while(!m_halted && !now_suspended) {
                   1779:                                                if(telnet_disconnected()) {
                   1780:                                                        break;
                   1781:                                                }
                   1782:                                                Sleep(10);
                   1783:                                        }
                   1784:                                        dasm_seg = SREG(CS);
                   1785:                                        dasm_ofs = m_eip;
                   1786:                                        
                   1787:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1788:                                        debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1789:                                        telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1790:                                        
                   1791:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1792:                                        debugger_regs_info(buffer);
                   1793:                                        telnet_printf("%s", buffer);
                   1794:                                        
                   1795:                                        if(break_point.hit) {
                   1796:                                                if(stricmp(params[0], "G") == 0 && num == 1) {
                   1797:                                                        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1798:                                                        telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1799:                                                }
                   1800:                                        } else if(rd_break_point.hit) {
                   1801:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1802:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1803:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1804:                                                m_prev_cs, m_prev_eip);
                   1805:                                        } else if(wr_break_point.hit) {
                   1806:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1807:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1808:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1809:                                                m_prev_cs, m_prev_eip);
                   1810:                                        } else if(in_break_point.hit) {
                   1811:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1812:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1813:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1814:                                                m_prev_cs, m_prev_eip);
                   1815:                                        } else if(out_break_point.hit) {
                   1816:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1817:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1818:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1819:                                                m_prev_cs, m_prev_eip);
                   1820:                                        } else if(int_break_point.hit) {
                   1821:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1822:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1823:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1824:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1825:                                                }
                   1826:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1827:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1828:                                                }
                   1829:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1830:                                        } else {
                   1831:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1832:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1833:                                        }
                   1834:                                        if(break_points_stored) {
                   1835:                                                memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
                   1836:                                        }
                   1837:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1838:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1839:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1840:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1841:                                } else {
                   1842:                                        telnet_printf("invalid parameter number\n");
                   1843:                                }
                   1844:                        } else if(stricmp(params[0], "T") == 0) {
                   1845:                                if(num == 1 || num == 2) {
                   1846:                                        int steps = 1;
                   1847:                                        if(num >= 2) {
                   1848:                                                steps = debugger_get_val(params[1]);
                   1849:                                        }
                   1850:                                        
                   1851:                                        telnet_command("\033[2l"); // key unlock
                   1852:                                        while(steps-- > 0) {
                   1853:                                                break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1854:                                                now_going = false;
                   1855:                                                now_suspended = false;
                   1856:                                                
                   1857:                                                while(!m_halted && !now_suspended) {
                   1858:                                                        if(telnet_disconnected()) {
                   1859:                                                                break;
                   1860:                                                        }
                   1861:                                                        Sleep(10);
                   1862:                                                }
                   1863:                                                dasm_seg = SREG(CS);
                   1864:                                                dasm_ofs = m_eip;
                   1865:                                                
                   1866:                                                telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1867:                                                debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1868:                                                telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1869:                                                
                   1870:                                                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1871:                                                debugger_regs_info(buffer);
                   1872:                                                telnet_printf("%s", buffer);
                   1873:                                                
                   1874:                                                if(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit || telnet_kbhit()) {
                   1875:                                                        break;
                   1876:                                                }
                   1877:                                        }
                   1878:                                        telnet_command("\033[2h"); // key lock
                   1879:                                        
                   1880:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1881:                                                Sleep(100);
                   1882:                                                if(!m_halted && !now_suspended) {
                   1883:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1884:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1885:                                                }
                   1886:                                        }
                   1887:                                        while(!m_halted && !now_suspended) {
                   1888:                                                if(telnet_disconnected()) {
                   1889:                                                        break;
                   1890:                                                }
                   1891:                                                Sleep(10);
                   1892:                                        }
                   1893:                                        if(break_point.hit) {
                   1894:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1895:                                                telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1896:                                        } else if(rd_break_point.hit) {
                   1897:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1898:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1899:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1900:                                                m_prev_cs, m_prev_eip);
                   1901:                                        } else if(wr_break_point.hit) {
                   1902:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1903:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1904:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1905:                                                m_prev_cs, m_prev_eip);
                   1906:                                        } else if(in_break_point.hit) {
                   1907:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1908:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1909:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1910:                                                m_prev_cs, m_prev_eip);
                   1911:                                        } else if(out_break_point.hit) {
                   1912:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1913:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1914:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1915:                                                m_prev_cs, m_prev_eip);
                   1916:                                        } else if(int_break_point.hit) {
                   1917:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1918:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1919:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1920:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1921:                                                }
                   1922:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1923:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1924:                                                }
                   1925:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1926:                                        } else if(steps > 0) {
                   1927:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1928:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1929:                                        }
                   1930:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1931:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1932:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1933:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1934:                                } else {
                   1935:                                        telnet_printf("invalid parameter number\n");
                   1936:                                }
                   1937:                        } else if(stricmp(params[0], "Q") == 0) {
                   1938:                                break;
                   1939:                        } else if(stricmp(params[0], "X") == 0) {
                   1940:                                debugger_process_info(buffer);
                   1941:                                telnet_printf("%s", buffer);
                   1942:                        } else if(stricmp(params[0], ">") == 0) {
                   1943:                                if(num == 2) {
                   1944:                                        if(fp_debugger != NULL) {
                   1945:                                                fclose(fp_debugger);
                   1946:                                                fp_debugger = NULL;
                   1947:                                        }
                   1948:                                        fp_debugger = fopen(params[1], "w");
                   1949:                                } else {
                   1950:                                        telnet_printf("invalid parameter number\n");
                   1951:                                }
                   1952:                        } else if(stricmp(params[0], "<") == 0) {
                   1953:                                if(num == 2) {
                   1954:                                        if(fi_debugger != NULL) {
                   1955:                                                fclose(fi_debugger);
                   1956:                                                fi_debugger = NULL;
                   1957:                                        }
                   1958:                                        fi_debugger = fopen(params[1], "r");
                   1959:                                } else {
                   1960:                                        telnet_printf("invalid parameter number\n");
                   1961:                                }
                   1962:                        } else if(stricmp(params[0], "?") == 0) {
                   1963:                                telnet_printf("D [<start> [<end>]] - dump memory\n");
                   1964:                                telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
                   1965:                                telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
                   1966:                                telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
                   1967:                                telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
                   1968:                                
                   1969:                                telnet_printf("R - show registers\n");
                   1970:                                telnet_printf("R <reg> <value> - edit register\n");
                   1971:                                telnet_printf("S <start> <end> <list> - search\n");
                   1972:                                telnet_printf("U [<start> [<end>]] - unassemble\n");
                   1973:                                
                   1974:                                telnet_printf("H <value> <value> - hexadd\n");
                   1975:                                
                   1976:                                telnet_printf("BP <address> - set breakpoint\n");
                   1977:                                telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
                   1978:                                telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
                   1979:                                telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
                   1980:                                telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
                   1981:                                telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
                   1982:                                
                   1983:                                telnet_printf("G - go (press enter key to break)\n");
                   1984:                                telnet_printf("G <address> - go and break at address\n");
                   1985:                                telnet_printf("P - trace one opcode (step over)\n");
                   1986:                                telnet_printf("T [<count>] - trace (step in)\n");
                   1987:                                telnet_printf("Q - quit\n");
                   1988:                                telnet_printf("X - show dos process info\n");
                   1989:                                
                   1990:                                telnet_printf("> <filename> - output logfile\n");
                   1991:                                telnet_printf("< <filename> - input commands from file\n");
                   1992:                                
                   1993:                                telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
                   1994:                                telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
                   1995:                        } else {
                   1996:                                telnet_printf("unknown command %s\n", params[0]);
                   1997:                        }
                   1998:                }
                   1999:        }
                   2000:        if(fp_debugger != NULL) {
                   2001:                fclose(fp_debugger);
                   2002:                fp_debugger = NULL;
                   2003:        }
                   2004:        if(fi_debugger != NULL) {
                   2005:                fclose(fi_debugger);
                   2006:                fi_debugger = NULL;
                   2007:        }
                   2008:        now_debugging = now_going = now_suspended = force_suspend = false;
                   2009:        closesocket(cli_socket);
                   2010: }
                   2011: 
                   2012: const char *debugger_get_ttermpro_path()
                   2013: {
                   2014:        static char path[MAX_PATH] = {0};
                   2015:        
                   2016:        if(getenv("ProgramFiles")) {
                   2017:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
                   2018:        }
                   2019:        return(path);
                   2020: }
                   2021: 
                   2022: const char *debugger_get_ttermpro_x86_path()
                   2023: {
                   2024:        static char path[MAX_PATH] = {0};
                   2025:        
                   2026:        if(getenv("ProgramFiles(x86)")) {
                   2027:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
                   2028:        }
                   2029:        return(path);
                   2030: }
                   2031: 
                   2032: const char *debugger_get_putty_path()
                   2033: {
                   2034:        static char path[MAX_PATH] = {0};
                   2035:        
                   2036:        if(getenv("ProgramFiles")) {
                   2037:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
                   2038:        }
                   2039:        return(path);
                   2040: }
                   2041: 
                   2042: const char *debugger_get_putty_x86_path()
                   2043: {
                   2044:        static char path[MAX_PATH] = {0};
                   2045:        
                   2046:        if(getenv("ProgramFiles(x86)")) {
                   2047:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
                   2048:        }
                   2049:        return(path);
                   2050: }
                   2051: 
                   2052: const char *debugger_get_telnet_path()
                   2053: {
                   2054:        // NOTE: When you run 32bit version of msdos.exe on Windows x64,
                   2055:        // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
                   2056:        // But 32bit version of telnet.exe will not be installed in SysWOW64
                   2057:        // and 64bit version of telnet.exe will be installed in System32.
                   2058:        static char path[MAX_PATH] = {0};
                   2059:        
                   2060:        if(getenv("windir") != NULL) {
                   2061:                sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
                   2062:        }
                   2063:        return(path);
                   2064: }
                   2065: 
                   2066: DWORD WINAPI debugger_thread(LPVOID)
                   2067: {
                   2068:        WSADATA was_data;
                   2069:        struct sockaddr_in svr_addr;
                   2070:        struct sockaddr_in cli_addr;
                   2071:        int cli_addr_len = sizeof(cli_addr);
                   2072:        int port = 23;
                   2073:        int bind_stat = SOCKET_ERROR;
                   2074:        struct timeval timeout;
                   2075:        
                   2076:        WSAStartup(MAKEWORD(2,0), &was_data);
                   2077:        
                   2078:        if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
                   2079:                memset(&svr_addr, 0, sizeof(svr_addr));
                   2080:                svr_addr.sin_family = AF_INET;
                   2081:                svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
                   2082:                
                   2083:                while(!m_halted && port < 10000) {
                   2084:                        svr_addr.sin_port = htons(port);
                   2085:                        if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
                   2086:                                break;
                   2087:                        } else {
                   2088:                                port = (port == 23) ? 9000 : (port + 1);
                   2089:                        }
                   2090:                }
                   2091:                if(bind_stat == 0) {
                   2092:                        timeout.tv_sec = 1;
                   2093:                        timeout.tv_usec = 0;
1.1.1.45  root     2094:                        setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
1.1.1.33  root     2095:                        
                   2096:                        listen(svr_socket, 1);
                   2097:                        
                   2098:                        char command[MAX_PATH] = {0};
                   2099:                        STARTUPINFO si;
                   2100:                        PROCESS_INFORMATION pi;
                   2101:                        
                   2102:                        if(_access(debugger_get_ttermpro_path(), 0) == 0) {
                   2103:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
                   2104:                        } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
                   2105:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
                   2106:                        } else if(_access(debugger_get_putty_path(), 0) == 0) {
                   2107:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
                   2108:                        } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
                   2109:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
                   2110:                        } else if(_access(debugger_get_telnet_path(), 0) == 0) {
                   2111:                                sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
                   2112:                        }
                   2113:                        if(command[0] != '\0') {
                   2114:                                memset(&si, 0, sizeof(STARTUPINFO));
                   2115:                                memset(&pi, 0, sizeof(PROCESS_INFORMATION));
                   2116:                                CreateProcess(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
                   2117:                        }
                   2118:                        
                   2119:                        while(!m_halted) {
                   2120:                                if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
                   2121:                                        u_long val = 1;
                   2122:                                        ioctlsocket(cli_socket, FIONBIO, &val);
                   2123:                                        debugger_main();
                   2124:                                }
                   2125:                        }
                   2126:                }
                   2127:        }
                   2128:        WSACleanup();
                   2129:        return(0);
                   2130: }
                   2131: #endif
                   2132: 
                   2133: /* ----------------------------------------------------------------------------
1.1       root     2134:        main
                   2135: ---------------------------------------------------------------------------- */
                   2136: 
1.1.1.28  root     2137: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                   2138: {
                   2139:        if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33  root     2140:                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     2141: #ifdef USE_SERVICE_THREAD
                   2142:                        EnterCriticalSection(&key_buf_crit_sect);
                   2143: #endif
1.1.1.33  root     2144:                        key_buf_char->clear();
                   2145:                        key_buf_scan->clear();
1.1.1.35  root     2146: #ifdef USE_SERVICE_THREAD
                   2147:                        LeaveCriticalSection(&key_buf_crit_sect);
                   2148: #endif
1.1.1.33  root     2149:                }
                   2150: //             key_code = key_recv = 0;
1.1.1.28  root     2151:                return TRUE;
                   2152:        } else if(dwCtrlType == CTRL_C_EVENT) {
                   2153:                return TRUE;
                   2154:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
                   2155:                // this program will be terminated abnormally, do minimum end process
                   2156:                exit_handler();
                   2157:                exit(1);
                   2158:        }
                   2159:        return FALSE;
                   2160: }
                   2161: 
                   2162: void exit_handler()
                   2163: {
                   2164:        if(temp_file_created) {
                   2165:                DeleteFile(temp_file_path);
                   2166:                temp_file_created = false;
                   2167:        }
                   2168:        if(key_buf_char != NULL) {
                   2169:                key_buf_char->release();
                   2170:                delete key_buf_char;
                   2171:                key_buf_char = NULL;
                   2172:        }
                   2173:        if(key_buf_scan != NULL) {
                   2174:                key_buf_scan->release();
                   2175:                delete key_buf_scan;
                   2176:                key_buf_scan = NULL;
                   2177:        }
1.1.1.32  root     2178: #ifdef SUPPORT_XMS
                   2179:        msdos_xms_release();
                   2180: #endif
1.1.1.28  root     2181:        hardware_release();
                   2182: }
                   2183: 
1.1.1.35  root     2184: #ifdef USE_VRAM_THREAD
1.1.1.28  root     2185: DWORD WINAPI vram_thread(LPVOID)
                   2186: {
                   2187:        while(!m_halted) {
                   2188:                EnterCriticalSection(&vram_crit_sect);
                   2189:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                   2190:                        vram_flush_char();
                   2191:                }
                   2192:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                   2193:                        vram_flush_attr();
                   2194:                }
                   2195:                vram_last_length_char = vram_length_char;
                   2196:                vram_last_length_attr = vram_length_attr;
                   2197:                LeaveCriticalSection(&vram_crit_sect);
                   2198:                // this is about half the maximum keyboard repeat rate - any
                   2199:                // lower tends to be jerky, any higher misses updates
                   2200:                Sleep(15);
                   2201:        }
                   2202:        return 0;
                   2203: }
                   2204: #endif
                   2205: 
1.1.1.45  root     2206: long get_section_in_exec_file(FILE *fp, const char *name)
1.1.1.28  root     2207: {
                   2208:        UINT8 header[0x400];
                   2209:        
                   2210:        long position = ftell(fp);
                   2211:        fseek(fp, 0, SEEK_SET);
                   2212:        fread(header, sizeof(header), 1, fp);
                   2213:        fseek(fp, position, SEEK_SET);
                   2214:        
                   2215:        try {
                   2216:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2217:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2218:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2219:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2220:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2221:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2222:                
                   2223:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                   2224:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                   2225:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                   2226:                                return(sectionHeader->PointerToRawData);
                   2227:                        }
                   2228:                }
                   2229:        } catch(...) {
                   2230:        }
                   2231:        return(0);
                   2232: }
                   2233: 
1.1.1.10  root     2234: bool is_started_from_command_prompt()
                   2235: {
1.1.1.18  root     2236:        bool ret = false;
                   2237:        
                   2238:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                   2239:        if(hLibrary) {
                   2240:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                   2241:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                   2242:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
                   2243:                if(lpfnGetConsoleProcessList) {
                   2244:                        DWORD pl;
                   2245:                        ret = (lpfnGetConsoleProcessList(&pl, 1) > 1);
                   2246:                        FreeLibrary(hLibrary);
                   2247:                        return(ret);
                   2248:                }
                   2249:                FreeLibrary(hLibrary);
                   2250:        }
                   2251:        
                   2252:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   2253:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                   2254:                DWORD dwParentProcessID = 0;
                   2255:                PROCESSENTRY32 pe32;
                   2256:                pe32.dwSize = sizeof(PROCESSENTRY32);
                   2257:                if(Process32First(hSnapshot, &pe32)) {
                   2258:                        do {
                   2259:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                   2260:                                        dwParentProcessID = pe32.th32ParentProcessID;
                   2261:                                        break;
                   2262:                                }
                   2263:                        } while(Process32Next(hSnapshot, &pe32));
                   2264:                }
                   2265:                CloseHandle(hSnapshot);
                   2266:                if(dwParentProcessID != 0) {
                   2267:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                   2268:                        if(hProcess != NULL) {
                   2269:                                HMODULE hMod;
                   2270:                                DWORD cbNeeded;
                   2271:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                   2272:                                        char module_name[MAX_PATH];
                   2273:                                        if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
                   2274:                                                ret = (_strnicmp(module_name, "cmd.exe", 7) == 0);
                   2275:                                        }
                   2276:                                }
                   2277:                                CloseHandle(hProcess);
                   2278:                        }
                   2279:                }
                   2280:        }
                   2281:        return(ret);
1.1.1.14  root     2282: }
                   2283: 
                   2284: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                   2285: {
1.1.1.24  root     2286:        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
1.1.1.14  root     2287:        OSVERSIONINFOEX osvi;
                   2288:        DWORDLONG dwlConditionMask = 0;
                   2289:        int op = VER_GREATER_EQUAL;
                   2290:        
                   2291:        // Initialize the OSVERSIONINFOEX structure.
                   2292:        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
                   2293:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
                   2294:        osvi.dwMajorVersion = dwMajorVersion;
                   2295:        osvi.dwMinorVersion = dwMinorVersion;
                   2296:        osvi.wServicePackMajor = wServicePackMajor;
                   2297:        osvi.wServicePackMinor = wServicePackMinor;
                   2298:        
                   2299:         // Initialize the condition mask.
                   2300:        VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                   2301:        VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                   2302:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                   2303:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                   2304:        
                   2305:        // Perform the test.
                   2306:        return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                   2307: }
                   2308: 
1.1.1.27  root     2309: void get_sio_port_numbers()
                   2310: {
                   2311:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                   2312:        HDEVINFO hDevInfo = 0;
                   2313:        HKEY hKey = 0;
                   2314:        if((hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
                   2315:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                   2316:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                   2317:                                char chData[256];
                   2318:                                DWORD dwType = 0;
                   2319:                                DWORD dwSize = sizeof(chData);
                   2320:                                int port_number = 0;
                   2321:                                
                   2322:                                if(RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
                   2323:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                   2324:                                                port_number = atoi(chData + 3);
                   2325:                                        }
                   2326:                                }
                   2327:                                RegCloseKey(hKey);
                   2328:                                
1.1.1.29  root     2329:                                if(sio_port_number[0] == port_number || sio_port_number[1] == port_number || sio_port_number[2] == port_number || sio_port_number[3] == port_number) {
1.1.1.27  root     2330:                                        continue;
                   2331:                                }
                   2332:                                if(sio_port_number[0] == 0) {
                   2333:                                        sio_port_number[0] = port_number;
                   2334:                                } else if(sio_port_number[1] == 0) {
                   2335:                                        sio_port_number[1] = port_number;
1.1.1.29  root     2336:                                } else if(sio_port_number[2] == 0) {
                   2337:                                        sio_port_number[2] = port_number;
                   2338:                                } else if(sio_port_number[3] == 0) {
                   2339:                                        sio_port_number[3] = port_number;
1.1.1.27  root     2340:                                }
1.1.1.29  root     2341:                                if(sio_port_number[0] != 0 && sio_port_number[1] != 0 && sio_port_number[2] != 0 && sio_port_number[3] != 0) {
1.1.1.27  root     2342:                                        break;
                   2343:                                }
                   2344:                        }
                   2345:                }
                   2346:        }
                   2347: }
                   2348: 
1.1.1.28  root     2349: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                   2350: 
1.1       root     2351: int main(int argc, char *argv[], char *envp[])
                   2352: {
1.1.1.9   root     2353:        int arg_offset = 0;
                   2354:        int standard_env = 0;
1.1.1.14  root     2355:        int buf_width = 0, buf_height = 0;
1.1.1.28  root     2356:        bool get_console_info_success = false;
                   2357:        bool screen_size_changed = false;
                   2358:        
                   2359:        _TCHAR path[MAX_PATH], full[MAX_PATH], *name = NULL;
                   2360:        GetModuleFileName(NULL, path, MAX_PATH);
                   2361:        GetFullPathName(path, MAX_PATH, full, &name);
1.1       root     2362:        
1.1.1.27  root     2363:        char dummy_argv_0[] = "msdos.exe";
                   2364:        char dummy_argv_1[MAX_PATH];
                   2365:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                   2366:        char new_exec_file[MAX_PATH];
                   2367:        bool convert_cmd_file = false;
1.1.1.28  root     2368:        unsigned int code_page = 0;
1.1.1.27  root     2369:        
                   2370:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root     2371:                // check if command file is embedded to this execution file
                   2372:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root     2373:                FILE* fp = fopen(full, "rb");
1.1.1.28  root     2374:                long offset = get_section_in_exec_file(fp, ".msdos");
                   2375:                if(offset != 0) {
1.1.1.30  root     2376:                        UINT8 buffer[16];
1.1.1.28  root     2377:                        fseek(fp, offset, SEEK_SET);
                   2378:                        fread(buffer, sizeof(buffer), 1, fp);
                   2379:                        
                   2380:                        // restore flags
                   2381:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                   2382:                        no_windows          = ((buffer[0] & 0x02) != 0);
                   2383:                        standard_env        = ((buffer[0] & 0x04) != 0);
                   2384:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                   2385:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                   2386:                        if((buffer[0] & 0x20) != 0) {
                   2387:                                get_sio_port_numbers();
                   2388:                        }
                   2389:                        if((buffer[0] & 0x40) != 0) {
                   2390:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                   2391:                                support_ems = true;
1.1.1.30  root     2392:                        }
1.1.1.27  root     2393: #ifdef SUPPORT_XMS
1.1.1.30  root     2394:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root     2395:                                support_xms = true;
                   2396:                        }
1.1.1.30  root     2397: #endif
1.1.1.28  root     2398:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                   2399:                                buf_width  = buffer[1] | (buffer[2] << 8);
                   2400:                                buf_height = buffer[3] | (buffer[4] << 8);
                   2401:                        }
                   2402:                        if(buffer[5] != 0) {
1.1.1.30  root     2403:                                dos_major_version = buffer[5];
                   2404:                                dos_minor_version = buffer[6];
                   2405:                        }
                   2406:                        if(buffer[7] != 0) {
                   2407:                                win_major_version = buffer[7];
                   2408:                                win_minor_version = buffer[8];
1.1.1.28  root     2409:                        }
1.1.1.30  root     2410:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root     2411:                                SetConsoleCP(code_page);
                   2412:                                SetConsoleOutputCP(code_page);
                   2413:                        }
1.1.1.30  root     2414:                        int name_len = buffer[11];
                   2415:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root     2416:                        
                   2417:                        // restore command file name
                   2418:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                   2419:                        fread(dummy_argv_1, name_len, 1, fp);
                   2420:                        
                   2421:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                   2422:                                // if original command file exists, create a temporary file name
                   2423:                                if(GetTempFileName(_T("."), _T("DOS"), 0, dummy_argv_1) != 0) {
                   2424:                                        // create a temporary command file in the current director
                   2425:                                        DeleteFile(dummy_argv_1);
1.1.1.27  root     2426:                                } else {
1.1.1.28  root     2427:                                        // create a temporary command file in the temporary folder
                   2428:                                        GetTempPath(MAX_PATH, path);
                   2429:                                        if(GetTempFileName(path, _T("DOS"), 0, dummy_argv_1) != 0) {
                   2430:                                                DeleteFile(dummy_argv_1);
                   2431:                                        } else {
                   2432:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                   2433:                                        }
1.1.1.27  root     2434:                                }
1.1.1.28  root     2435:                                // check the command file type
                   2436:                                fread(buffer, 2, 1, fp);
                   2437:                                fseek(fp, -2, SEEK_CUR);
                   2438:                                if(memcmp(buffer, "MZ", 2) != 0) {
                   2439:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                   2440:                                } else {
                   2441:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root     2442:                                }
                   2443:                        }
1.1.1.28  root     2444:                        
                   2445:                        // restore command file
                   2446:                        FILE* fo = fopen(dummy_argv_1, "wb");
                   2447:                        for(int i = 0; i < file_len; i++) {
                   2448:                                fputc(fgetc(fp), fo);
                   2449:                        }
                   2450:                        fclose(fo);
                   2451:                        
                   2452:                        GetFullPathName(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
                   2453:                        temp_file_created = true;
                   2454:                        SetFileAttributes(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
                   2455:                        
                   2456:                        // adjust argc/argv
                   2457:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                   2458:                                dummy_argv[i + 1] = argv[i];
                   2459:                        }
                   2460:                        argc++;
                   2461:                        argv = dummy_argv;
1.1.1.27  root     2462:                }
                   2463:                fclose(fp);
                   2464:        }
1.1.1.9   root     2465:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     2466:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   2467:                        stay_busy = true;
                   2468:                        arg_offset++;
1.1.1.27  root     2469:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   2470:                        if(argv[i][2] != '\0') {
                   2471:                                strcpy(new_exec_file, &argv[i][2]);
                   2472:                        } else {
                   2473:                                strcpy(new_exec_file, "new_exec_file.exe");
                   2474:                        }
                   2475:                        convert_cmd_file = true;
                   2476:                        arg_offset++;
1.1.1.28  root     2477:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   2478:                        if(IS_NUMERIC(argv[i][2])) {
                   2479:                                code_page = atoi(&argv[i][2]);
                   2480:                        } else {
                   2481:                                code_page = GetConsoleCP();
                   2482:                        }
                   2483:                        arg_offset++;
1.1.1.25  root     2484:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   2485:                        no_windows = true;
                   2486:                        arg_offset++;
                   2487:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     2488:                        standard_env = 1;
                   2489:                        arg_offset++;
1.1.1.14  root     2490:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   2491:                        ignore_illegal_insn = true;
                   2492:                        arg_offset++;
                   2493:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   2494:                        limit_max_memory = true;
                   2495:                        arg_offset++;
                   2496:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.17  root     2497:                        if(sscanf(argv[1] + 2, "%d,%d", &buf_height, &buf_width) != 2) {
                   2498:                                buf_width = buf_height = 0;
                   2499:                        }
                   2500:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   2501:                                buf_width = 80;
                   2502:                        }
                   2503:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   2504:                                buf_height = 25;
                   2505:                        }
1.1.1.14  root     2506:                        arg_offset++;
1.1.1.25  root     2507:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     2508:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     2509:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   2510:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   2511:                                        sio_port_number[1] = atoi(p1 + 1);
                   2512:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   2513:                                                sio_port_number[2] = atoi(p2 + 1);
                   2514:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   2515:                                                        sio_port_number[3] = atoi(p3 + 1);
                   2516:                                                }
                   2517:                                        }
1.1.1.25  root     2518:                                }
1.1.1.29  root     2519:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     2520:                        }
1.1.1.29  root     2521:                        if(sio_port_number[0] == 0 || sio_port_number[1] == 0 || sio_port_number[2] == 0 || sio_port_number[3] == 0) {
1.1.1.27  root     2522:                                get_sio_port_numbers();
1.1.1.25  root     2523:                        }
                   2524:                        arg_offset++;
1.1.1.9   root     2525:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     2526:                        if(strlen(argv[i]) >= 5 && IS_NUMERIC(argv[i][2]) && argv[i][3] == '.' && IS_NUMERIC(argv[i][4]) && (argv[i][5] == '\0' || IS_NUMERIC(argv[i][5]))) {
1.1.1.30  root     2527:                                dos_major_version = argv[i][2] - '0';
                   2528:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   2529:                        }
                   2530:                        arg_offset++;
                   2531:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   2532:                        if(strlen(argv[i]) >= 5 && IS_NUMERIC(argv[i][2]) && argv[i][3] == '.' && IS_NUMERIC(argv[i][4]) && (argv[i][5] == '\0' || IS_NUMERIC(argv[i][5]))) {
                   2533:                                win_major_version = argv[i][2] - '0';
                   2534:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     2535:                        }
                   2536:                        arg_offset++;
1.1.1.25  root     2537:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   2538:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   2539:                        support_ems = true;
                   2540: #ifdef SUPPORT_XMS
                   2541:                        support_xms = true;
                   2542: #endif
                   2543:                        arg_offset++;
1.1.1.9   root     2544:                } else {
                   2545:                        break;
                   2546:                }
                   2547:        }
                   2548:        if(argc < 2 + arg_offset) {
1.1       root     2549: #ifdef _WIN64
1.1.1.14  root     2550:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     2551: #else
1.1.1.14  root     2552:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     2553: #endif
1.1.1.25  root     2554:                fprintf(stderr,
1.1.1.28  root     2555:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     2556:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     2557:                        "\n"
                   2558:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     2559: #ifdef _WIN64
1.1.1.27  root     2560:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     2561: #else
1.1.1.27  root     2562:                        "\t-c\tconvert command file to 32bit execution file\n"
                   2563: #endif
1.1.1.28  root     2564:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     2565:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   2566:                        "\t-e\tuse a reduced environment block\n"
                   2567:                        "\t-i\tignore invalid instructions\n"
                   2568:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   2569:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   2570:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   2571:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     2572:                        "\t-w\tset the Windows version\n"
1.1.1.19  root     2573: #ifdef SUPPORT_XMS
1.1.1.28  root     2574:                        "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19  root     2575: #else
1.1.1.28  root     2576:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2577: #endif
                   2578:                );
1.1.1.10  root     2579:                
                   2580:                if(!is_started_from_command_prompt()) {
                   2581:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2582:                        while(!_kbhit()) {
                   2583:                                Sleep(10);
                   2584:                        }
                   2585:                }
1.1.1.20  root     2586: #ifdef _DEBUG
                   2587:                _CrtDumpMemoryLeaks();
                   2588: #endif
1.1       root     2589:                return(EXIT_FAILURE);
                   2590:        }
1.1.1.27  root     2591:        if(convert_cmd_file) {
                   2592:                retval = EXIT_FAILURE;
1.1.1.28  root     2593:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2594:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2595:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2596:                        
1.1.1.28  root     2597:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2598:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2599:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2600:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2601:                        } else {
1.1.1.28  root     2602:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2603:                                if(offset != 0) {
                   2604:                                        UINT8 buffer[14];
                   2605:                                        fseek(fp, offset, SEEK_SET);
                   2606:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2607:                                        memset(path, 0, sizeof(path));
                   2608:                                        fread(path, buffer[9], 1, fp);
                   2609:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2610:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2611:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2612:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2613:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2614:                                } else {
                   2615:                                        // read pe header of msdos.exe
                   2616:                                        UINT8 header[0x400];
                   2617:                                        fseek(fp, 0, SEEK_SET);
                   2618:                                        fread(header, sizeof(header), 1, fp);
                   2619:                                        
                   2620:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2621:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2622:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2623:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2624:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2625:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2626:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2627:                                        
                   2628:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2629:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2630:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2631:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2632:                                        if(dwExtraLastSectionBytes != 0) {
                   2633:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2634:                                                dwLastSectionSize += dwRemain;
                   2635:                                        }
                   2636:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2637:                                        
                   2638:                                        // store msdos.exe
                   2639:                                        fseek(fp, 0, SEEK_SET);
                   2640:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2641:                                                if((data = fgetc(fp)) != EOF) {
                   2642:                                                        fputc(data, fo);
                   2643:                                                } else {
                   2644:                                                        // we should not reach here :-(
                   2645:                                                        fputc(0, fo);
                   2646:                                                }
                   2647:                                        }
                   2648:                                        
                   2649:                                        // store options
                   2650:                                        UINT8 flags = 0;
                   2651:                                        if(stay_busy) {
                   2652:                                                flags |= 0x01;
                   2653:                                        }
                   2654:                                        if(no_windows) {
                   2655:                                                flags |= 0x02;
                   2656:                                        }
                   2657:                                        if(standard_env) {
                   2658:                                                flags |= 0x04;
                   2659:                                        }
                   2660:                                        if(ignore_illegal_insn) {
                   2661:                                                flags |= 0x08;
                   2662:                                        }
                   2663:                                        if(limit_max_memory) {
                   2664:                                                flags |= 0x10;
                   2665:                                        }
1.1.1.29  root     2666:                                        if(sio_port_number[0] != 0 || sio_port_number[1] != 0 || sio_port_number[2] != 0 || sio_port_number[3] != 0) {
1.1.1.28  root     2667:                                                flags |= 0x20;
                   2668:                                        }
                   2669:                                        if(support_ems) {
                   2670:                                                flags |= 0x40;
                   2671:                                        }
1.1.1.30  root     2672: #ifdef SUPPORT_XMS
                   2673:                                        if(support_xms) {
                   2674:                                                flags |= 0x80;
                   2675:                                        }
                   2676: #endif
1.1.1.28  root     2677:                                        
                   2678:                                        fputc(flags, fo);
                   2679:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2680:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2681:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2682:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2683:                                        fputc(dos_major_version, fo);
                   2684:                                        fputc(dos_minor_version, fo);
                   2685:                                        fputc(win_major_version, fo);
                   2686:                                        fputc(win_minor_version, fo);
1.1.1.28  root     2687:                                        fputc((code_page >> 0) & 0xff, fo);
                   2688:                                        fputc((code_page >> 8) & 0xff, fo);
                   2689:                                        
                   2690:                                        // store command file info
                   2691:                                        GetFullPathName(argv[arg_offset + 1], MAX_PATH, full, &name);
                   2692:                                        int name_len = strlen(name);
                   2693:                                        fseek(fs, 0, SEEK_END);
                   2694:                                        long file_size = ftell(fs);
                   2695:                                        
                   2696:                                        fputc(name_len, fo);
                   2697:                                        fputc((file_size >>  0) & 0xff, fo);
                   2698:                                        fputc((file_size >>  8) & 0xff, fo);
                   2699:                                        fputc((file_size >> 16) & 0xff, fo);
                   2700:                                        fputc((file_size >> 24) & 0xff, fo);
                   2701:                                        fwrite(name, name_len, 1, fo);
                   2702:                                        
                   2703:                                        // store command file
                   2704:                                        fseek(fs, 0, SEEK_SET);
                   2705:                                        for(int i = 0; i < file_size; i++) {
                   2706:                                                if((data = fgetc(fs)) != EOF) {
                   2707:                                                        fputc(data, fo);
                   2708:                                                } else {
                   2709:                                                        // we should not reach here :-(
                   2710:                                                        fputc(0, fo);
                   2711:                                                }
                   2712:                                        }
                   2713:                                        
                   2714:                                        // store padding data and update pe header
1.1.1.29  root     2715:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   2716:                                        coffHeader->NumberOfSections++;
                   2717:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   2718:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   2719:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   2720:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   2721:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     2722:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   2723:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   2724:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     2725:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     2726:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   2727:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     2728:                                                        if(i < 2) {
                   2729:                                                                fputc(padding[i & 15], fo);
                   2730:                                                        } else {
                   2731:                                                                fputc(padding[(i - 2) & 15], fo);
                   2732:                                                        }
1.1.1.28  root     2733:                                                }
                   2734:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   2735:                                        }
                   2736:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   2737:                                        
                   2738:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   2739:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   2740:                                        if(dwExtraNewSectionBytes != 0) {
                   2741:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   2742:                                                dwNewSectionSize += dwRemain;
                   2743:                                        }
                   2744:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   2745:                                        
                   2746:                                        fseek(fo, 0, SEEK_SET);
                   2747:                                        fwrite(header, sizeof(header), 1, fo);
                   2748:                                        
                   2749:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   2750:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     2751:                                }
                   2752:                        }
                   2753:                        if(fp != NULL) {
                   2754:                                fclose(fp);
                   2755:                        }
                   2756:                        if(fs != NULL) {
                   2757:                                fclose(fs);
                   2758:                        }
                   2759:                        if(fo != NULL) {
                   2760:                                fclose(fo);
                   2761:                        }
                   2762:                }
                   2763: #ifdef _DEBUG
                   2764:                _CrtDumpMemoryLeaks();
                   2765: #endif
                   2766:                return(retval);
                   2767:        }
1.1       root     2768:        
1.1.1.14  root     2769:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   2770:        
1.1.1.23  root     2771:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     2772:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     2773:        CONSOLE_CURSOR_INFO ci;
1.1.1.23  root     2774:        
1.1.1.28  root     2775:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     2776:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.24  root     2777:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1       root     2778:        
1.1.1.14  root     2779:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   2780:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   2781:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   2782:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     2783:                }
                   2784:        }
1.1.1.28  root     2785:        if(get_console_info_success) {
1.1.1.12  root     2786:                scr_width = csbi.dwSize.X;
1.1.1.14  root     2787:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2788:                
1.1.1.28  root     2789:                // v-text shadow buffer size must be lesser than 0x7fd0
                   2790:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     2791:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   2792:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   2793:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     2794:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     2795:                                scr_width = 80;
                   2796:                                scr_height = 25;
                   2797:                        }
1.1.1.28  root     2798:                        screen_size_changed = true;
1.1.1.14  root     2799:                }
1.1.1.12  root     2800:        } else {
                   2801:                // for a proof (not a console)
                   2802:                scr_width = 80;
                   2803:                scr_height = 25;
                   2804:        }
1.1.1.14  root     2805:        scr_buf_size.X = scr_width;
                   2806:        scr_buf_size.Y = scr_height;
                   2807:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   2808:        scr_top = csbi.srWindow.Top;
1.1       root     2809:        cursor_moved = false;
                   2810:        
1.1.1.35  root     2811: #ifdef USE_SERVICE_THREAD
                   2812:        InitializeCriticalSection(&input_crit_sect);
                   2813:        InitializeCriticalSection(&key_buf_crit_sect);
                   2814:        InitializeCriticalSection(&putch_crit_sect);
                   2815: #endif
1.1.1.25  root     2816:        key_buf_char = new FIFO(256);
                   2817:        key_buf_scan = new FIFO(256);
1.1       root     2818:        
                   2819:        hardware_init();
                   2820:        
1.1.1.33  root     2821: #ifdef USE_DEBUGGER
                   2822:        debugger_init();
                   2823: #endif
                   2824:        
1.1.1.9   root     2825:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     2826:                retval = EXIT_FAILURE;
                   2827:        } else {
1.1.1.27  root     2828: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     2829:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   2830: #endif
                   2831:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   2832:                
1.1.1.28  root     2833:                if(screen_size_changed) {
1.1.1.24  root     2834:                        change_console_size(scr_width, scr_height);
                   2835:                }
1.1.1.8   root     2836:                TIMECAPS caps;
                   2837:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   2838:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.35  root     2839: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2840:                InitializeCriticalSection(&vram_crit_sect);
                   2841:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   2842: #endif
1.1.1.33  root     2843: #ifdef USE_DEBUGGER
                   2844:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   2845:                // wait until telnet client starts and connects to me
                   2846:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   2847:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   2848:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   2849:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   2850:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   2851:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   2852:                                Sleep(100);
                   2853:                        }
                   2854:                }
                   2855: #endif
1.1       root     2856:                hardware_run();
1.1.1.35  root     2857: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2858:                vram_flush();
                   2859:                DeleteCriticalSection(&vram_crit_sect);
                   2860: #endif
1.1.1.24  root     2861:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     2862:                
1.1.1.24  root     2863:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.28  root     2864:                if(get_console_info_success) {
1.1.1.23  root     2865:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2866:                        if(restore_console_on_exit) {
1.1.1.14  root     2867:                                // window can't be bigger than buffer,
                   2868:                                // buffer can't be smaller than window,
                   2869:                                // so make a tiny window,
                   2870:                                // set the required buffer,
                   2871:                                // then set the required window
                   2872:                                SMALL_RECT rect;
                   2873:                                SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
                   2874:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     2875:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     2876:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12  root     2877:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   2878:                        }
1.1.1.14  root     2879:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   2880:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     2881:                }
1.1.1.24  root     2882:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   2883:                
1.1       root     2884:                msdos_finish();
1.1.1.14  root     2885:                
                   2886:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     2887:        }
1.1.1.35  root     2888:        if(temp_file_created) {
                   2889:                DeleteFile(temp_file_path);
                   2890:                temp_file_created = false;
                   2891:        }
1.1.1.10  root     2892:        hardware_finish();
                   2893:        
1.1.1.28  root     2894:        if(key_buf_char != NULL) {
                   2895:                key_buf_char->release();
                   2896:                delete key_buf_char;
                   2897:                key_buf_char = NULL;
                   2898:        }
                   2899:        if(key_buf_scan != NULL) {
                   2900:                key_buf_scan->release();
                   2901:                delete key_buf_scan;
                   2902:                key_buf_scan = NULL;
                   2903:        }
1.1.1.35  root     2904: #ifdef USE_SERVICE_THREAD
                   2905:        DeleteCriticalSection(&input_crit_sect);
                   2906:        DeleteCriticalSection(&key_buf_crit_sect);
                   2907:        DeleteCriticalSection(&putch_crit_sect);
                   2908: #endif
1.1.1.20  root     2909: #ifdef _DEBUG
                   2910:        _CrtDumpMemoryLeaks();
                   2911: #endif
1.1       root     2912:        return(retval);
                   2913: }
                   2914: 
1.1.1.20  root     2915: /* ----------------------------------------------------------------------------
                   2916:        console
                   2917: ---------------------------------------------------------------------------- */
                   2918: 
1.1.1.14  root     2919: void change_console_size(int width, int height)
1.1.1.12  root     2920: {
1.1.1.23  root     2921:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2922:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2923:        SMALL_RECT rect;
                   2924:        COORD co;
                   2925:        
                   2926:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     2927:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   2928:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
                   2929:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
                   2930:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2931:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2932:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   2933:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
                   2934:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2935:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2936:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     2937:                }
                   2938:        }
1.1.1.14  root     2939:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     2940:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     2941:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     2942:                SetConsoleCursorPosition(hStdout, co);
                   2943:                cursor_moved = true;
                   2944:        }
1.1.1.14  root     2945:        
                   2946:        // window can't be bigger than buffer,
                   2947:        // buffer can't be smaller than window,
                   2948:        // so make a tiny window,
                   2949:        // set the required buffer,
                   2950:        // then set the required window
                   2951:        SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12  root     2952:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     2953:        co.X = width;
                   2954:        co.Y = height;
1.1.1.12  root     2955:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     2956:        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2957:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   2958:        
                   2959:        scr_width = scr_buf_size.X = width;
                   2960:        scr_height = scr_buf_size.Y = height;
                   2961:        scr_top = 0;
                   2962:        
                   2963:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   2964:        
                   2965:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     2966:        text_vram_end_address = text_vram_top_address + regen;
                   2967:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   2968:        
1.1.1.14  root     2969:        if(regen > 0x4000) {
                   2970:                regen = 0x8000;
                   2971:                vram_pages = 1;
                   2972:        } else if(regen > 0x2000) {
                   2973:                regen = 0x4000;
                   2974:                vram_pages = 2;
                   2975:        } else if(regen > 0x1000) {
                   2976:                regen = 0x2000;
                   2977:                vram_pages = 4;
                   2978:        } else {
                   2979:                regen = 0x1000;
                   2980:                vram_pages = 8;
                   2981:        }
1.1.1.15  root     2982:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   2983:        *(UINT16 *)(mem + 0x44c) = regen;
                   2984:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   2985:        
1.1.1.24  root     2986:        mouse.min_position.x = 0;
                   2987:        mouse.min_position.y = 0;
1.1.1.34  root     2988:        mouse.max_position.x = 8 * (scr_width  - 1);
                   2989:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     2990:        
1.1.1.15  root     2991:        restore_console_on_exit = true;
1.1.1.14  root     2992: }
                   2993: 
                   2994: void clear_scr_buffer(WORD attr)
                   2995: {
                   2996:        for(int y = 0; y < scr_height; y++) {
                   2997:                for(int x = 0; x < scr_width; x++) {
                   2998:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   2999:                        SCR_BUF(y,x).Attributes = attr;
                   3000:                }
                   3001:        }
1.1.1.12  root     3002: }
                   3003: 
1.1.1.24  root     3004: bool update_console_input()
1.1       root     3005: {
1.1.1.35  root     3006: #ifdef USE_SERVICE_THREAD
                   3007:        EnterCriticalSection(&input_crit_sect);
                   3008: #endif
1.1.1.23  root     3009:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     3010:        DWORD dwNumberOfEvents = 0;
1.1       root     3011:        DWORD dwRead;
                   3012:        INPUT_RECORD ir[16];
1.1.1.24  root     3013:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   3014:        bool result = false;
1.1       root     3015:        
1.1.1.8   root     3016:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   3017:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   3018:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     3019:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.34  root     3020:                                        if(mouse.hidden == 0) {
                   3021:                                                // NOTE: if restore_console_on_exit, console is not scrolled
                   3022:                                                if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   3023:                                                        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   3024:                                                }
                   3025:                                                // FIXME: character size is always 8x8 ???
                   3026:                                                int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   3027:                                                int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
                   3028:                                                
                   3029:                                                if(mouse.position.x != x || mouse.position.y != y) {
                   3030:                                                        mouse.position.x = x;
                   3031:                                                        mouse.position.y = y;
                   3032:                                                        mouse.status |= 1;
1.1.1.43  root     3033:                                                        mouse.status_alt |= 1;
1.1.1.34  root     3034:                                                }
                   3035:                                        }
                   3036:                                        if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   3037:                                                for(int i = 0; i < MAX_MOUSE_BUTTONS; i++) {
                   3038:                                                        static const DWORD bits[] = {
                   3039:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   3040:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
                   3041:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
                   3042:                                                        };
                   3043:                                                        bool prev_status = mouse.buttons[i].status;
                   3044:                                                        mouse.buttons[i].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[i]) != 0);
                   3045:                                                        
                   3046:                                                        if(!prev_status && mouse.buttons[i].status) {
                   3047:                                                                mouse.buttons[i].pressed_times++;
                   3048:                                                                mouse.buttons[i].pressed_position.x = mouse.position.x;
                   3049:                                                                mouse.buttons[i].pressed_position.y = mouse.position.x;
1.1.1.43  root     3050:                                                                if(i < 2) {
                   3051:                                                                        mouse.status_alt |= 2 << (i * 2);
                   3052:                                                                }
1.1.1.34  root     3053:                                                                mouse.status |= 2 << (i * 2);
                   3054:                                                        } else if(prev_status && !mouse.buttons[i].status) {
                   3055:                                                                mouse.buttons[i].released_times++;
                   3056:                                                                mouse.buttons[i].released_position.x = mouse.position.x;
                   3057:                                                                mouse.buttons[i].released_position.y = mouse.position.x;
1.1.1.43  root     3058:                                                                if(i < 2) {
                   3059:                                                                        mouse.status_alt |= 4 << (i * 2);
                   3060:                                                                }
1.1.1.34  root     3061:                                                                mouse.status |= 4 << (i * 2);
1.1.1.14  root     3062:                                                        }
                   3063:                                                }
                   3064:                                        }
1.1.1.24  root     3065:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3066:                                        // update keyboard flags in bios data area
1.1.1.35  root     3067:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
                   3068:                                                mem[0x417] |= 0x40;
1.1.1.33  root     3069:                                        } else {
1.1.1.35  root     3070:                                                mem[0x417] &= ~0x40;
1.1.1.33  root     3071:                                        }
1.1.1.35  root     3072:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
                   3073:                                                mem[0x417] |= 0x20;
1.1.1.33  root     3074:                                        } else {
1.1.1.35  root     3075:                                                mem[0x417] &= ~0x20;
                   3076:                                        }
                   3077:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
                   3078:                                                mem[0x417] |= 0x10;
                   3079:                                        } else {
                   3080:                                                mem[0x417] &= ~0x10;
1.1.1.33  root     3081:                                        }
                   3082:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43  root     3083:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3084:                                                        mouse.status_alt |= 0x80;
                   3085:                                                }
1.1.1.33  root     3086:                                                mem[0x417] |= 0x08;
                   3087:                                        } else {
                   3088:                                                mem[0x417] &= ~0x08;
                   3089:                                        }
1.1.1.35  root     3090:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43  root     3091:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3092:                                                        mouse.status_alt |= 0x40;
                   3093:                                                }
1.1.1.35  root     3094:                                                mem[0x417] |= 0x04;
1.1.1.33  root     3095:                                        } else {
1.1.1.35  root     3096:                                                mem[0x417] &= ~0x04;
1.1.1.33  root     3097:                                        }
                   3098:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43  root     3099:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3100:                                                        mouse.status_alt |= 0x20;
                   3101:                                                }
1.1.1.33  root     3102:                                                if(!(mem[0x417] & 0x03)) {
                   3103:                                                        mem[0x417] |= 0x02; // left shift
                   3104:                                                }
                   3105:                                        } else {
                   3106:                                                mem[0x417] &= ~0x03;
                   3107:                                        }
1.1.1.35  root     3108:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3109:                                                mem[0x418] |= 0x02;
                   3110:                                        } else {
                   3111:                                                mem[0x418] &= ~0x02;
                   3112:                                        }
                   3113:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3114:                                                mem[0x418] |= 0x01;
                   3115:                                        } else {
                   3116:                                                mem[0x418] &= ~0x01;
                   3117:                                        }
1.1.1.33  root     3118:                                        
1.1.1.28  root     3119:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.24  root     3120:                                        kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3121:                                        kbd_status |= 1;
                   3122:                                        
                   3123:                                        // update dos key buffer
                   3124:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3125:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
                   3126:                                        
                   3127:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3128:                                                // make
1.1.1.24  root     3129:                                                kbd_data &= 0x7f;
                   3130:                                                
1.1.1.33  root     3131:                                                if(chr == 0x00) {
1.1.1.24  root     3132:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3133:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3134:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3135:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3136:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3137:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3138:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3139:                                                                } else if(scn == 0x35) {
                   3140:                                                                        scn = 0xa4;             // keypad /
                   3141:                                                                }
                   3142:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3143:                                                                if(scn == 0x07) {
                   3144:                                                                        chr = 0x1e;     // Ctrl+^
                   3145:                                                                } else if(scn == 0x0c) {
                   3146:                                                                        chr = 0x1f;     // Ctrl+_
                   3147:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3148:                                                                        static const UINT8 ctrl_map[] = {
                   3149:                                                                                0x95,   // keypad /
                   3150:                                                                                0,
                   3151:                                                                                0x96,   // keypad *
                   3152:                                                                                0, 0, 0,
                   3153:                                                                                0x5e,   // F1
                   3154:                                                                                0x5f,   // F2
                   3155:                                                                                0x60,   // F3
                   3156:                                                                                0x61,   // F4
                   3157:                                                                                0x62,   // F5
                   3158:                                                                                0x63,   // F6
                   3159:                                                                                0x64,   // F7
                   3160:                                                                                0x65,   // F8
                   3161:                                                                                0x66,   // F9
                   3162:                                                                                0x67,   // F10
                   3163:                                                                                0,
                   3164:                                                                                0,
                   3165:                                                                                0x77,   // Home
                   3166:                                                                                0x8d,   // Up
                   3167:                                                                                0x84,   // PgUp
                   3168:                                                                                0x8e,   // keypad -
                   3169:                                                                                0x73,   // Left
                   3170:                                                                                0x8f,   // keypad center
                   3171:                                                                                0x74,   // Right
                   3172:                                                                                0x90,   // keyapd +
                   3173:                                                                                0x75,   // End
                   3174:                                                                                0x91,   // Down
                   3175:                                                                                0x76,   // PgDn
                   3176:                                                                                0x92,   // Insert
                   3177:                                                                                0x93,   // Delete
                   3178:                                                                                0, 0, 0,
                   3179:                                                                                0x89,   // F11
                   3180:                                                                                0x8a,   // F12
                   3181:                                                                        };
                   3182:                                                                        scn = ctrl_map[scn - 0x35];
                   3183:                                                                }
                   3184:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3185:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3186:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3187:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3188:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3189:                                                                }
                   3190:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3191:                                                                scn += 0x85 - 0x57;
                   3192:                                                        }
                   3193:                                                        // ignore shift, ctrl, alt, win and menu keys
                   3194:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && (scn < 0x5b || scn > 0x5e)) {
1.1.1.32  root     3195:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3196: #ifdef USE_SERVICE_THREAD
                   3197:                                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3198: #endif
1.1.1.32  root     3199:                                                                        if(chr == 0) {
                   3200:                                                                                key_buf_char->write(0x00);
                   3201:                                                                                key_buf_scan->write(ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
                   3202:                                                                        }
                   3203:                                                                        key_buf_char->write(chr);
                   3204:                                                                        key_buf_scan->write(scn);
1.1.1.35  root     3205: #ifdef USE_SERVICE_THREAD
                   3206:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3207: #endif
1.1.1.24  root     3208:                                                                }
                   3209:                                                        }
                   3210:                                                } else {
                   3211:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3212:                                                                chr = 0;
                   3213:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3214:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3215:                                                                }
                   3216:                                                        }
1.1.1.32  root     3217:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3218: #ifdef USE_SERVICE_THREAD
                   3219:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3220: #endif
1.1.1.32  root     3221:                                                                key_buf_char->write(chr);
                   3222:                                                                key_buf_scan->write(scn);
1.1.1.35  root     3223: #ifdef USE_SERVICE_THREAD
                   3224:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3225: #endif
1.1.1.32  root     3226:                                                        }
1.1.1.24  root     3227:                                                }
1.1.1.33  root     3228:                                        } else if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
                   3229:                                                // ctrl-break, ctrl-c
                   3230:                                                if(scn == 0x46) {
                   3231:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3232: #ifdef USE_SERVICE_THREAD
                   3233:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3234: #endif
1.1.1.33  root     3235:                                                                key_buf_char->write(0x00);
                   3236:                                                                key_buf_scan->write(0x00);
1.1.1.35  root     3237: #ifdef USE_SERVICE_THREAD
                   3238:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3239: #endif
1.1.1.33  root     3240:                                                        }
                   3241:                                                        ctrl_break_pressed = true;
                   3242:                                                        mem[0x471] = 0x80;
                   3243:                                                        raise_int_1bh = true;
                   3244:                                                } else {
                   3245:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3246: #ifdef USE_SERVICE_THREAD
                   3247:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3248: #endif
1.1.1.33  root     3249:                                                                key_buf_char->write(chr);
                   3250:                                                                key_buf_scan->write(scn);
1.1.1.35  root     3251: #ifdef USE_SERVICE_THREAD
                   3252:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3253: #endif
1.1.1.33  root     3254:                                                        }
                   3255:                                                        ctrl_c_pressed = (scn == 0x2e);
                   3256:                                                }
                   3257:                                        } else {
                   3258:                                                // break
                   3259:                                                kbd_data |= 0x80;
1.1       root     3260:                                        }
1.1.1.24  root     3261:                                        result = key_changed = true;
1.1.1.36  root     3262:                                        // IME may be on and it may causes screen scroll up and cursor position change
                   3263:                                        cursor_moved = true;
1.1       root     3264:                                }
                   3265:                        }
                   3266:                }
                   3267:        }
1.1.1.35  root     3268: #ifdef USE_SERVICE_THREAD
                   3269:        LeaveCriticalSection(&input_crit_sect);
                   3270: #endif
1.1.1.24  root     3271:        return(result);
1.1.1.8   root     3272: }
                   3273: 
1.1.1.14  root     3274: bool update_key_buffer()
1.1.1.8   root     3275: {
1.1.1.35  root     3276:        if(update_console_input()) {
                   3277:                return(true);
                   3278:        }
                   3279:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3280: #ifdef USE_SERVICE_THREAD
                   3281:                EnterCriticalSection(&key_buf_crit_sect);
                   3282: #endif
                   3283:                bool empty = key_buf_char->empty();
                   3284: #ifdef USE_SERVICE_THREAD
                   3285:                LeaveCriticalSection(&key_buf_crit_sect);
                   3286: #endif
                   3287:                if(!empty) return(true);
                   3288:        }
                   3289:        return(false);
1.1.1.8   root     3290: }
                   3291: 
1.1.1.20  root     3292: /* ----------------------------------------------------------------------------
                   3293:        MS-DOS virtual machine
                   3294: ---------------------------------------------------------------------------- */
                   3295: 
1.1.1.32  root     3296: static const struct {
1.1.1.33  root     3297:        char *name;
                   3298:        DWORD lcid;
                   3299:        char *std;
                   3300:        char *dlt;
                   3301: } tz_table[] = {
                   3302:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3303: //     0       GMT             Greenwich Mean Time             GMT0
                   3304:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3305:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3306:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3307:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3308: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3309:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3310:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3311: //     3       BST             Brazil Standard Time            BST3
                   3312:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3313:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3314:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3315: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3316:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3317: //     3       GST             Greenland Standard Time         GST3
                   3318:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3319: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3320:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3321: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3322:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3323: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3324:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3325:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3326: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3327:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3328:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3329:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3330: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3331:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3332: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3333:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3334: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3335:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3336: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3337:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3338:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3339:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3340: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3341:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3342: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3343:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3344:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3345:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3346: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3347:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3348:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3349: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3350: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3351:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3352: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3353:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3354:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3355: //     11      SST             Samoa Standard Time             SST11
                   3356:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3357: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3358:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3359: //     -10     GST             Guam Standard Time              GST-10
                   3360:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3361: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3362:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3363:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3364:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3365: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3366:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3367:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3368: //     -9      JST             Japan Standard Time             JST-9
                   3369:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3370: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3371:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3372:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3373: //     -8      HKT             Hong Kong Time                  HKT-8
                   3374:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3375: //     -8      CCT             China Coast Time                CCT-8
                   3376:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3377:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3378: //     -8      SST             Singapore Standard Time         SST-8
                   3379:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3380: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3381:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3382:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3383: //     -7:30   JT              Java Standard Time              JST-7:30
                   3384: //     -7      NST             North Sumatra Time              NST-7
                   3385:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3386: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3387:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3388: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3389:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3390: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3391:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3392:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3393: //     -2      EET             Eastern Europe Time             EET-2
                   3394:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3395:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3396:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3397:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3398: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3399:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3400: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3401: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3402: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3403: //     -1      CET     CES     Central European Time           CET-1CES
                   3404:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3405:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3406:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3407:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3408: //     -1      WAT             West African Time               WAT-1
                   3409:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3410:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3411: //     0       UTC             Universal Coordinated Time      UTC0
                   3412:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3413:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3414:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3415:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3416:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3417:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3418: };
                   3419: 
                   3420: static const struct {
1.1.1.32  root     3421:        UINT16 code;
                   3422:        char *message_english;
                   3423:        char *message_japanese;
                   3424: } standard_error_table[] = {
                   3425:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3426:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3427:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3428:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3429:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3430:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3431:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3432:        {0x08,  "Insufficient memory", "������������܂���."},
                   3433:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3434:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3435:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3436:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3437:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3438:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3439:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3440:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3441:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3442:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3443:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3444:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3445:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3446:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3447:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3448:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3449:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3450:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3451:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3452:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3453:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3454:        {0x1F,  "General failure", "�G���[�ł�."},
                   3455:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3456:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3457:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3458:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3459:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3460:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3461:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3462:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3463: /*
                   3464:        {0x32,  "Network request not supported", NULL},
                   3465:        {0x33,  "Remote computer not listening", NULL},
                   3466:        {0x34,  "Duplicate name on network", NULL},
                   3467:        {0x35,  "Network name not found", NULL},
                   3468:        {0x36,  "Network busy", NULL},
                   3469:        {0x37,  "Network device no longer exists", NULL},
                   3470:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3471:        {0x39,  "Network adapter hardware error", NULL},
                   3472:        {0x3A,  "Incorrect response from network", NULL},
                   3473:        {0x3B,  "Unexpected network error", NULL},
                   3474:        {0x3C,  "Incompatible remote adapter", NULL},
                   3475:        {0x3D,  "Print queue full", NULL},
                   3476:        {0x3E,  "Queue not full", NULL},
                   3477:        {0x3F,  "Not enough space to print file", NULL},
                   3478:        {0x40,  "Network name was deleted", NULL},
                   3479:        {0x41,  "Network: Access denied", NULL},
                   3480:        {0x42,  "Network device type incorrect", NULL},
                   3481:        {0x43,  "Network name not found", NULL},
                   3482:        {0x44,  "Network name limit exceeded", NULL},
                   3483:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3484:        {0x46,  "Temporarily paused", NULL},
                   3485:        {0x47,  "Network request not accepted", NULL},
                   3486:        {0x48,  "Network print/disk redirection paused", NULL},
                   3487:        {0x49,  "Network software not installed", NULL},
                   3488:        {0x4A,  "Unexpected adapter close", NULL},
                   3489: */
                   3490:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3491:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3492:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3493:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3494:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3495:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3496:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3497:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3498:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3499:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
                   3500: /*
                   3501:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3502:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3503:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3504:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3505:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
                   3506: */
                   3507:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3508:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3509:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3510:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3511:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3512:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3513: };
                   3514: 
                   3515: static const struct {
                   3516:        UINT16 code;
                   3517:        char *message_english;
                   3518:        char *message_japanese;
                   3519: } param_error_table[] = {
                   3520:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3521:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3522:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3523:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3524:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3525:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3526:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3527:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3528:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3529:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3530:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3531: };
                   3532: 
                   3533: static const struct {
                   3534:        UINT16 code;
                   3535:        char *message_english;
                   3536:        char *message_japanese;
                   3537: } critical_error_table[] = {
                   3538:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3539:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3540:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3541:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3542:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3543:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3544:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3545:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3546:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3547:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3548:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3549:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3550:        {0x0C,  "General failure", "�G���[�ł�."},
                   3551:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3552:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3553:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3554:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3555:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3556:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3557:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3558:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3559:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3560: };
                   3561: 
1.1.1.20  root     3562: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3563: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3564: void msdos_putch(UINT8 data);
1.1.1.35  root     3565: #ifdef USE_SERVICE_THREAD
                   3566: void msdos_putch_tmp(UINT8 data);
                   3567: #endif
1.1.1.45  root     3568: const char *msdos_short_path(const char *path);
1.1.1.44  root     3569: bool msdos_is_valid_drive(int drv);
                   3570: bool msdos_is_removable_drive(int drv);
                   3571: bool msdos_is_cdrom_drive(int drv);
                   3572: bool msdos_is_remote_drive(int drv);
                   3573: bool msdos_is_subst_drive(int drv);
1.1.1.20  root     3574: 
1.1       root     3575: // process info
                   3576: 
                   3577: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3578: {
                   3579:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3580:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3581:                        memset(&process[i], 0, sizeof(process_t));
                   3582:                        process[i].psp = psp_seg;
                   3583:                        return(&process[i]);
                   3584:                }
                   3585:        }
                   3586:        fatalerror("too many processes\n");
                   3587:        return(NULL);
                   3588: }
                   3589: 
1.1.1.33  root     3590: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error)
1.1       root     3591: {
                   3592:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3593:                if(process[i].psp == psp_seg) {
                   3594:                        return(&process[i]);
                   3595:                }
                   3596:        }
1.1.1.33  root     3597:        if(show_error) {
                   3598:                fatalerror("invalid psp address\n");
                   3599:        }
1.1       root     3600:        return(NULL);
                   3601: }
                   3602: 
1.1.1.33  root     3603: process_t *msdos_process_info_get(UINT16 psp_seg)
                   3604: {
                   3605:        return(msdos_process_info_get(psp_seg, true));
                   3606: }
                   3607: 
1.1.1.23  root     3608: void msdos_sda_update(int psp_seg)
                   3609: {
                   3610:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3611:        
                   3612:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3613:                if(process[i].psp == psp_seg) {
                   3614:                        sda->switchar = process[i].switchar;
                   3615:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3616:                        sda->current_dta.w.h = process[i].dta.w.h;
                   3617:                        sda->current_psp = process[i].psp;
                   3618:                        break;
                   3619:                }
                   3620:        }
                   3621:        sda->malloc_strategy = malloc_strategy;
                   3622:        sda->return_code = retval;
                   3623:        sda->current_drive = _getdrive();
                   3624: }
                   3625: 
1.1.1.13  root     3626: // dta info
                   3627: 
                   3628: void msdos_dta_info_init()
                   3629: {
1.1.1.14  root     3630:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     3631:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3632:        }
                   3633: }
                   3634: 
                   3635: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   3636: {
                   3637:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     3638:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3639:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   3640:                        if(free_dta == NULL) {
1.1.1.13  root     3641:                                free_dta = &dtalist[i];
                   3642:                        }
1.1.1.14  root     3643:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     3644:                        return(&dtalist[i]);
                   3645:                }
                   3646:        }
1.1.1.14  root     3647:        if(free_dta) {
1.1.1.13  root     3648:                free_dta->psp = psp_seg;
                   3649:                free_dta->dta = dta_laddr;
                   3650:                return(free_dta);
                   3651:        }
                   3652:        fatalerror("too many dta\n");
                   3653:        return(NULL);
                   3654: }
                   3655: 
                   3656: void msdos_dta_info_free(UINT16 psp_seg)
                   3657: {
1.1.1.14  root     3658:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3659:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     3660:                        FindClose(dtalist[i].find_handle);
                   3661:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3662:                }
                   3663:        }
                   3664: }
                   3665: 
1.1       root     3666: void msdos_cds_update(int drv)
                   3667: {
1.1.1.44  root     3668:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1       root     3669:        
1.1.1.44  root     3670:        memset(cds, 0, 88);
                   3671:        
                   3672:        if(msdos_is_valid_drive(drv)) {
                   3673:                char path[MAX_PATH];
                   3674:                if(msdos_is_remote_drive(drv)) {
                   3675:                        cds->drive_attrib = 0xc000;     // network drive
                   3676:                } else if(msdos_is_subst_drive(drv)) {
                   3677:                        cds->drive_attrib = 0x5000;     // subst drive
                   3678:                } else {
                   3679:                        cds->drive_attrib = 0x4000;     // physical drive
                   3680:                }
                   3681:                if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
                   3682:                        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
                   3683:                }
                   3684:        }
                   3685:        if(cds->path_name[0] == '\0') {
                   3686:                sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   3687:        }
                   3688:        cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   3689:        cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
                   3690:        cds->word_1 = cds->word_2 = 0xffff;
                   3691:        cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
                   3692:        cds->bs_offset = 2;
                   3693: }
                   3694: 
1.1.1.45  root     3695: void msdos_cds_update(int drv, const char *path)
1.1.1.44  root     3696: {
                   3697:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
                   3698:        
                   3699:        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1       root     3700: }
                   3701: 
1.1.1.17  root     3702: // nls information tables
                   3703: 
                   3704: // uppercase table (func 6502h)
                   3705: void msdos_upper_table_update()
                   3706: {
                   3707:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3708:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3709:                UINT8 c[4];
1.1.1.33  root     3710:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3711:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     3712:                c[0] = 0x80 + i;
                   3713:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   3714:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3715:        }
                   3716: }
                   3717: 
1.1.1.23  root     3718: // lowercase table (func 6503h)
                   3719: void msdos_lower_table_update()
                   3720: {
                   3721:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   3722:        for(unsigned i = 0; i < 0x80; ++i) {
                   3723:                UINT8 c[4];
1.1.1.33  root     3724:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3725:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     3726:                c[0] = 0x80 + i;
                   3727:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   3728:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3729:        }
                   3730: }
                   3731: 
1.1.1.17  root     3732: // filename uppercase table (func 6504h)
                   3733: void msdos_filename_upper_table_init()
                   3734: {
                   3735:        // depended on (file)system, not on active codepage
                   3736:        // temporary solution: just filling data
                   3737:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3738:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3739:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   3740:        }
                   3741: }
                   3742: 
                   3743: // filaname terminator table (func 6505h)
                   3744: void msdos_filename_terminator_table_init()
                   3745: {
                   3746:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   3747:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   3748:        
                   3749:        data[2] = 1;            // marker? (permissible character value)
                   3750:        data[3] = 0x00;         // 00h...FFh
                   3751:        data[4] = 0xff;
                   3752:        data[5] = 0;            // marker? (excluded character)
                   3753:        data[6] = 0x00;         // 00h...20h
                   3754:        data[7] = 0x20;
                   3755:        data[8] = 2;            // marker? (illegal characters for filename)
                   3756:        data[9] = (UINT8)strlen(illegal_chars);
                   3757:        memcpy(data + 10, illegal_chars, data[9]);
                   3758:        
                   3759:        // total length
                   3760:        *(UINT16 *)data = (10 - 2) + data[9];
                   3761: }
                   3762: 
                   3763: // collating table (func 6506h)
                   3764: void msdos_collating_table_update()
                   3765: {
                   3766:        // temporary solution: just filling data
                   3767:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     3768:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     3769:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   3770:        }
                   3771: }
                   3772: 
1.1       root     3773: // dbcs
                   3774: 
                   3775: void msdos_dbcs_table_update()
                   3776: {
                   3777:        UINT8 dbcs_data[DBCS_SIZE];
                   3778:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   3779:        
                   3780:        CPINFO info;
                   3781:        GetCPInfo(active_code_page, &info);
                   3782:        
                   3783:        if(info.MaxCharSize != 1) {
                   3784:                for(int i = 0;; i += 2) {
                   3785:                        UINT8 lo = info.LeadByte[i + 0];
                   3786:                        UINT8 hi = info.LeadByte[i + 1];
                   3787:                        dbcs_data[2 + i + 0] = lo;
                   3788:                        dbcs_data[2 + i + 1] = hi;
                   3789:                        if(lo == 0 && hi == 0) {
                   3790:                                dbcs_data[0] = i + 2;
                   3791:                                break;
                   3792:                        }
                   3793:                }
                   3794:        } else {
                   3795:                dbcs_data[0] = 2;       // ???
                   3796:        }
                   3797:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   3798: }
                   3799: 
1.1.1.17  root     3800: void msdos_dbcs_table_finish()
                   3801: {
1.1.1.32  root     3802:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     3803:                _setmbcp(system_code_page);
                   3804:        }
1.1.1.32  root     3805:        if(console_code_page != GetConsoleCP()) {
                   3806:                SetConsoleCP(console_code_page);
                   3807:                SetConsoleOutputCP(console_code_page);
                   3808:        }
1.1.1.17  root     3809: }
                   3810: 
                   3811: void msdos_nls_tables_init()
1.1       root     3812: {
1.1.1.32  root     3813:        active_code_page = console_code_page = GetConsoleCP();
                   3814:        system_code_page = _getmbcp();
                   3815:        
                   3816:        if(active_code_page != system_code_page) {
                   3817:                if(_setmbcp(active_code_page) != 0) {
                   3818:                        active_code_page = system_code_page;
                   3819:                }
                   3820:        }
                   3821:        
1.1.1.17  root     3822:        msdos_upper_table_update();
1.1.1.23  root     3823:        msdos_lower_table_update();
1.1.1.17  root     3824:        msdos_filename_terminator_table_init();
                   3825:        msdos_filename_upper_table_init();
                   3826:        msdos_collating_table_update();
1.1       root     3827:        msdos_dbcs_table_update();
                   3828: }
                   3829: 
1.1.1.17  root     3830: void msdos_nls_tables_update()
1.1       root     3831: {
1.1.1.17  root     3832:        msdos_dbcs_table_update();
                   3833:        msdos_upper_table_update();
1.1.1.23  root     3834:        msdos_lower_table_update();
                   3835: //     msdos_collating_table_update();
1.1       root     3836: }
                   3837: 
                   3838: int msdos_lead_byte_check(UINT8 code)
                   3839: {
                   3840:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   3841:        
                   3842:        for(int i = 0;; i += 2) {
                   3843:                UINT8 lo = dbcs_table[i + 0];
                   3844:                UINT8 hi = dbcs_table[i + 1];
                   3845:                if(lo == 0 && hi == 0) {
                   3846:                        break;
                   3847:                }
                   3848:                if(lo <= code && code <= hi) {
                   3849:                        return(1);
                   3850:                }
                   3851:        }
                   3852:        return(0);
                   3853: }
                   3854: 
1.1.1.20  root     3855: int msdos_ctrl_code_check(UINT8 code)
                   3856: {
1.1.1.22  root     3857:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     3858: }
                   3859: 
1.1.1.36  root     3860: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
                   3861: {
                   3862:        int is_kanji_1st = 0;
                   3863:        int is_kanji_2nd = 0;
                   3864:        
                   3865:        for(int p = 0;; p++) {
                   3866:                if(is_kanji_1st) {
                   3867:                        is_kanji_1st = 0;
                   3868:                        is_kanji_2nd = 1;
                   3869:                } else if(msdos_lead_byte_check(buf[p])) {
                   3870:                        is_kanji_1st = 1;
                   3871:                }
                   3872:                if(p == n) {
                   3873:                        return(is_kanji_2nd);
                   3874:                }
                   3875:                is_kanji_2nd = 0;
                   3876:        }
                   3877: }
                   3878: 
1.1       root     3879: // file control
                   3880: 
1.1.1.45  root     3881: const char *msdos_remove_double_quote(const char *path)
1.1.1.14  root     3882: {
                   3883:        static char tmp[MAX_PATH];
                   3884:        
                   3885:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45  root     3886:                memset(tmp, 0, sizeof(tmp));
1.1.1.14  root     3887:                memcpy(tmp, path + 1, strlen(path) - 2);
                   3888:        } else {
                   3889:                strcpy(tmp, path);
                   3890:        }
                   3891:        return(tmp);
                   3892: }
                   3893: 
1.1.1.45  root     3894: const char *msdos_remove_end_separator(const char *path)
1.1.1.32  root     3895: {
                   3896:        static char tmp[MAX_PATH];
                   3897:        
                   3898:        strcpy(tmp, path);
1.1.1.45  root     3899:        
                   3900:        // for example "C:\" case, the end separator should not be removed
                   3901:        if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
                   3902:                tmp[strlen(tmp) - 1] = '\0';
1.1.1.32  root     3903:        }
                   3904:        return(tmp);
                   3905: }
                   3906: 
1.1.1.45  root     3907: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14  root     3908: {
                   3909:        static char tmp[MAX_PATH];
1.1.1.45  root     3910:        const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14  root     3911:        
                   3912:        if(strlen(tmp_dir) == 0) {
                   3913:                strcpy(tmp, file);
                   3914:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   3915:                sprintf(tmp, "%s%s", tmp_dir, file);
                   3916:        } else {
                   3917:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   3918:        }
                   3919:        return(tmp);
                   3920: }
                   3921: 
1.1.1.45  root     3922: const char *msdos_trimmed_path(const char *path, int lfn)
1.1       root     3923: {
                   3924:        static char tmp[MAX_PATH];
                   3925:        
                   3926:        if(lfn) {
                   3927:                strcpy(tmp, path);
                   3928:        } else {
                   3929:                // remove space in the path
1.1.1.45  root     3930:                const char *src = path;
                   3931:                char *dst = tmp;
1.1       root     3932:                
                   3933:                while(*src != '\0') {
                   3934:                        if(msdos_lead_byte_check(*src)) {
                   3935:                                *dst++ = *src++;
                   3936:                                *dst++ = *src++;
                   3937:                        } else if(*src != ' ') {
                   3938:                                *dst++ = *src++;
                   3939:                        } else {
                   3940:                                src++;  // skip space
                   3941:                        }
                   3942:                }
                   3943:                *dst = '\0';
                   3944:        }
1.1.1.14  root     3945:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   3946:                // redirect C:\COMMAND.COM to comspec_path
                   3947:                strcpy(tmp, comspec_path);
                   3948:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   3949:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   3950:                static int root_drive_protected = -1;
                   3951:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   3952:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   3953:                
                   3954:                if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
                   3955:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   3956:                        strcpy(name, name_temp);
                   3957:                        name_temp[0] = '\0';
                   3958:                        
                   3959:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   3960:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   3961:                                if(root_drive_protected == -1) {
                   3962:                                        FILE *fp = NULL;
                   3963:                                        
                   3964:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   3965:                                        root_drive_protected = 1;
                   3966:                                        try {
                   3967:                                                if((fp = fopen(temp, "w")) != NULL) {
                   3968:                                                        if(fprintf(fp, "TEST") == 4) {
                   3969:                                                                root_drive_protected = 0;
                   3970:                                                        }
                   3971:                                                }
                   3972:                                        } catch(...) {
                   3973:                                        }
                   3974:                                        if(fp != NULL) {
                   3975:                                                fclose(fp);
                   3976:                                        }
                   3977:                                        if(_access(temp, 0) == 0) {
                   3978:                                                remove(temp);
                   3979:                                        }
                   3980:                                }
                   3981:                                if(root_drive_protected == 1) {
                   3982:                                        if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
                   3983:                                           GetEnvironmentVariable("TMP",  temp, MAX_PATH) != 0) {
                   3984:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   3985:                                        }
                   3986:                                }
                   3987:                        }
                   3988:                }
                   3989:        }
1.1       root     3990:        return(tmp);
                   3991: }
                   3992: 
1.1.1.45  root     3993: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28  root     3994: {
1.1.1.32  root     3995:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     3996:        static char env_path[ENV_SIZE];
                   3997:        char tmp[ENV_SIZE], *token;
                   3998:        
                   3999:        memset(env_path, 0, sizeof(env_path));
                   4000:        strcpy(tmp, src);
                   4001:        token = my_strtok(tmp, ";");
                   4002:        
                   4003:        while(token != NULL) {
                   4004:                if(token[0] != '\0') {
1.1.1.45  root     4005:                        const char *path = msdos_remove_double_quote(token);
                   4006:                        char short_path[MAX_PATH];
1.1.1.32  root     4007:                        if(path != NULL && strlen(path) != 0) {
                   4008:                                if(env_path[0] != '\0') {
                   4009:                                        strcat(env_path, ";");
                   4010:                                }
1.1.1.28  root     4011:                                if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     4012:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     4013:                                } else {
                   4014:                                        my_strupr(short_path);
1.1.1.32  root     4015:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     4016:                                }
                   4017:                        }
                   4018:                }
                   4019:                token = my_strtok(NULL, ";");
                   4020:        }
                   4021:        return(env_path);
                   4022: }
                   4023: 
1.1.1.45  root     4024: bool match(const char *text, const char *pattern)
1.1       root     4025: {
1.1.1.24  root     4026:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     4027:        switch(*pattern) {
1.1       root     4028:        case '\0':
                   4029:                return !*text;
                   4030:        case '*':
1.1.1.14  root     4031:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     4032:        case '?':
                   4033:                return *text && match(text + 1, pattern + 1);
                   4034:        default:
                   4035:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   4036:        }
                   4037: }
                   4038: 
1.1.1.45  root     4039: bool msdos_match_volume_label(const char *path, const char *volume)
1.1       root     4040: {
1.1.1.45  root     4041:        const char *p = NULL;
1.1       root     4042:        
1.1.1.14  root     4043:        if(!*volume) {
                   4044:                return false;
                   4045:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     4046:                return msdos_match_volume_label(p + 1, volume);
                   4047:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   4048:                return msdos_match_volume_label(p + 1, volume);
                   4049:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     4050:                char tmp[MAX_PATH];
                   4051:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   4052:                return match(volume, tmp);
1.1       root     4053:        } else {
                   4054:                return match(volume, path);
                   4055:        }
                   4056: }
                   4057: 
1.1.1.45  root     4058: const char *msdos_fcb_path(fcb_t *fcb)
1.1       root     4059: {
                   4060:        static char tmp[MAX_PATH];
                   4061:        char name[9], ext[4];
                   4062:        
                   4063:        memset(name, 0, sizeof(name));
                   4064:        memcpy(name, fcb->file_name, 8);
                   4065:        strcpy(name, msdos_trimmed_path(name, 0));
                   4066:        
                   4067:        memset(ext, 0, sizeof(ext));
                   4068:        memcpy(ext, fcb->file_name + 8, 3);
                   4069:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4070:        
                   4071:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4072:                strcpy(name, "*");
                   4073:        }
                   4074:        if(ext[0] == '\0') {
                   4075:                strcpy(tmp, name);
                   4076:        } else {
                   4077:                if(strcmp(ext, "???") == 0) {
                   4078:                        strcpy(ext, "*");
                   4079:                }
                   4080:                sprintf(tmp, "%s.%s", name, ext);
                   4081:        }
                   4082:        return(tmp);
                   4083: }
                   4084: 
1.1.1.45  root     4085: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1       root     4086: {
                   4087:        char *ext = my_strchr(path, '.');
                   4088:        
                   4089:        memset(fcb->file_name, 0x20, 8 + 3);
                   4090:        if(ext != NULL && path[0] != '.') {
                   4091:                *ext = '\0';
                   4092:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4093:        }
                   4094:        memcpy(fcb->file_name, path, strlen(path));
                   4095: }
                   4096: 
1.1.1.45  root     4097: const char *msdos_short_path(const char *path)
1.1       root     4098: {
                   4099:        static char tmp[MAX_PATH];
                   4100:        
1.1.1.24  root     4101:        if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
                   4102:                strcpy(tmp, path);
                   4103:        }
1.1       root     4104:        my_strupr(tmp);
                   4105:        return(tmp);
                   4106: }
                   4107: 
1.1.1.45  root     4108: const char *msdos_short_name(WIN32_FIND_DATA *fd)
1.1.1.13  root     4109: {
                   4110:        static char tmp[MAX_PATH];
1.1.1.45  root     4111:        
1.1.1.14  root     4112:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4113:                strcpy(tmp, fd->cAlternateFileName);
                   4114:        } else {
                   4115:                strcpy(tmp, fd->cFileName);
                   4116:        }
                   4117:        my_strupr(tmp);
                   4118:        return(tmp);
                   4119: }
                   4120: 
1.1.1.45  root     4121: const char *msdos_short_full_path(const char *path)
1.1       root     4122: {
                   4123:        static char tmp[MAX_PATH];
                   4124:        char full[MAX_PATH], *name;
                   4125:        
1.1.1.14  root     4126:        // Full works with non-existent files, but Short does not
1.1       root     4127:        GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14  root     4128:        *tmp = '\0';
                   4129:        if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
                   4130:                name[-1] = '\0';
                   4131:                DWORD len = GetShortPathName(full, tmp, MAX_PATH);
                   4132:                if(len == 0) {
                   4133:                        strcpy(tmp, full);
                   4134:                } else {
                   4135:                        tmp[len++] = '\\';
                   4136:                        strcpy(tmp + len, name);
                   4137:                }
                   4138:        }
1.1       root     4139:        my_strupr(tmp);
                   4140:        return(tmp);
                   4141: }
                   4142: 
1.1.1.45  root     4143: const char *msdos_short_full_dir(const char *path)
1.1       root     4144: {
                   4145:        static char tmp[MAX_PATH];
                   4146:        char full[MAX_PATH], *name;
                   4147:        
                   4148:        GetFullPathName(path, MAX_PATH, full, &name);
                   4149:        name[-1] = '\0';
1.1.1.24  root     4150:        if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
                   4151:                strcpy(tmp, full);
                   4152:        }
1.1       root     4153:        my_strupr(tmp);
                   4154:        return(tmp);
                   4155: }
                   4156: 
1.1.1.45  root     4157: const char *msdos_local_file_path(const char *path, int lfn)
1.1       root     4158: {
1.1.1.45  root     4159:        static char trimmed[MAX_PATH];
                   4160:        
                   4161:        strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14  root     4162: #if 0
                   4163:        // I have forgotten the reason of this routine... :-(
1.1       root     4164:        if(_access(trimmed, 0) != 0) {
                   4165:                process_t *process = msdos_process_info_get(current_psp);
                   4166:                static char tmp[MAX_PATH];
                   4167:                
                   4168:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4169:                if(_access(tmp, 0) == 0) {
                   4170:                        return(tmp);
                   4171:                }
                   4172:        }
1.1.1.14  root     4173: #endif
1.1       root     4174:        return(trimmed);
                   4175: }
                   4176: 
1.1.1.45  root     4177: bool msdos_is_device_path(const char *path)
1.1.1.11  root     4178: {
                   4179:        char full[MAX_PATH], *name;
                   4180:        
1.1.1.24  root     4181:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4182:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4183:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4184:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4185:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4186:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4187:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4188:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4189:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4190:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4191:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4192:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4193:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4194:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4195:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4196:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4197:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4198:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4199:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4200:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4201:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4202:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4203:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4204:                        return(true);
                   4205:                } else if(name != NULL) {
                   4206:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4207:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4208:                           _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43  root     4209: //                        _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30  root     4210:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4211:                                return(true);
                   4212:                        }
                   4213:                }
1.1.1.24  root     4214:        }
                   4215:        return(false);
1.1.1.11  root     4216: }
                   4217: 
1.1.1.45  root     4218: bool msdos_is_con_path(const char *path)
1.1.1.8   root     4219: {
1.1.1.14  root     4220:        char full[MAX_PATH], *name;
1.1.1.8   root     4221:        
1.1.1.24  root     4222:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4223:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4224:        }
                   4225:        return(false);
                   4226: }
                   4227: 
1.1.1.45  root     4228: int msdos_is_comm_path(const char *path)
1.1.1.24  root     4229: {
                   4230:        char full[MAX_PATH], *name;
                   4231:        
                   4232:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4233:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4234:                        return(1);
                   4235:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4236:                        return(2);
                   4237:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4238:                        return(3);
                   4239:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4240:                        return(4);
1.1.1.24  root     4241:                }
                   4242:        }
1.1.1.29  root     4243:        return(0);
                   4244: }
                   4245: 
1.1.1.45  root     4246: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37  root     4247: {
                   4248:        // COM1:{110,150,300,600,1200,2400,4800,9600},{N,O,E,M,S},{8,7,6,5},{1,1.5,2}
1.1.1.45  root     4249:        const char *p = NULL;
1.1.1.37  root     4250:        
                   4251:        if((p = strstr(path, ":")) != NULL) {
                   4252:                UINT8 selector = sio_read(sio_port - 1, 3);
                   4253:                
                   4254:                // baud rate
                   4255:                int baud = max(110, min(9600, atoi(p + 1)));
                   4256:                UINT16 divisor = 115200 / baud;
                   4257:                
                   4258:                if((p = strstr(p + 1, ",")) != NULL) {
                   4259:                        // parity
                   4260:                        if(p[1] == 'N' || p[1] == 'n') {
                   4261:                                selector = (selector & ~0x38) | 0x00;
                   4262:                        } else if(p[1] == 'O' || p[1] == 'o') {
                   4263:                                selector = (selector & ~0x38) | 0x08;
                   4264:                        } else if(p[1] == 'E' || p[1] == 'e') {
                   4265:                                selector = (selector & ~0x38) | 0x18;
                   4266:                        } else if(p[1] == 'M' || p[1] == 'm') {
                   4267:                                selector = (selector & ~0x38) | 0x28;
                   4268:                        } else if(p[1] == 'S' || p[1] == 's') {
                   4269:                                selector = (selector & ~0x38) | 0x38;
                   4270:                        }
                   4271:                        if((p = strstr(p + 1, ",")) != NULL) {
                   4272:                                // word length
                   4273:                                if(p[1] == '8') {
                   4274:                                        selector = (selector & ~0x03) | 0x03;
                   4275:                                } else if(p[1] == '7') {
                   4276:                                        selector = (selector & ~0x03) | 0x02;
                   4277:                                } else if(p[1] == '6') {
                   4278:                                        selector = (selector & ~0x03) | 0x01;
                   4279:                                } else if(p[1] == '5') {
                   4280:                                        selector = (selector & ~0x03) | 0x00;
                   4281:                                }
                   4282:                                if((p = strstr(p + 1, ",")) != NULL) {
                   4283:                                        // stop bits
                   4284:                                        float bits = atof(p + 1);
                   4285:                                        if(bits > 1.0F) {
                   4286:                                                selector |= 0x04;
                   4287:                                        } else {
                   4288:                                                selector &= ~0x04;
                   4289:                                        }
                   4290:                                }
                   4291:                        }
                   4292:                }
                   4293:                sio_write(sio_port - 1, 3, selector | 0x80);
                   4294:                sio_write(sio_port - 1, 0, divisor & 0xff);
                   4295:                sio_write(sio_port - 1, 1, divisor >> 8);
                   4296:                sio_write(sio_port - 1, 3, selector);
                   4297:        }
                   4298: }
                   4299: 
1.1.1.45  root     4300: int msdos_is_prn_path(const char *path)
1.1.1.30  root     4301: {
                   4302:        char full[MAX_PATH], *name;
                   4303:        
                   4304:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4305:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4306:                        return(1);
                   4307:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4308:                        return(1);
                   4309:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4310:                        return(2);
                   4311:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4312:                        return(3);
                   4313:                }
                   4314:        }
                   4315:        return(0);
                   4316: }
                   4317: 
1.1.1.44  root     4318: bool msdos_is_valid_drive(int drv)
                   4319: {
                   4320:        return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
                   4321: }
                   4322: 
                   4323: bool msdos_is_removable_drive(int drv)
                   4324: {
                   4325:        char volume[] = "A:\\";
                   4326:        
                   4327:        volume[0] = 'A' + drv;
                   4328:        
                   4329:        return(GetDriveType(volume) == DRIVE_REMOVABLE);
                   4330: }
                   4331: 
                   4332: bool msdos_is_cdrom_drive(int drv)
                   4333: {
                   4334:        char volume[] = "A:\\";
                   4335:        
                   4336:        volume[0] = 'A' + drv;
                   4337:        
                   4338:        return(GetDriveType(volume) == DRIVE_CDROM);
                   4339: }
                   4340: 
                   4341: bool msdos_is_remote_drive(int drv)
                   4342: {
                   4343:        char volume[] = "A:\\";
                   4344:        
                   4345:        volume[0] = 'A' + drv;
                   4346:        
                   4347:        return(GetDriveType(volume) == DRIVE_REMOTE);
                   4348: }
                   4349: 
                   4350: bool msdos_is_subst_drive(int drv)
                   4351: {
                   4352:        char device[] = "A:", path[MAX_PATH];
                   4353:        
                   4354:        device[0] = 'A' + drv;
                   4355:        
                   4356:        if(QueryDosDevice(device, path, MAX_PATH)) {
                   4357:                if(strncmp(path, "\\??\\", 4) == 0) {
                   4358:                        return(true);
                   4359:                }
                   4360:        }
                   4361:        return(false);
                   4362: }
                   4363: 
1.1.1.45  root     4364: bool msdos_is_existing_file(const char *path)
1.1.1.24  root     4365: {
                   4366:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   4367:        WIN32_FIND_DATA FindData;
                   4368:        HANDLE hFind;
                   4369:        
                   4370:        if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
                   4371:                FindClose(hFind);
                   4372:                return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
                   4373:        }
                   4374:        return(false);
1.1.1.8   root     4375: }
                   4376: 
1.1.1.45  root     4377: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9   root     4378: {
                   4379:        static char tmp[MAX_PATH];
1.1.1.28  root     4380:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4381:        
1.1.1.28  root     4382:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.9   root     4383:        if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
                   4384:                sprintf(file_name, "COMMAND.COM");
                   4385:                if(_access(tmp, 0) == 0) {
                   4386:                        return(tmp);
                   4387:                }
                   4388:        }
1.1.1.28  root     4389:        
                   4390:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.9   root     4391:        if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
                   4392:                sprintf(file_name, "COMMAND.COM");
                   4393:                if(_access(tmp, 0) == 0) {
                   4394:                        return(tmp);
                   4395:                }
                   4396:        }
1.1.1.28  root     4397:        
                   4398:        // check if COMMAND.COM is in the current directory
1.1.1.9   root     4399:        if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
                   4400:                if(_access(tmp, 0) == 0) {
                   4401:                        return(tmp);
                   4402:                }
                   4403:        }
1.1.1.28  root     4404:        
                   4405:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4406:        strcpy(path, env_path);
                   4407:        char *token = my_strtok(path, ";");
1.1.1.9   root     4408:        while(token != NULL) {
1.1.1.14  root     4409:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4410:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4411:                        if(_access(tmp, 0) == 0) {
                   4412:                                return(tmp);
                   4413:                        }
                   4414:                }
                   4415:                token = my_strtok(NULL, ";");
                   4416:        }
                   4417:        return(NULL);
                   4418: }
                   4419: 
1.1.1.14  root     4420: int msdos_drive_number(const char *path)
1.1       root     4421: {
                   4422:        char tmp[MAX_PATH], *name;
                   4423:        
1.1.1.45  root     4424:        if(GetFullPathName(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
                   4425:                if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4426:                        return(tmp[0] - 'a');
                   4427:                } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
                   4428:                        return(tmp[0] - 'A');
                   4429:                }
1.1       root     4430:        }
1.1.1.45  root     4431: //     return(msdos_drive_number("."));
                   4432:        return(_getdrive() - 1);
1.1       root     4433: }
                   4434: 
1.1.1.45  root     4435: const char *msdos_volume_label(const char *path)
1.1       root     4436: {
                   4437:        static char tmp[MAX_PATH];
                   4438:        char volume[] = "A:\\";
                   4439:        
                   4440:        if(path[1] == ':') {
                   4441:                volume[0] = path[0];
                   4442:        } else {
                   4443:                volume[0] = 'A' + _getdrive() - 1;
                   4444:        }
                   4445:        if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
                   4446:                memset(tmp, 0, sizeof(tmp));
                   4447:        }
                   4448:        return(tmp);
                   4449: }
                   4450: 
1.1.1.45  root     4451: const char *msdos_short_volume_label(const char *label)
1.1       root     4452: {
                   4453:        static char tmp[(8 + 1 + 3) + 1];
1.1.1.45  root     4454:        const char *src = label;
1.1       root     4455:        int remain = strlen(label);
                   4456:        char *dst_n = tmp;
                   4457:        char *dst_e = tmp + 9;
                   4458:        
                   4459:        strcpy(tmp, "        .   ");
                   4460:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4461:                if(msdos_lead_byte_check(*src)) {
                   4462:                        if(++i == 8) {
                   4463:                                break;
                   4464:                        }
                   4465:                        *dst_n++ = *src++;
                   4466:                        remain--;
                   4467:                }
                   4468:                *dst_n++ = *src++;
                   4469:                remain--;
                   4470:        }
                   4471:        if(remain > 0) {
                   4472:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4473:                        if(msdos_lead_byte_check(*src)) {
                   4474:                                if(++i == 3) {
                   4475:                                        break;
                   4476:                                }
                   4477:                                *dst_e++ = *src++;
                   4478:                                remain--;
                   4479:                        }
                   4480:                        *dst_e++ = *src++;
                   4481:                        remain--;
                   4482:                }
                   4483:                *dst_e = '\0';
                   4484:        } else {
                   4485:                *dst_n = '\0';
                   4486:        }
                   4487:        my_strupr(tmp);
                   4488:        return(tmp);
                   4489: }
                   4490: 
1.1.1.13  root     4491: errno_t msdos_maperr(unsigned long oserrno)
                   4492: {
                   4493:        _doserrno = oserrno;
1.1.1.14  root     4494:        switch(oserrno) {
1.1.1.13  root     4495:        case ERROR_FILE_NOT_FOUND:         // 2
                   4496:        case ERROR_PATH_NOT_FOUND:         // 3
                   4497:        case ERROR_INVALID_DRIVE:          // 15
                   4498:        case ERROR_NO_MORE_FILES:          // 18
                   4499:        case ERROR_BAD_NETPATH:            // 53
                   4500:        case ERROR_BAD_NET_NAME:           // 67
                   4501:        case ERROR_BAD_PATHNAME:           // 161
                   4502:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4503:                return ENOENT;
                   4504:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4505:                return EMFILE;
                   4506:        case ERROR_ACCESS_DENIED:          // 5
                   4507:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4508:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4509:        case ERROR_CANNOT_MAKE:            // 82
                   4510:        case ERROR_FAIL_I24:               // 83
                   4511:        case ERROR_DRIVE_LOCKED:           // 108
                   4512:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4513:        case ERROR_NOT_LOCKED:             // 158
                   4514:        case ERROR_LOCK_FAILED:            // 167
                   4515:                return EACCES;
                   4516:        case ERROR_INVALID_HANDLE:         // 6
                   4517:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4518:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4519:                return EBADF;
                   4520:        case ERROR_ARENA_TRASHED:          // 7
                   4521:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4522:        case ERROR_INVALID_BLOCK:          // 9
                   4523:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4524:                return ENOMEM;
                   4525:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4526:                return E2BIG;
                   4527:        case ERROR_BAD_FORMAT:             // 11
                   4528:                return ENOEXEC;
                   4529:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4530:                return EXDEV;
                   4531:        case ERROR_FILE_EXISTS:            // 80
                   4532:        case ERROR_ALREADY_EXISTS:         // 183
                   4533:                return EEXIST;
                   4534:        case ERROR_NO_PROC_SLOTS:          // 89
                   4535:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4536:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4537:                return EAGAIN;
                   4538:        case ERROR_BROKEN_PIPE:            // 109
                   4539:                return EPIPE;
                   4540:        case ERROR_DISK_FULL:              // 112
                   4541:                return ENOSPC;
                   4542:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4543:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4544:                return ECHILD;
                   4545:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4546:                return ENOTEMPTY;
                   4547:        }
1.1.1.14  root     4548:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4549:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4550:                return EACCES;
                   4551:        }
1.1.1.14  root     4552:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4553:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4554:                return ENOEXEC;
                   4555:        }
                   4556:        return EINVAL;
                   4557: }
                   4558: 
1.1.1.45  root     4559: int msdos_open(const char *path, int oflag)
1.1.1.13  root     4560: {
1.1.1.14  root     4561:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45  root     4562:                return(_open(path, oflag));
1.1.1.13  root     4563:        }
1.1.1.14  root     4564:        
                   4565:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4566:        DWORD disposition;
1.1.1.14  root     4567:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4568:        default:
1.1.1.13  root     4569:        case _O_EXCL:
                   4570:                disposition = OPEN_EXISTING;
                   4571:                break;
                   4572:        case _O_CREAT:
                   4573:                disposition = OPEN_ALWAYS;
                   4574:                break;
                   4575:        case _O_CREAT | _O_EXCL:
                   4576:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4577:                disposition = CREATE_NEW;
                   4578:                break;
                   4579:        case _O_TRUNC:
                   4580:        case _O_TRUNC | _O_EXCL:
                   4581:                disposition = TRUNCATE_EXISTING;
                   4582:                break;
                   4583:        case _O_CREAT | _O_TRUNC:
                   4584:                disposition = CREATE_ALWAYS;
                   4585:                break;
                   4586:        }
1.1.1.14  root     4587:        
1.1.1.45  root     4588:        HANDLE h = CreateFile(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
1.1.1.13  root     4589:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4590:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4591:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4592:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4593:                // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.45  root     4594:                h = CreateFile(path, GENERIC_READ,
1.1.1.13  root     4595:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4596:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4597:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4598:                        errno = msdos_maperr(GetLastError());
1.1.1.45  root     4599:                        return(-1);
1.1.1.13  root     4600:                }
                   4601:        }
1.1.1.14  root     4602:        
1.1.1.13  root     4603:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     4604:        if(fd == -1) {
1.1.1.13  root     4605:                CloseHandle(h);
                   4606:        }
1.1.1.45  root     4607:        return(fd);
                   4608: }
                   4609: 
                   4610: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
                   4611: {
                   4612:        int fd = -1;
                   4613:        
                   4614:        *sio_port = *lpt_port = 0;
                   4615:        
                   4616:        if(msdos_is_con_path(path)) {
                   4617:                // MODE.COM opens CON device with read/write mode :-(
                   4618:                if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
                   4619:                        oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
                   4620:                        oflag |= _O_RDONLY;
                   4621:                }
                   4622:                if((fd = msdos_open("CON", oflag)) == -1) {
                   4623: //                     fd = msdos_open("NUL", oflag);
                   4624:                }
                   4625:        } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
                   4626:                fd = msdos_open("NUL", oflag);
                   4627:                msdos_set_comm_params(*sio_port, path);
                   4628:        } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
                   4629:                fd = msdos_open("NUL", oflag);
                   4630:        } else if(msdos_is_device_path(path)) {
                   4631:                fd = msdos_open("NUL", oflag);
                   4632: //     } else if(oflag & _O_CREAT) {
                   4633: //             fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
                   4634: //     } else {
                   4635: //             fd = _open(path, oflag);
                   4636:        }
                   4637:        return(fd);
                   4638: }
                   4639: 
                   4640: UINT16 msdos_device_info(const char *path)
                   4641: {
                   4642:        if(msdos_is_con_path(path)) {
                   4643:                return(0x80d3);
                   4644:        } else if(msdos_is_comm_path(path)) {
                   4645:                return(0x80a0);
                   4646:        } else if(msdos_is_prn_path(path)) {
                   4647: //             return(0xa8c0);
                   4648:                return(0x80a0);
                   4649:        } else if(msdos_is_device_path(path)) {
                   4650:                if(strstr(path, "EMMXXXX0") != NULL) {
                   4651:                        return(0xc0c0);
                   4652:                } else if(strstr(path, "MSCD001") != NULL) {
                   4653:                        return(0xc880);
                   4654:                } else {
                   4655:                        return(0x8084);
                   4656:                }
                   4657:        } else {
                   4658:                return(msdos_drive_number(path));
                   4659:        }
1.1.1.13  root     4660: }
                   4661: 
1.1.1.37  root     4662: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg, int sio_port, int lpt_port)
1.1       root     4663: {
                   4664:        static int id = 0;
                   4665:        char full[MAX_PATH], *name;
                   4666:        
                   4667:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4668:                strcpy(file_handler[fd].path, full);
                   4669:        } else {
                   4670:                strcpy(file_handler[fd].path, path);
                   4671:        }
1.1.1.14  root     4672:        // isatty makes no distinction between CON & NUL
                   4673:        // GetFileSize fails on CON, succeeds on NUL
                   4674:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45  root     4675:                if(info == 0x80d3) {
                   4676:                        info = 0x8084;
                   4677:                }
1.1.1.14  root     4678:                atty = 0;
                   4679:        } else if(!atty && info == 0x80d3) {
1.1.1.45  root     4680: //             info = msdos_drive_number(".");
                   4681:                info = msdos_drive_number(path);
1.1.1.14  root     4682:        }
1.1       root     4683:        file_handler[fd].valid = 1;
                   4684:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37  root     4685:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     4686:        file_handler[fd].mode = mode;
                   4687:        file_handler[fd].info = info;
                   4688:        file_handler[fd].psp = psp_seg;
1.1.1.37  root     4689:        file_handler[fd].sio_port = sio_port;
                   4690:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     4691:        
                   4692:        // init system file table
                   4693:        if(fd < 20) {
                   4694:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   4695:                
                   4696:                memset(sft, 0, 0x3b);
                   4697:                
                   4698:                *(UINT16 *)(sft + 0x00) = 1;
                   4699:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
                   4700:                *(UINT8  *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
                   4701:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   4702:                
                   4703:                if(!(file_handler[fd].info & 0x80)) {
                   4704:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   4705:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   4706:                        
                   4707:                        FILETIME time, local;
                   4708:                        HANDLE hHandle;
                   4709:                        WORD dos_date = 0, dos_time = 0;
                   4710:                        DWORD file_size = 0;
                   4711:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   4712:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   4713:                                        FileTimeToLocalFileTime(&time, &local);
                   4714:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   4715:                                }
                   4716:                                file_size = GetFileSize(hHandle, NULL);
                   4717:                        }
                   4718:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   4719:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   4720:                        *(UINT32 *)(sft + 0x11) = file_size;
                   4721:                }
                   4722:                
                   4723:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   4724:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   4725:                my_strupr(fname);
                   4726:                my_strupr(ext);
                   4727:                memset(sft + 0x20, 0x20, 11);
                   4728:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   4729:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   4730:                
                   4731:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   4732:        }
1.1       root     4733: }
                   4734: 
1.1.1.37  root     4735: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg)
                   4736: {
                   4737:         msdos_file_handler_open(fd, path, atty, mode, info, psp_seg, 0, 0);
                   4738: }
                   4739: 
1.1       root     4740: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   4741: {
                   4742:        strcpy(file_handler[dst].path, file_handler[src].path);
                   4743:        file_handler[dst].valid = 1;
                   4744:        file_handler[dst].id = file_handler[src].id;
                   4745:        file_handler[dst].atty = file_handler[src].atty;
                   4746:        file_handler[dst].mode = file_handler[src].mode;
                   4747:        file_handler[dst].info = file_handler[src].info;
                   4748:        file_handler[dst].psp = psp_seg;
1.1.1.37  root     4749:        file_handler[dst].sio_port = file_handler[src].sio_port;
                   4750:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     4751: }
                   4752: 
1.1.1.20  root     4753: void msdos_file_handler_close(int fd)
1.1       root     4754: {
                   4755:        file_handler[fd].valid = 0;
1.1.1.21  root     4756:        
                   4757:        if(fd < 20) {
                   4758:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   4759:        }
1.1       root     4760: }
                   4761: 
1.1.1.14  root     4762: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     4763: {
1.1.1.14  root     4764:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   4765:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   4766:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     4767: }
                   4768: 
                   4769: // find file
                   4770: 
                   4771: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   4772: {
                   4773:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4774:                return(0);      // search directory only !!!
                   4775:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   4776:                return(0);
                   4777:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   4778:                return(0);
                   4779:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4780:                return(0);
                   4781:        } else if((attribute & required_mask) != required_mask) {
                   4782:                return(0);
                   4783:        } else {
                   4784:                return(1);
                   4785:        }
                   4786: }
                   4787: 
1.1.1.13  root     4788: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
                   4789: {
1.1.1.14  root     4790:        if(fd->cAlternateFileName[0]) {
1.1.1.42  root     4791:                return(1);
1.1.1.13  root     4792:        }
                   4793:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     4794:        if(len > 12) {
1.1.1.42  root     4795:                return(0);
1.1.1.13  root     4796:        }
                   4797:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     4798:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42  root     4799:                return(0);
1.1.1.13  root     4800:        }
1.1.1.42  root     4801:        return(1);
1.1.1.13  root     4802: }
                   4803: 
1.1       root     4804: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
                   4805: {
                   4806:        FILETIME local;
                   4807:        
                   4808:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   4809:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   4810:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   4811:        
                   4812:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   4813:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   4814:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   4815:        
                   4816:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   4817:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   4818:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   4819: }
                   4820: 
                   4821: // i/o
                   4822: 
                   4823: void msdos_stdio_reopen()
                   4824: {
                   4825:        if(!file_handler[0].valid) {
                   4826:                _dup2(DUP_STDIN, 0);
                   4827:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   4828:        }
                   4829:        if(!file_handler[1].valid) {
                   4830:                _dup2(DUP_STDOUT, 1);
                   4831:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   4832:        }
                   4833:        if(!file_handler[2].valid) {
                   4834:                _dup2(DUP_STDERR, 2);
                   4835:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   4836:        }
1.1.1.21  root     4837:        if(!file_handler[3].valid) {
                   4838:                _dup2(DUP_STDAUX, 3);
                   4839:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   4840:        }
                   4841:        if(!file_handler[4].valid) {
                   4842:                _dup2(DUP_STDPRN, 4);
1.1.1.45  root     4843: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   4844:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     4845:        }
                   4846:        for(int i = 0; i < 5; i++) {
                   4847:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   4848:                        msdos_psp_set_file_table(i, i, current_psp);
                   4849:                }
                   4850:        }
1.1       root     4851: }
                   4852: 
1.1.1.37  root     4853: int msdos_read(int fd, void *buffer, unsigned int count)
                   4854: {
                   4855:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   4856:                // read from serial port
                   4857:                int read = 0;
                   4858:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   4859:                        UINT8 *buf = (UINT8 *)buffer;
                   4860:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   4861:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     4862:                        DWORD timeout = timeGetTime() + 1000;
                   4863:                        while(read < count) {
                   4864:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
                   4865:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
                   4866:                                        timeout = timeGetTime() + 1000;
                   4867:                                } else {
                   4868:                                        if(timeGetTime() > timeout) {
                   4869:                                                break;
                   4870:                                        }
                   4871:                                        Sleep(10);
1.1.1.37  root     4872:                                }
                   4873:                        }
                   4874:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   4875:                }
                   4876:                return(read);
                   4877:        }
                   4878:        return(_read(fd, buffer, count));
                   4879: }
                   4880: 
1.1       root     4881: int msdos_kbhit()
                   4882: {
                   4883:        msdos_stdio_reopen();
                   4884:        
1.1.1.20  root     4885:        process_t *process = msdos_process_info_get(current_psp);
                   4886:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4887:        
                   4888:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4889:                // stdin is redirected to file
1.1.1.20  root     4890:                return(eof(fd) == 0);
1.1       root     4891:        }
                   4892:        
                   4893:        // check keyboard status
1.1.1.35  root     4894:        if(key_recv != 0) {
1.1       root     4895:                return(1);
                   4896:        }
1.1.1.35  root     4897:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4898: #ifdef USE_SERVICE_THREAD
                   4899:                EnterCriticalSection(&key_buf_crit_sect);
                   4900: #endif
                   4901:                bool empty = key_buf_char->empty();
                   4902: #ifdef USE_SERVICE_THREAD
                   4903:                LeaveCriticalSection(&key_buf_crit_sect);
                   4904: #endif
                   4905:                if(!empty) return(1);
                   4906:        }
                   4907:        return(_kbhit());
1.1       root     4908: }
                   4909: 
                   4910: int msdos_getch_ex(int echo)
                   4911: {
                   4912:        static char prev = 0;
                   4913:        
                   4914:        msdos_stdio_reopen();
                   4915:        
1.1.1.20  root     4916:        process_t *process = msdos_process_info_get(current_psp);
                   4917:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4918:        
                   4919:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4920:                // stdin is redirected to file
                   4921: retry:
                   4922:                char data;
1.1.1.37  root     4923:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     4924:                        char tmp = data;
                   4925:                        if(data == 0x0a) {
                   4926:                                if(prev == 0x0d) {
                   4927:                                        goto retry; // CRLF -> skip LF
                   4928:                                } else {
                   4929:                                        data = 0x0d; // LF only -> CR
                   4930:                                }
                   4931:                        }
                   4932:                        prev = tmp;
                   4933:                        return(data);
                   4934:                }
                   4935:                return(EOF);
                   4936:        }
                   4937:        
                   4938:        // input from console
1.1.1.5   root     4939:        int key_char, key_scan;
1.1.1.33  root     4940:        if(key_recv != 0) {
1.1.1.5   root     4941:                key_char = (key_code >> 0) & 0xff;
                   4942:                key_scan = (key_code >> 8) & 0xff;
                   4943:                key_code >>= 16;
1.1.1.33  root     4944:                key_recv >>= 16;
1.1.1.5   root     4945:        } else {
1.1.1.35  root     4946:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_halted) {
                   4947:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4948: #ifdef USE_SERVICE_THREAD
                   4949:                                EnterCriticalSection(&key_buf_crit_sect);
                   4950: #endif
                   4951:                                bool empty = key_buf_char->empty();
                   4952: #ifdef USE_SERVICE_THREAD
                   4953:                                LeaveCriticalSection(&key_buf_crit_sect);
                   4954: #endif
                   4955:                                if(!empty) break;
                   4956:                        }
1.1.1.23  root     4957:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   4958:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   4959:                                if(_kbhit()) {
1.1.1.32  root     4960:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     4961: #ifdef USE_SERVICE_THREAD
                   4962:                                                EnterCriticalSection(&key_buf_crit_sect);
                   4963: #endif
1.1.1.32  root     4964:                                                key_buf_char->write(_getch());
1.1.1.35  root     4965:                                                key_buf_scan->write(0x00);
                   4966: #ifdef USE_SERVICE_THREAD
                   4967:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   4968: #endif
1.1.1.32  root     4969:                                        }
1.1.1.23  root     4970:                                } else {
                   4971:                                        Sleep(10);
                   4972:                                }
                   4973:                        } else {
                   4974:                                if(!update_key_buffer()) {
                   4975:                                        Sleep(10);
                   4976:                                }
1.1.1.14  root     4977:                        }
                   4978:                }
                   4979:                if(m_halted) {
1.1.1.33  root     4980:                        // insert CR to terminate input loops
1.1.1.14  root     4981:                        key_char = 0x0d;
                   4982:                        key_scan = 0;
1.1.1.32  root     4983:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     4984: #ifdef USE_SERVICE_THREAD
                   4985:                        EnterCriticalSection(&key_buf_crit_sect);
                   4986: #endif
1.1.1.14  root     4987:                        key_char = key_buf_char->read();
                   4988:                        key_scan = key_buf_scan->read();
1.1.1.41  root     4989:                        // write to bottom of key buffer
                   4990:                        mem[0x43c] = (UINT8)key_char;
                   4991:                        mem[0x43d] = (UINT8)key_scan;
1.1.1.35  root     4992: #ifdef USE_SERVICE_THREAD
                   4993:                        LeaveCriticalSection(&key_buf_crit_sect);
                   4994: #endif
1.1.1.5   root     4995:                }
1.1       root     4996:        }
                   4997:        if(echo && key_char) {
                   4998:                msdos_putch(key_char);
                   4999:        }
                   5000:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   5001: }
                   5002: 
                   5003: inline int msdos_getch()
                   5004: {
                   5005:        return(msdos_getch_ex(0));
                   5006: }
                   5007: 
                   5008: inline int msdos_getche()
                   5009: {
                   5010:        return(msdos_getch_ex(1));
                   5011: }
                   5012: 
                   5013: int msdos_write(int fd, const void *buffer, unsigned int count)
                   5014: {
1.1.1.37  root     5015:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5016:                // write to serial port
1.1.1.38  root     5017:                int written = 0;
1.1.1.37  root     5018:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5019:                        UINT8 *buf = (UINT8 *)buffer;
                   5020:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5021:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5022:                        DWORD timeout = timeGetTime() + 1000;
                   5023:                        while(written < count) {
                   5024:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
                   5025:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
                   5026:                                        timeout = timeGetTime() + 1000;
                   5027:                                } else {
                   5028:                                        if(timeGetTime() > timeout) {
                   5029:                                                break;
                   5030:                                        }
                   5031:                                        Sleep(10);
                   5032:                                }
1.1.1.37  root     5033:                        }
                   5034:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5035:                }
1.1.1.38  root     5036:                return(written);
1.1.1.37  root     5037:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   5038:                // write to printer port
                   5039:                UINT8 *buf = (UINT8 *)buffer;
                   5040:                for(unsigned int i = 0; i < count; i++) {
                   5041: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5042:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5043:                }
                   5044:                return(count);
                   5045:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     5046:                // CR+LF -> LF
1.1.1.37  root     5047:                static int is_cr = 0;
1.1       root     5048:                UINT8 *buf = (UINT8 *)buffer;
                   5049:                for(unsigned int i = 0; i < count; i++) {
                   5050:                        UINT8 data = buf[i];
                   5051:                        if(is_cr) {
                   5052:                                if(data != 0x0a) {
                   5053:                                        UINT8 tmp = 0x0d;
                   5054:                                        _write(1, &tmp, 1);
                   5055:                                }
                   5056:                                _write(1, &data, 1);
                   5057:                                is_cr = 0;
                   5058:                        } else if(data == 0x0d) {
                   5059:                                is_cr = 1;
                   5060:                        } else {
                   5061:                                _write(1, &data, 1);
                   5062:                        }
                   5063:                }
                   5064:                return(count);
                   5065:        }
1.1.1.14  root     5066:        vram_flush();
1.1       root     5067:        return(_write(fd, buffer, count));
                   5068: }
                   5069: 
                   5070: void msdos_putch(UINT8 data)
1.1.1.35  root     5071: #ifdef USE_SERVICE_THREAD
                   5072: {
                   5073:        EnterCriticalSection(&putch_crit_sect);
                   5074:        msdos_putch_tmp(data);
                   5075:        LeaveCriticalSection(&putch_crit_sect);
                   5076: }
                   5077: void msdos_putch_tmp(UINT8 data)
                   5078: #endif
1.1       root     5079: {
1.1.1.34  root     5080:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5081:        SMALL_RECT rect;
                   5082:        COORD co;
1.1       root     5083:        static int p = 0;
                   5084:        static int is_kanji = 0;
                   5085:        static int is_esc = 0;
                   5086:        static int stored_x;
                   5087:        static int stored_y;
                   5088:        static WORD stored_a;
1.1.1.20  root     5089:        static char tmp[64], out[64];
1.1       root     5090:        
                   5091:        msdos_stdio_reopen();
                   5092:        
1.1.1.20  root     5093:        process_t *process = msdos_process_info_get(current_psp);
                   5094:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5095:        
                   5096:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5097:                // stdout is redirected to file
1.1.1.20  root     5098:                msdos_write(fd, &data, 1);
1.1       root     5099:                return;
                   5100:        }
1.1.1.23  root     5101:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     5102:        
                   5103:        // output to console
                   5104:        tmp[p++] = data;
                   5105:        
1.1.1.14  root     5106:        vram_flush();
                   5107:        
1.1       root     5108:        if(is_kanji) {
                   5109:                // kanji character
                   5110:                is_kanji = 0;
                   5111:        } else if(is_esc) {
                   5112:                // escape sequense
                   5113:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   5114:                        p = is_esc = 0;
                   5115:                } else if(tmp[1] == '=' && p == 4) {
                   5116:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     5117:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     5118:                        SetConsoleCursorPosition(hStdout, co);
                   5119:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5120:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     5121:                        cursor_moved = false;
                   5122:                        p = is_esc = 0;
                   5123:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
                   5124:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5125:                        co.X = csbi.dwCursorPosition.X;
                   5126:                        co.Y = csbi.dwCursorPosition.Y;
                   5127:                        WORD wAttributes = csbi.wAttributes;
                   5128:                        
                   5129:                        if(tmp[1] == 'D') {
                   5130:                                co.Y++;
                   5131:                        } else if(tmp[1] == 'E') {
                   5132:                                co.X = 0;
                   5133:                                co.Y++;
                   5134:                        } else if(tmp[1] == 'M') {
                   5135:                                co.Y--;
                   5136:                        } else if(tmp[1] == '*') {
1.1.1.14  root     5137:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5138:                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5139:                                co.X = 0;
                   5140:                                co.Y = csbi.srWindow.Top;
1.1       root     5141:                        } else if(tmp[1] == '[') {
                   5142:                                int param[256], params = 0;
                   5143:                                memset(param, 0, sizeof(param));
                   5144:                                for(int i = 2; i < p; i++) {
                   5145:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   5146:                                                param[params] *= 10;
                   5147:                                                param[params] += tmp[i] - '0';
                   5148:                                        } else {
                   5149:                                                params++;
                   5150:                                        }
                   5151:                                }
                   5152:                                if(data == 'A') {
1.1.1.14  root     5153:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     5154:                                } else if(data == 'B') {
1.1.1.14  root     5155:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     5156:                                } else if(data == 'C') {
1.1.1.14  root     5157:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     5158:                                } else if(data == 'D') {
1.1.1.14  root     5159:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     5160:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     5161:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   5162:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     5163:                                } else if(data == 'J') {
1.1.1.14  root     5164:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5165:                                        if(param[0] == 0) {
                   5166:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5167:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5168:                                                if(co.Y < csbi.srWindow.Bottom) {
                   5169:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5170:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5171:                                                }
                   5172:                                        } else if(param[0] == 1) {
1.1.1.14  root     5173:                                                if(co.Y > csbi.srWindow.Top) {
                   5174:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
                   5175:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5176:                                                }
                   5177:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5178:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5179:                                        } else if(param[0] == 2) {
1.1.1.14  root     5180:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5181:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5182:                                                co.X = co.Y = 0;
                   5183:                                        }
                   5184:                                } else if(data == 'K') {
1.1.1.14  root     5185:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5186:                                        if(param[0] == 0) {
                   5187:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5188:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5189:                                        } else if(param[0] == 1) {
                   5190:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5191:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5192:                                        } else if(param[0] == 2) {
                   5193:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5194:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5195:                                        }
                   5196:                                } else if(data == 'L') {
1.1.1.14  root     5197:                                        if(params == 0) {
                   5198:                                                param[0] = 1;
1.1       root     5199:                                        }
1.1.1.14  root     5200:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5201:                                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5202:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5203:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5204:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5205:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14  root     5206:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5207:                                        co.X = 0;
                   5208:                                } else if(data == 'M') {
1.1.1.14  root     5209:                                        if(params == 0) {
                   5210:                                                param[0] = 1;
                   5211:                                        }
                   5212:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5213:                                                clear_scr_buffer(csbi.wAttributes);
                   5214:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5215:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5216:                                        } else {
1.1.1.14  root     5217:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5218:                                                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5219:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5220:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5221:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5222:                                        }
                   5223:                                        co.X = 0;
                   5224:                                } else if(data == 'h') {
                   5225:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5226:                                                CONSOLE_CURSOR_INFO cur;
                   5227:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5228:                                                if(cur.bVisible) {
                   5229:                                                        cur.bVisible = FALSE;
1.1.1.14  root     5230: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5231:                                                }
                   5232:                                        }
                   5233:                                } else if(data == 'l') {
                   5234:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5235:                                                CONSOLE_CURSOR_INFO cur;
                   5236:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5237:                                                if(!cur.bVisible) {
                   5238:                                                        cur.bVisible = TRUE;
1.1.1.14  root     5239: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5240:                                                }
                   5241:                                        }
                   5242:                                } else if(data == 'm') {
                   5243:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5244:                                        int reverse = 0, hidden = 0;
                   5245:                                        for(int i = 0; i < params; i++) {
                   5246:                                                if(param[i] == 1) {
                   5247:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5248:                                                } else if(param[i] == 4) {
                   5249:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5250:                                                } else if(param[i] == 7) {
                   5251:                                                        reverse = 1;
                   5252:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5253:                                                        hidden = 1;
                   5254:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5255:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5256:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5257:                                                                param[i] -= 16;
                   5258:                                                        } else {
                   5259:                                                                param[i] -= 30;
                   5260:                                                        }
                   5261:                                                        if(param[i] & 1) {
                   5262:                                                                wAttributes |= FOREGROUND_RED;
                   5263:                                                        }
                   5264:                                                        if(param[i] & 2) {
                   5265:                                                                wAttributes |= FOREGROUND_GREEN;
                   5266:                                                        }
                   5267:                                                        if(param[i] & 4) {
                   5268:                                                                wAttributes |= FOREGROUND_BLUE;
                   5269:                                                        }
                   5270:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5271:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5272:                                                        if((param[i] - 40) & 1) {
                   5273:                                                                wAttributes |= BACKGROUND_RED;
                   5274:                                                        }
                   5275:                                                        if((param[i] - 40) & 2) {
                   5276:                                                                wAttributes |= BACKGROUND_GREEN;
                   5277:                                                        }
                   5278:                                                        if((param[i] - 40) & 4) {
                   5279:                                                                wAttributes |= BACKGROUND_BLUE;
                   5280:                                                        }
                   5281:                                                }
                   5282:                                        }
                   5283:                                        if(reverse) {
                   5284:                                                wAttributes &= ~0xff;
                   5285:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5286:                                        }
                   5287:                                        if(hidden) {
                   5288:                                                wAttributes &= ~0x0f;
                   5289:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5290:                                        }
                   5291:                                } else if(data == 'n') {
                   5292:                                        if(param[0] == 6) {
                   5293:                                                char tmp[16];
                   5294:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5295:                                                int len = strlen(tmp);
1.1.1.32  root     5296:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5297: #ifdef USE_SERVICE_THREAD
                   5298:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5299: #endif
1.1.1.32  root     5300:                                                        for(int i = 0; i < len; i++) {
                   5301:                                                                key_buf_char->write(tmp[i]);
                   5302:                                                                key_buf_scan->write(0x00);
                   5303:                                                        }
1.1.1.35  root     5304: #ifdef USE_SERVICE_THREAD
                   5305:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5306: #endif
1.1       root     5307:                                                }
                   5308:                                        }
                   5309:                                } else if(data == 's') {
                   5310:                                        stored_x = co.X;
                   5311:                                        stored_y = co.Y;
                   5312:                                        stored_a = wAttributes;
                   5313:                                } else if(data == 'u') {
                   5314:                                        co.X = stored_x;
                   5315:                                        co.Y = stored_y;
                   5316:                                        wAttributes = stored_a;
                   5317:                                }
                   5318:                        }
                   5319:                        if(co.X < 0) {
                   5320:                                co.X = 0;
                   5321:                        } else if(co.X >= csbi.dwSize.X) {
                   5322:                                co.X = csbi.dwSize.X - 1;
                   5323:                        }
1.1.1.14  root     5324:                        if(co.Y < csbi.srWindow.Top) {
                   5325:                                co.Y = csbi.srWindow.Top;
                   5326:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5327:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5328:                        }
                   5329:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5330:                                SetConsoleCursorPosition(hStdout, co);
                   5331:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5332:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5333:                                cursor_moved = false;
                   5334:                        }
                   5335:                        if(wAttributes != csbi.wAttributes) {
                   5336:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5337:                        }
                   5338:                        p = is_esc = 0;
                   5339:                }
                   5340:                return;
                   5341:        } else {
                   5342:                if(msdos_lead_byte_check(data)) {
                   5343:                        is_kanji = 1;
                   5344:                        return;
                   5345:                } else if(data == 0x1b) {
                   5346:                        is_esc = 1;
                   5347:                        return;
                   5348:                }
                   5349:        }
1.1.1.20  root     5350:        
                   5351:        DWORD q = 0, num;
                   5352:        is_kanji = 0;
                   5353:        for(int i = 0; i < p; i++) {
                   5354:                UINT8 c = tmp[i];
                   5355:                if(is_kanji) {
                   5356:                        is_kanji = 0;
                   5357:                } else if(msdos_lead_byte_check(data)) {
                   5358:                        is_kanji = 1;
                   5359:                } else if(msdos_ctrl_code_check(data)) {
                   5360:                        out[q++] = '^';
                   5361:                        c += 'A' - 1;
                   5362:                }
                   5363:                out[q++] = c;
                   5364:        }
1.1.1.34  root     5365:        if(q == 1 && out[0] == 0x08) {
                   5366:                // back space
                   5367:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5368:                if(csbi.dwCursorPosition.X > 0) {
                   5369:                        co.X = csbi.dwCursorPosition.X - 1;
                   5370:                        co.Y = csbi.dwCursorPosition.Y;
                   5371:                        SetConsoleCursorPosition(hStdout, co);
                   5372:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5373:                        co.X = csbi.dwSize.X - 1;
                   5374:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5375:                        SetConsoleCursorPosition(hStdout, co);
                   5376:                } else {
                   5377:                        WriteConsole(hStdout, out, q, &num, NULL); // to make sure
                   5378:                }
                   5379:        } else {
                   5380:                WriteConsole(hStdout, out, q, &num, NULL);
                   5381:        }
1.1       root     5382:        p = 0;
1.1.1.14  root     5383:        
1.1.1.15  root     5384:        if(!restore_console_on_exit) {
                   5385:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5386:                scr_top = csbi.srWindow.Top;
                   5387:        }
1.1       root     5388:        cursor_moved = true;
                   5389: }
                   5390: 
                   5391: int msdos_aux_in()
                   5392: {
1.1.1.21  root     5393:        msdos_stdio_reopen();
                   5394:        
1.1.1.20  root     5395:        process_t *process = msdos_process_info_get(current_psp);
                   5396:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5397:        
                   5398:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5399:                char data = 0;
1.1.1.37  root     5400:                msdos_read(fd, &data, 1);
1.1       root     5401:                return(data);
                   5402:        } else {
                   5403:                return(EOF);
                   5404:        }
                   5405: }
                   5406: 
                   5407: void msdos_aux_out(char data)
                   5408: {
1.1.1.21  root     5409:        msdos_stdio_reopen();
                   5410:        
1.1.1.20  root     5411:        process_t *process = msdos_process_info_get(current_psp);
                   5412:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5413:        
                   5414:        if(fd < process->max_files && file_handler[fd].valid) {
                   5415:                msdos_write(fd, &data, 1);
1.1       root     5416:        }
                   5417: }
                   5418: 
                   5419: void msdos_prn_out(char data)
                   5420: {
1.1.1.21  root     5421:        msdos_stdio_reopen();
                   5422:        
1.1.1.20  root     5423:        process_t *process = msdos_process_info_get(current_psp);
                   5424:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5425:        
                   5426:        if(fd < process->max_files && file_handler[fd].valid) {
                   5427:                msdos_write(fd, &data, 1);
1.1       root     5428:        }
                   5429: }
                   5430: 
                   5431: // memory control
                   5432: 
1.1.1.45  root     5433: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name)
1.1       root     5434: {
                   5435:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5436:        
                   5437:        mcb->mz = mz;
                   5438:        mcb->psp = psp;
1.1.1.30  root     5439:        mcb->paragraphs = paragraphs;
1.1.1.39  root     5440:        
                   5441:        if(prog_name != NULL) {
                   5442:                memset(mcb->prog_name, 0, 8);
                   5443:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
                   5444:        }
1.1       root     5445:        return(mcb);
                   5446: }
                   5447: 
1.1.1.39  root     5448: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs)
                   5449: {
                   5450:        return(msdos_mcb_create(mcb_seg, mz, psp, paragraphs, NULL));
                   5451: }
                   5452: 
1.1       root     5453: void msdos_mcb_check(mcb_t *mcb)
                   5454: {
                   5455:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5456:                #if 0
                   5457:                        // shutdown now !!!
                   5458:                        fatalerror("broken memory control block\n");
                   5459:                #else
                   5460:                        // return error code and continue
                   5461:                        throw(0x07); // broken memory control block
                   5462:                #endif
1.1       root     5463:        }
                   5464: }
                   5465: 
1.1.1.39  root     5466: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5467: {
                   5468:        int mcb_seg = seg - 1;
                   5469:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5470:        msdos_mcb_check(mcb);
                   5471:        
1.1.1.30  root     5472:        if(mcb->paragraphs > paragraphs) {
1.1       root     5473:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5474:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5475:                
                   5476:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5477:                mcb->mz = 'M';
1.1.1.30  root     5478:                mcb->paragraphs = paragraphs;
1.1       root     5479:        }
                   5480: }
                   5481: 
                   5482: void msdos_mem_merge(int seg)
                   5483: {
                   5484:        int mcb_seg = seg - 1;
                   5485:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5486:        msdos_mcb_check(mcb);
                   5487:        
                   5488:        while(1) {
                   5489:                if(mcb->mz == 'Z') {
                   5490:                        break;
                   5491:                }
1.1.1.30  root     5492:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5493:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5494:                msdos_mcb_check(next_mcb);
                   5495:                
                   5496:                if(next_mcb->psp != 0) {
                   5497:                        break;
                   5498:                }
                   5499:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5500:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5501:        }
                   5502: }
                   5503: 
1.1.1.8   root     5504: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5505: {
                   5506:        while(1) {
                   5507:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5508:                bool last_block;
1.1       root     5509:                
1.1.1.14  root     5510:                if(mcb->psp == 0) {
                   5511:                        msdos_mem_merge(mcb_seg + 1);
                   5512:                } else {
                   5513:                        msdos_mcb_check(mcb);
                   5514:                }
1.1.1.33  root     5515:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5516:                        // check if the next is dummy mcb to link to umb
                   5517:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5518:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5519:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5520:                }
                   5521:                if(!(new_process && !last_block)) {
1.1.1.30  root     5522:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5523:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5524:                                mcb->psp = current_psp;
                   5525:                                return(mcb_seg + 1);
                   5526:                        }
                   5527:                }
                   5528:                if(mcb->mz == 'Z') {
                   5529:                        break;
                   5530:                }
1.1.1.30  root     5531:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5532:        }
                   5533:        return(-1);
                   5534: }
                   5535: 
                   5536: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5537: {
                   5538:        int mcb_seg = seg - 1;
                   5539:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5540:        msdos_mcb_check(mcb);
1.1.1.30  root     5541:        int current_paragraphs = mcb->paragraphs;
1.1       root     5542:        
                   5543:        msdos_mem_merge(seg);
1.1.1.30  root     5544:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5545:                if(max_paragraphs) {
1.1.1.30  root     5546:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5547:                }
1.1       root     5548:                msdos_mem_split(seg, current_paragraphs);
                   5549:                return(-1);
                   5550:        }
                   5551:        msdos_mem_split(seg, paragraphs);
                   5552:        return(0);
                   5553: }
                   5554: 
                   5555: void msdos_mem_free(int seg)
                   5556: {
                   5557:        int mcb_seg = seg - 1;
                   5558:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5559:        msdos_mcb_check(mcb);
                   5560:        
                   5561:        mcb->psp = 0;
                   5562:        msdos_mem_merge(seg);
                   5563: }
                   5564: 
1.1.1.8   root     5565: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5566: {
                   5567:        int max_paragraphs = 0;
                   5568:        
                   5569:        while(1) {
                   5570:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5571:                bool last_block;
                   5572:                
1.1       root     5573:                msdos_mcb_check(mcb);
                   5574:                
1.1.1.33  root     5575:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5576:                        // check if the next is dummy mcb to link to umb
                   5577:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5578:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5579:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5580:                }
                   5581:                if(!(new_process && !last_block)) {
1.1.1.30  root     5582:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   5583:                                max_paragraphs = mcb->paragraphs;
1.1       root     5584:                        }
                   5585:                }
                   5586:                if(mcb->mz == 'Z') {
                   5587:                        break;
                   5588:                }
1.1.1.30  root     5589:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5590:        }
1.1.1.14  root     5591:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     5592: }
                   5593: 
1.1.1.8   root     5594: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   5595: {
                   5596:        int last_seg = -1;
                   5597:        
                   5598:        while(1) {
                   5599:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5600:                msdos_mcb_check(mcb);
                   5601:                
1.1.1.14  root     5602:                if(mcb->psp == psp) {
1.1.1.8   root     5603:                        last_seg = mcb_seg;
                   5604:                }
1.1.1.14  root     5605:                if(mcb->mz == 'Z') {
                   5606:                        break;
                   5607:                }
1.1.1.30  root     5608:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     5609:        }
                   5610:        return(last_seg);
                   5611: }
                   5612: 
1.1.1.19  root     5613: int msdos_mem_get_umb_linked()
                   5614: {
1.1.1.33  root     5615:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5616:        msdos_mcb_check(mcb);
1.1.1.19  root     5617:        
1.1.1.33  root     5618:        if(mcb->mz == 'M') {
                   5619:                return(-1);
1.1.1.19  root     5620:        }
                   5621:        return(0);
                   5622: }
                   5623: 
1.1.1.33  root     5624: void msdos_mem_link_umb()
1.1.1.19  root     5625: {
1.1.1.33  root     5626:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5627:        msdos_mcb_check(mcb);
1.1.1.19  root     5628:        
1.1.1.33  root     5629:        mcb->mz = 'M';
                   5630:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39  root     5631:        
                   5632:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     5633: }
                   5634: 
1.1.1.33  root     5635: void msdos_mem_unlink_umb()
1.1.1.19  root     5636: {
1.1.1.33  root     5637:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5638:        msdos_mcb_check(mcb);
1.1.1.19  root     5639:        
1.1.1.33  root     5640:        mcb->mz = 'Z';
                   5641:        mcb->paragraphs = 0;
1.1.1.39  root     5642:        
                   5643:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     5644: }
                   5645: 
1.1.1.29  root     5646: #ifdef SUPPORT_HMA
                   5647: 
                   5648: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   5649: {
                   5650:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5651:        
                   5652:        mcb->ms[0] = 'M';
                   5653:        mcb->ms[1] = 'S';
                   5654:        mcb->owner = owner;
                   5655:        mcb->size = size;
                   5656:        mcb->next = next;
                   5657:        return(mcb);
                   5658: }
                   5659: 
                   5660: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   5661: {
                   5662:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   5663: }
                   5664: 
                   5665: int msdos_hma_mem_split(int offset, int size)
                   5666: {
                   5667:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5668:        
                   5669:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5670:                return(-1);
                   5671:        }
                   5672:        if(mcb->size >= size + 0x10) {
                   5673:                int new_offset = offset + 0x10 + size;
                   5674:                int new_size = mcb->size - 0x10 - size;
                   5675:                
                   5676:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   5677:                mcb->size = size;
                   5678:                mcb->next = new_offset;
                   5679:                return(0);
                   5680:        }
                   5681:        return(-1);
                   5682: }
                   5683: 
                   5684: void msdos_hma_mem_merge(int offset)
                   5685: {
                   5686:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5687:        
                   5688:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5689:                return;
                   5690:        }
                   5691:        while(1) {
                   5692:                if(mcb->next == 0) {
                   5693:                        break;
                   5694:                }
                   5695:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   5696:                
                   5697:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   5698:                        return;
                   5699:                }
                   5700:                if(next_mcb->owner != 0) {
                   5701:                        break;
                   5702:                }
                   5703:                mcb->size += 0x10 + next_mcb->size;
                   5704:                mcb->next = next_mcb->next;
                   5705:        }
                   5706: }
                   5707: 
                   5708: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   5709: {
                   5710:        int offset = 0x10; // first mcb in HMA
                   5711:        
                   5712:        while(1) {
                   5713:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5714:                
                   5715:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5716:                        return(-1);
                   5717:                }
                   5718:                if(mcb->owner == 0) {
                   5719:                        msdos_hma_mem_merge(offset);
                   5720:                }
                   5721:                if(mcb->owner == 0 && mcb->size >= size) {
                   5722:                        msdos_hma_mem_split(offset, size);
                   5723:                        mcb->owner = owner;
                   5724:                        return(offset);
                   5725:                }
                   5726:                if(mcb->next == 0) {
                   5727:                        break;
                   5728:                }
                   5729:                offset = mcb->next;
                   5730:        }
                   5731:        return(-1);
                   5732: }
                   5733: 
                   5734: int msdos_hma_mem_realloc(int offset, int size)
                   5735: {
                   5736:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5737:        
                   5738:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5739:                return(-1);
                   5740:        }
                   5741:        if(mcb->size < size) {
                   5742:                return(-1);
                   5743:        }
                   5744:        msdos_hma_mem_split(offset, size);
                   5745:        return(0);
                   5746: }
                   5747: 
                   5748: void msdos_hma_mem_free(int offset)
                   5749: {
                   5750:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5751:        
                   5752:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5753:                return;
                   5754:        }
                   5755:        mcb->owner = 0;
                   5756:        msdos_hma_mem_merge(offset);
                   5757: }
                   5758: 
                   5759: int msdos_hma_mem_get_free(int *available_offset)
                   5760: {
                   5761:        int offset = 0x10; // first mcb in HMA
                   5762:        int size = 0;
                   5763:        
                   5764:        while(1) {
                   5765:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5766:                
                   5767:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5768:                        return(0);
                   5769:                }
                   5770:                if(mcb->owner == 0 && size < mcb->size) {
                   5771:                        if(available_offset != NULL) {
                   5772:                                *available_offset = offset;
                   5773:                        }
                   5774:                        size = mcb->size;
                   5775:                }
                   5776:                if(mcb->next == 0) {
                   5777:                        break;
                   5778:                }
                   5779:                offset = mcb->next;
                   5780:        }
                   5781:        return(size);
                   5782: }
                   5783: 
                   5784: #endif
                   5785: 
1.1       root     5786: // environment
                   5787: 
1.1.1.45  root     5788: void msdos_env_set_argv(int env_seg, const char *argv)
1.1       root     5789: {
                   5790:        char *dst = (char *)(mem + (env_seg << 4));
                   5791:        
                   5792:        while(1) {
                   5793:                if(dst[0] == 0) {
                   5794:                        break;
                   5795:                }
                   5796:                dst += strlen(dst) + 1;
                   5797:        }
                   5798:        *dst++ = 0; // end of environment
                   5799:        *dst++ = 1; // top of argv[0]
                   5800:        *dst++ = 0;
                   5801:        memcpy(dst, argv, strlen(argv));
                   5802:        dst += strlen(argv);
                   5803:        *dst++ = 0;
                   5804:        *dst++ = 0;
                   5805: }
                   5806: 
1.1.1.45  root     5807: const char *msdos_env_get_argv(int env_seg)
1.1       root     5808: {
                   5809:        static char env[ENV_SIZE];
                   5810:        char *src = env;
                   5811:        
                   5812:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5813:        while(1) {
                   5814:                if(src[0] == 0) {
                   5815:                        if(src[1] == 1) {
                   5816:                                return(src + 3);
                   5817:                        }
                   5818:                        break;
                   5819:                }
                   5820:                src += strlen(src) + 1;
                   5821:        }
                   5822:        return(NULL);
                   5823: }
                   5824: 
1.1.1.45  root     5825: const char *msdos_env_get(int env_seg, const char *name)
1.1       root     5826: {
                   5827:        static char env[ENV_SIZE];
                   5828:        char *src = env;
                   5829:        
                   5830:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5831:        while(1) {
                   5832:                if(src[0] == 0) {
                   5833:                        break;
                   5834:                }
                   5835:                int len = strlen(src);
                   5836:                char *n = my_strtok(src, "=");
                   5837:                char *v = src + strlen(n) + 1;
                   5838:                
                   5839:                if(_stricmp(name, n) == 0) {
                   5840:                        return(v);
                   5841:                }
                   5842:                src += len + 1;
                   5843:        }
                   5844:        return(NULL);
                   5845: }
                   5846: 
1.1.1.45  root     5847: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1       root     5848: {
                   5849:        char env[ENV_SIZE];
                   5850:        char *src = env;
                   5851:        char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45  root     5852:        const char *argv = msdos_env_get_argv(env_seg);
1.1       root     5853:        int done = 0;
                   5854:        
                   5855:        memcpy(src, dst, ENV_SIZE);
                   5856:        memset(dst, 0, ENV_SIZE);
                   5857:        while(1) {
                   5858:                if(src[0] == 0) {
                   5859:                        break;
                   5860:                }
                   5861:                int len = strlen(src);
                   5862:                char *n = my_strtok(src, "=");
                   5863:                char *v = src + strlen(n) + 1;
                   5864:                char tmp[1024];
                   5865:                
                   5866:                if(_stricmp(name, n) == 0) {
                   5867:                        sprintf(tmp, "%s=%s", n, value);
                   5868:                        done = 1;
                   5869:                } else {
                   5870:                        sprintf(tmp, "%s=%s", n, v);
                   5871:                }
                   5872:                memcpy(dst, tmp, strlen(tmp));
                   5873:                dst += strlen(tmp) + 1;
                   5874:                src += len + 1;
                   5875:        }
                   5876:        if(!done) {
                   5877:                char tmp[1024];
                   5878:                
                   5879:                sprintf(tmp, "%s=%s", name, value);
                   5880:                memcpy(dst, tmp, strlen(tmp));
                   5881:                dst += strlen(tmp) + 1;
                   5882:        }
                   5883:        if(argv) {
                   5884:                *dst++ = 0; // end of environment
                   5885:                *dst++ = 1; // top of argv[0]
                   5886:                *dst++ = 0;
                   5887:                memcpy(dst, argv, strlen(argv));
                   5888:                dst += strlen(argv);
                   5889:                *dst++ = 0;
                   5890:                *dst++ = 0;
                   5891:        }
                   5892: }
                   5893: 
                   5894: // process
                   5895: 
1.1.1.8   root     5896: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     5897: {
                   5898:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5899:        
                   5900:        memset(psp, 0, PSP_SIZE);
                   5901:        psp->exit[0] = 0xcd;
                   5902:        psp->exit[1] = 0x20;
1.1.1.8   root     5903:        psp->first_mcb = mcb_seg;
1.1.1.46! root     5904: #if 1
        !          5905:        psp->call5[0] = 0xcd;   // int 6fh
        !          5906:        psp->call5[1] = 0x6f;
        !          5907:        psp->call5[2] = 0xc3;   // ret
        !          5908: #else
        !          5909:        psp->call5[0] = 0x8a;   // mov ah, cl
        !          5910:        psp->call5[1] = 0xe1;
        !          5911:        psp->call5[2] = 0xcd;   // int 21h
        !          5912:        psp->call5[3] = 0x21;
        !          5913:        psp->call5[4] = 0xc3;   // ret
        !          5914: #endif
1.1       root     5915:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   5916:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   5917:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   5918:        psp->parent_psp = parent_psp;
1.1.1.20  root     5919:        if(parent_psp == (UINT16)-1) {
                   5920:                for(int i = 0; i < 20; i++) {
                   5921:                        if(file_handler[i].valid) {
                   5922:                                psp->file_table[i] = i;
                   5923:                        } else {
                   5924:                                psp->file_table[i] = 0xff;
                   5925:                        }
1.1       root     5926:                }
1.1.1.20  root     5927:        } else {
                   5928:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     5929:        }
                   5930:        psp->env_seg = env_seg;
                   5931:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     5932:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     5933:        psp->file_table_size = 20;
                   5934:        psp->file_table_ptr.w.l = 0x18;
                   5935:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     5936:        psp->service[0] = 0xcd;
                   5937:        psp->service[1] = 0x21;
                   5938:        psp->service[2] = 0xcb;
                   5939:        return(psp);
                   5940: }
                   5941: 
1.1.1.20  root     5942: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   5943: {
                   5944:        if(psp_seg && fd < 20) {
                   5945:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5946:                psp->file_table[fd] = value;
                   5947:        }
                   5948: }
                   5949: 
                   5950: int msdos_psp_get_file_table(int fd, int psp_seg)
                   5951: {
                   5952:        if(psp_seg && fd < 20) {
                   5953:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5954:                fd = psp->file_table[fd];
                   5955:        }
                   5956:        return fd;
                   5957: }
                   5958: 
1.1.1.45  root     5959: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al)
1.1       root     5960: {
                   5961:        // load command file
                   5962:        int fd = -1;
1.1.1.45  root     5963:        int sio_port = 0;
                   5964:        int lpt_port = 0;
1.1       root     5965:        int dos_command = 0;
1.1.1.24  root     5966:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     5967:        char pipe_stdin_path[MAX_PATH] = {0};
                   5968:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39  root     5969:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     5970:        
                   5971:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   5972:        int opt_len = mem[opt_ofs];
                   5973:        memset(opt, 0, sizeof(opt));
                   5974:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   5975:        
1.1.1.14  root     5976:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   5977:                // this is a batch file, run command.com
                   5978:                char tmp[MAX_PATH];
                   5979:                if(opt_len != 0) {
                   5980:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   5981:                } else {
                   5982:                        sprintf(tmp, "/C %s", cmd);
                   5983:                }
                   5984:                strcpy(opt, tmp);
                   5985:                opt_len = strlen(opt);
                   5986:                mem[opt_ofs] = opt_len;
                   5987:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5988:                strcpy(command, comspec_path);
                   5989:                strcpy(name_tmp, "COMMAND.COM");
                   5990:        } else {
                   5991:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   5992:                        // redirect C:\COMMAND.COM to comspec_path
                   5993:                        strcpy(command, comspec_path);
                   5994:                } else {
                   5995:                        strcpy(command, cmd);
                   5996:                }
1.1.1.24  root     5997:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   5998:                        return(-1);
                   5999:                }
1.1.1.14  root     6000:                memset(name_tmp, 0, sizeof(name_tmp));
                   6001:                strcpy(name_tmp, name);
                   6002:                
                   6003:                // check command.com
1.1.1.38  root     6004:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   6005:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     6006:                        if(opt_len == 0) {
                   6007: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   6008:                                process_t *current_process = NULL;
                   6009:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   6010:                                        if(process[i].psp == current_psp) {
                   6011:                                                current_process = &process[i];
                   6012:                                                break;
                   6013:                                        }
                   6014:                                }
                   6015:                                if(current_process != NULL) {
                   6016:                                        param->cmd_line.dw = current_process->dta.dw;
                   6017:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6018:                                        opt_len = mem[opt_ofs];
                   6019:                                        memset(opt, 0, sizeof(opt));
                   6020:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6021:                                }
                   6022:                        }
                   6023:                        for(int i = 0; i < opt_len; i++) {
                   6024:                                if(opt[i] == ' ') {
                   6025:                                        continue;
                   6026:                                }
                   6027:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   6028:                                        for(int j = i + 3; j < opt_len; j++) {
                   6029:                                                if(opt[j] == ' ') {
                   6030:                                                        continue;
                   6031:                                                }
                   6032:                                                char *token = my_strtok(opt + j, " ");
                   6033:                                                
1.1.1.38  root     6034:                                                strcpy(command, token);
                   6035:                                                char tmp[MAX_PATH];
                   6036:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39  root     6037:                                                strcpy(opt, "");
                   6038:                                                for(int i = 0; i < strlen(tmp); i++) {
                   6039:                                                        if(tmp[i] != ' ') {
                   6040:                                                                strcpy(opt, tmp + i);
                   6041:                                                                break;
                   6042:                                                        }
                   6043:                                                }
                   6044:                                                strcpy(tmp, opt);
1.1.1.38  root     6045:                                                
                   6046:                                                if(al == 0x00) {
1.1.1.39  root     6047:                                                        #define GET_FILE_PATH() { \
                   6048:                                                                if(token[0] != '>' && token[0] != '<') { \
                   6049:                                                                        token++; \
                   6050:                                                                } \
                   6051:                                                                token++; \
                   6052:                                                                while(*token == ' ') { \
                   6053:                                                                        token++; \
                   6054:                                                                } \
                   6055:                                                                char *ptr = token; \
                   6056:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
                   6057:                                                                        ptr++; \
                   6058:                                                                } \
                   6059:                                                                *ptr = '\0'; \
                   6060:                                                        }
                   6061:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
                   6062:                                                                GET_FILE_PATH();
1.1.1.38  root     6063:                                                                strcpy(pipe_stdin_path, token);
                   6064:                                                                strcpy(opt, tmp);
                   6065:                                                        }
1.1.1.39  root     6066:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
                   6067:                                                                GET_FILE_PATH();
1.1.1.38  root     6068:                                                                strcpy(pipe_stdout_path, token);
                   6069:                                                                strcpy(opt, tmp);
                   6070:                                                        }
1.1.1.39  root     6071:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6072:                                                                GET_FILE_PATH();
                   6073:                                                                strcpy(pipe_stderr_path, token);
                   6074:                                                                strcpy(opt, tmp);
                   6075:                                                        }
                   6076:                                                        #undef GET_FILE_PATH
                   6077:                                                        
                   6078:                                                        if((token = strstr(opt, "0<")) != NULL) {
                   6079:                                                                *token = '\0';
                   6080:                                                        }
                   6081:                                                        if((token = strstr(opt, "1>")) != NULL) {
                   6082:                                                                *token = '\0';
                   6083:                                                        }
                   6084:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6085:                                                                *token = '\0';
                   6086:                                                        }
1.1.1.38  root     6087:                                                        if((token = strstr(opt, "<")) != NULL) {
                   6088:                                                                *token = '\0';
                   6089:                                                        }
                   6090:                                                        if((token = strstr(opt, ">")) != NULL) {
                   6091:                                                                *token = '\0';
                   6092:                                                        }
1.1.1.14  root     6093:                                                }
1.1.1.39  root     6094:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
                   6095:                                                        opt[i] = '\0';
                   6096:                                                }
1.1.1.38  root     6097:                                                opt_len = strlen(opt);
                   6098:                                                mem[opt_ofs] = opt_len;
                   6099:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6100:                                                dos_command = 1;
1.1.1.14  root     6101:                                                break;
1.1       root     6102:                                        }
                   6103:                                }
1.1.1.14  root     6104:                                break;
1.1       root     6105:                        }
                   6106:                }
                   6107:        }
                   6108:        
                   6109:        // load command file
                   6110:        strcpy(path, command);
                   6111:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6112:                sprintf(path, "%s.COM", command);
                   6113:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6114:                        sprintf(path, "%s.EXE", command);
                   6115:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     6116:                                sprintf(path, "%s.BAT", command);
                   6117:                                if(_access(path, 0) == 0) {
                   6118:                                        // this is a batch file, run command.com
                   6119:                                        char tmp[MAX_PATH];
                   6120:                                        if(opt_len != 0) {
                   6121:                                                sprintf(tmp, "/C %s %s", path, opt);
                   6122:                                        } else {
                   6123:                                                sprintf(tmp, "/C %s", path);
                   6124:                                        }
                   6125:                                        strcpy(opt, tmp);
                   6126:                                        opt_len = strlen(opt);
                   6127:                                        mem[opt_ofs] = opt_len;
                   6128:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6129:                                        strcpy(path, comspec_path);
                   6130:                                        strcpy(name_tmp, "COMMAND.COM");
                   6131:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6132:                                } else {
                   6133:                                        // search path in parent environments
                   6134:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45  root     6135:                                        const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14  root     6136:                                        if(env != NULL) {
                   6137:                                                char env_path[4096];
                   6138:                                                strcpy(env_path, env);
                   6139:                                                char *token = my_strtok(env_path, ";");
                   6140:                                                
                   6141:                                                while(token != NULL) {
                   6142:                                                        if(strlen(token) != 0) {
                   6143:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   6144:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6145:                                                                        break;
                   6146:                                                                }
                   6147:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   6148:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6149:                                                                        break;
                   6150:                                                                }
                   6151:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   6152:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6153:                                                                        break;
                   6154:                                                                }
                   6155:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   6156:                                                                if(_access(path, 0) == 0) {
                   6157:                                                                        // this is a batch file, run command.com
                   6158:                                                                        char tmp[MAX_PATH];
                   6159:                                                                        if(opt_len != 0) {
                   6160:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   6161:                                                                        } else {
                   6162:                                                                                sprintf(tmp, "/C %s", path);
                   6163:                                                                        }
                   6164:                                                                        strcpy(opt, tmp);
                   6165:                                                                        opt_len = strlen(opt);
                   6166:                                                                        mem[opt_ofs] = opt_len;
                   6167:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6168:                                                                        strcpy(path, comspec_path);
                   6169:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   6170:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6171:                                                                        break;
                   6172:                                                                }
1.1.1.8   root     6173:                                                        }
1.1.1.14  root     6174:                                                        token = my_strtok(NULL, ";");
1.1       root     6175:                                                }
                   6176:                                        }
                   6177:                                }
                   6178:                        }
                   6179:                }
                   6180:        }
                   6181:        if(fd == -1) {
1.1.1.38  root     6182:                // we can not find command.com in the path, so open comspec_path
                   6183:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6184:                        strcpy(command, comspec_path);
                   6185:                        strcpy(path, command);
                   6186:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6187:                }
                   6188:        }
                   6189:        if(fd == -1) {
1.1       root     6190:                if(dos_command) {
                   6191:                        // may be dos command
                   6192:                        char tmp[MAX_PATH];
                   6193:                        sprintf(tmp, "%s %s", command, opt);
                   6194:                        system(tmp);
                   6195:                        return(0);
                   6196:                } else {
                   6197:                        return(-1);
                   6198:                }
                   6199:        }
                   6200:        _read(fd, file_buffer, sizeof(file_buffer));
                   6201:        _close(fd);
                   6202:        
                   6203:        // copy environment
1.1.1.29  root     6204:        int umb_linked, env_seg, psp_seg;
1.1       root     6205:        
1.1.1.29  root     6206:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6207:                msdos_mem_unlink_umb();
                   6208:        }
1.1.1.8   root     6209:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6210:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6211:                        if(umb_linked != 0) {
                   6212:                                msdos_mem_link_umb();
                   6213:                        }
                   6214:                        return(-1);
                   6215:                }
1.1       root     6216:        }
                   6217:        if(param->env_seg == 0) {
                   6218:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6219:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6220:        } else {
                   6221:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6222:        }
                   6223:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6224:        
                   6225:        // check exe header
                   6226:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6227:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6228:        UINT16 cs, ss, ip, sp;
                   6229:        
                   6230:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6231:                // memory allocation
                   6232:                int header_size = header->header_size * 16;
                   6233:                int load_size = header->pages * 512 - header_size;
                   6234:                if(header_size + load_size < 512) {
                   6235:                        load_size = 512 - header_size;
                   6236:                }
                   6237:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6238:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6239:                        msdos_mem_free(env_seg);
                   6240:                        return(-1);
                   6241:                }
                   6242:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6243:                if(paragraphs > free_paragraphs) {
                   6244:                        paragraphs = free_paragraphs;
                   6245:                }
1.1.1.8   root     6246:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6247:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6248:                                if(umb_linked != 0) {
                   6249:                                        msdos_mem_link_umb();
                   6250:                                }
                   6251:                                msdos_mem_free(env_seg);
                   6252:                                return(-1);
                   6253:                        }
1.1       root     6254:                }
                   6255:                // relocation
                   6256:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6257:                for(int i = 0; i < header->relocations; i++) {
                   6258:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6259:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6260:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6261:                }
                   6262:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6263:                // segments
                   6264:                cs = header->init_cs + start_seg;
                   6265:                ss = header->init_ss + start_seg;
                   6266:                ip = header->init_ip;
                   6267:                sp = header->init_sp - 2; // for symdeb
                   6268:        } else {
                   6269:                // memory allocation
                   6270:                paragraphs = free_paragraphs;
1.1.1.8   root     6271:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6272:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6273:                                if(umb_linked != 0) {
                   6274:                                        msdos_mem_link_umb();
                   6275:                                }
                   6276:                                msdos_mem_free(env_seg);
                   6277:                                return(-1);
                   6278:                        }
1.1       root     6279:                }
                   6280:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6281:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6282:                // segments
                   6283:                cs = ss = psp_seg;
                   6284:                ip = 0x100;
                   6285:                sp = 0xfffe;
                   6286:        }
1.1.1.29  root     6287:        if(umb_linked != 0) {
                   6288:                msdos_mem_link_umb();
                   6289:        }
1.1       root     6290:        
                   6291:        // create psp
1.1.1.3   root     6292:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6293:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6294:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6295:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6296:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6297:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6298:        
                   6299:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6300:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6301:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6302:        
1.1.1.4   root     6303:        for(int i = 0; i < 8; i++) {
                   6304:                if(name_tmp[i] == '.') {
                   6305:                        mcb_psp->prog_name[i] = '\0';
                   6306:                        break;
                   6307:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6308:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6309:                        i++;
                   6310:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6311:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6312:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6313:                } else {
                   6314:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6315:                }
                   6316:        }
                   6317:        
1.1       root     6318:        // process info
                   6319:        process_t *process = msdos_process_info_create(psp_seg);
                   6320:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6321: #ifdef USE_DEBUGGER
                   6322:        strcpy(process->module_path, path);
                   6323: #endif
1.1       root     6324:        process->dta.w.l = 0x80;
                   6325:        process->dta.w.h = psp_seg;
                   6326:        process->switchar = '/';
                   6327:        process->max_files = 20;
                   6328:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6329:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6330:        process->parent_ds = SREG(DS);
1.1.1.31  root     6331:        process->parent_es = SREG(ES);
1.1       root     6332:        
                   6333:        current_psp = psp_seg;
1.1.1.23  root     6334:        msdos_sda_update(current_psp);
1.1       root     6335:        
                   6336:        if(al == 0x00) {
                   6337:                int_10h_feh_called = int_10h_ffh_called = false;
                   6338:                
1.1.1.38  root     6339:                // pipe
                   6340:                if(pipe_stdin_path[0] != '\0') {
1.1.1.45  root     6341: //                     if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
                   6342:                        if(msdos_is_device_path(pipe_stdin_path)) {
                   6343:                                fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
                   6344:                        } else {
                   6345:                                fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
                   6346:                        }
                   6347:                        if(fd != -1) {
                   6348:                                msdos_file_handler_open(fd, pipe_stdin_path, _isatty(fd), 0, msdos_device_info(pipe_stdin_path), current_psp, sio_port, lpt_port);
1.1.1.38  root     6349:                                psp->file_table[0] = fd;
                   6350:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6351:                        }
                   6352:                }
                   6353:                if(pipe_stdout_path[0] != '\0') {
                   6354:                        if(_access(pipe_stdout_path, 0) == 0) {
                   6355:                                SetFileAttributes(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6356:                                DeleteFile(pipe_stdout_path);
                   6357:                        }
1.1.1.45  root     6358: //                     if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6359:                        if(msdos_is_device_path(pipe_stdout_path)) {
                   6360:                                fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6361:                        } else {
                   6362:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6363:                        }
                   6364:                        if(fd != -1) {
                   6365:                                msdos_file_handler_open(fd, pipe_stdout_path, _isatty(fd), 1, msdos_device_info(pipe_stdout_path), current_psp, sio_port, lpt_port);
1.1.1.38  root     6366:                                psp->file_table[1] = fd;
                   6367:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6368:                        }
                   6369:                }
1.1.1.39  root     6370:                if(pipe_stderr_path[0] != '\0') {
                   6371:                        if(_access(pipe_stderr_path, 0) == 0) {
                   6372:                                SetFileAttributes(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
                   6373:                                DeleteFile(pipe_stderr_path);
                   6374:                        }
1.1.1.45  root     6375: //                     if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6376:                        if(msdos_is_device_path(pipe_stderr_path)) {
                   6377:                                fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6378:                        } else {
                   6379:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6380:                        }
                   6381:                        if(fd != -1) {
                   6382:                                msdos_file_handler_open(fd, pipe_stderr_path, _isatty(fd), 1, msdos_device_info(pipe_stderr_path), current_psp, sio_port, lpt_port);
1.1.1.39  root     6383:                                psp->file_table[2] = fd;
                   6384:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6385:                        }
                   6386:                }
1.1.1.38  root     6387:                
1.1       root     6388:                // registers and segments
                   6389:                REG16(AX) = REG16(BX) = 0x00;
                   6390:                REG16(CX) = 0xff;
                   6391:                REG16(DX) = psp_seg;
                   6392:                REG16(SI) = ip;
                   6393:                REG16(DI) = sp;
                   6394:                REG16(SP) = sp;
1.1.1.3   root     6395:                SREG(DS) = SREG(ES) = psp_seg;
                   6396:                SREG(SS) = ss;
                   6397:                i386_load_segment_descriptor(DS);
                   6398:                i386_load_segment_descriptor(ES);
                   6399:                i386_load_segment_descriptor(SS);
1.1       root     6400:                
                   6401:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6402:                i386_jmp_far(cs, ip);
                   6403:        } else if(al == 0x01) {
                   6404:                // copy ss:sp and cs:ip to param block
                   6405:                param->sp = sp;
                   6406:                param->ss = ss;
                   6407:                param->ip = ip;
                   6408:                param->cs = cs;
1.1.1.31  root     6409:                
                   6410:                // the AX value to be passed to the child program is put on top of the child's stack
                   6411:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6412:        }
                   6413:        return(0);
                   6414: }
                   6415: 
                   6416: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6417: {
                   6418:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6419:        
                   6420:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6421:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6422:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6423:        
1.1.1.3   root     6424:        SREG(SS) = psp->stack.w.h;
                   6425:        i386_load_segment_descriptor(SS);
1.1       root     6426:        REG16(SP) = psp->stack.w.l;
                   6427:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6428:        
1.1.1.28  root     6429: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6430:        process_t *current_process = NULL;
                   6431:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6432:                if(process[i].psp == psp_seg) {
                   6433:                        current_process = &process[i];
                   6434:                        break;
                   6435:                }
                   6436:        }
                   6437:        if(current_process == NULL) {
                   6438:                throw(0x1f); // general failure
                   6439:        }
                   6440:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6441:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6442:        if(current_process->called_by_int2eh) {
                   6443:                REG16(AX) = ret;
                   6444:        }
                   6445:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6446:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6447:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6448:        i386_load_segment_descriptor(ES);
1.1       root     6449:        
                   6450:        if(mem_free) {
1.1.1.8   root     6451:                int mcb_seg;
                   6452:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6453:                        msdos_mem_free(mcb_seg + 1);
                   6454:                }
                   6455:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6456:                        msdos_mem_free(mcb_seg + 1);
                   6457:                }
1.1       root     6458:                
                   6459:                for(int i = 0; i < MAX_FILES; i++) {
                   6460:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6461:                                _close(i);
1.1.1.20  root     6462:                                msdos_file_handler_close(i);
                   6463:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6464:                        }
                   6465:                }
1.1.1.13  root     6466:                msdos_dta_info_free(psp_seg);
1.1       root     6467:        }
1.1.1.14  root     6468:        msdos_stdio_reopen();
1.1       root     6469:        
1.1.1.28  root     6470:        memset(current_process, 0, sizeof(process_t));
1.1       root     6471:        
                   6472:        current_psp = psp->parent_psp;
                   6473:        retval = ret;
1.1.1.23  root     6474:        msdos_sda_update(current_psp);
1.1       root     6475: }
                   6476: 
                   6477: // drive
                   6478: 
1.1.1.42  root     6479: int pcbios_update_drive_param(int drive_num, int force_update);
                   6480: 
1.1       root     6481: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6482: {
1.1.1.41  root     6483:        if(!(drive_num >= 0 && drive_num < 26)) {
                   6484:                return(0);
                   6485:        }
1.1.1.42  root     6486:        pcbios_update_drive_param(drive_num, force_update);
                   6487:        
                   6488:        drive_param_t *drive_param = &drive_params[drive_num];
1.1       root     6489:        *seg = DPB_TOP >> 4;
                   6490:        *ofs = sizeof(dpb_t) * drive_num;
                   6491:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6492:        
                   6493:        memset(dpb, 0, sizeof(dpb_t));
                   6494:        
1.1.1.41  root     6495:        dpb->drive_num = drive_num;
                   6496:        dpb->unit_num = drive_num;
1.1.1.42  root     6497:        
                   6498:        if(drive_param->valid) {
                   6499:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   6500:                
                   6501:                dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
                   6502:                dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
                   6503:                dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6504:                dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6505:                switch(geo->MediaType) {
                   6506:                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6507:                        dpb->media_type = 0xff;
                   6508:                        break;
                   6509:                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6510:                        dpb->media_type = 0xfe;
                   6511:                        break;
                   6512:                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6513:                        dpb->media_type = 0xfd;
                   6514:                        break;
                   6515:                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6516:                        dpb->media_type = 0xfc;
                   6517:                        break;
                   6518:                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6519:                case F3_1Pt2_512:
                   6520:                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6521:                case F5_720_512:
                   6522:                        dpb->media_type = 0xf9;
                   6523:                        break;
                   6524:                case FixedMedia:        // hard disk
                   6525:                case RemovableMedia:
                   6526:                case Unknown:
                   6527:                        dpb->media_type = 0xf8;
                   6528:                        break;
                   6529:                default:
                   6530:                        dpb->media_type = 0xf0;
                   6531:                        break;
                   6532:                }
                   6533:        }
1.1.1.41  root     6534:        dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
                   6535:        dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42  root     6536:        dpb->info_sector = 0xffff;
                   6537:        dpb->backup_boot_sector = 0xffff;
                   6538:        dpb->free_clusters = 0xffff;
                   6539:        dpb->free_search_cluster = 0xffffffff;
                   6540:        
                   6541:        return(drive_param->valid);
1.1       root     6542: }
                   6543: 
                   6544: // pc bios
                   6545: 
1.1.1.35  root     6546: #ifdef USE_SERVICE_THREAD
                   6547: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   6548: {
                   6549: #if defined(HAS_I386)
                   6550:        if(m_SF != 0) {
                   6551:                m_SF = 0;
                   6552:                mem[0xfffd0 + 0x15] = 0x79;     // jns -4
                   6553:        } else {
                   6554:                m_SF = 1;
                   6555:                mem[0xfffd0 + 0x15] = 0x78;     // js -4
                   6556:        }
                   6557: #else
                   6558:        if(m_SignVal < 0) {
                   6559:                m_SignVal = 0;
                   6560:                mem[0xfffd0 + 0x15] = 0x79;     // jns -4
                   6561:        } else {
                   6562:                m_SignVal = -1;
                   6563:                mem[0xfffd0 + 0x15] = 0x78;     // js -4
                   6564:        }
                   6565: #endif
                   6566:        i386_call_far(0xfffd, 0x0013);
                   6567:        in_service = true;
                   6568:        service_exit = false;
                   6569:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   6570: }
                   6571: 
                   6572: void finish_service_loop()
                   6573: {
                   6574:        if(in_service && service_exit) {
                   6575: #if defined(HAS_I386)
                   6576:                if(m_SF != 0) {
                   6577:                        m_SF = 0;
                   6578:                } else {
                   6579:                        m_SF = 1;
                   6580:                }
                   6581: #else
                   6582:                if(m_SignVal < 0) {
                   6583:                        m_SignVal = 0;
                   6584:                } else {
                   6585:                        m_SignVal = -1;
                   6586:                }
                   6587: #endif
                   6588:                in_service = false;
                   6589:        }
                   6590: }
                   6591: #endif
                   6592: 
1.1.1.19  root     6593: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   6594: {
                   6595:        static unsigned __int64 start_msec_since_midnight = 0;
                   6596:        static unsigned __int64 start_msec_since_hostboot = 0;
                   6597:        
                   6598:        if(start_msec_since_midnight == 0) {
                   6599:                SYSTEMTIME time;
                   6600:                GetLocalTime(&time);
                   6601:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   6602:                start_msec_since_hostboot = cur_msec;
                   6603:        }
                   6604:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   6605:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   6606:        return (UINT32)tick;
                   6607: }
                   6608: 
                   6609: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   6610: {
                   6611:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   6612:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   6613:        
                   6614:        if(prev_tick > next_tick) {
                   6615:                mem[0x470] = 1;
                   6616:        }
                   6617:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   6618: }
                   6619: 
1.1.1.14  root     6620: inline void pcbios_irq0()
                   6621: {
                   6622:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     6623:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     6624: }
                   6625: 
1.1.1.16  root     6626: int pcbios_get_text_vram_address(int page)
1.1       root     6627: {
                   6628:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6629:                return TEXT_VRAM_TOP;
1.1       root     6630:        } else {
1.1.1.14  root     6631:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     6632:        }
                   6633: }
                   6634: 
1.1.1.16  root     6635: int pcbios_get_shadow_buffer_address(int page)
1.1       root     6636: {
1.1.1.14  root     6637:        if(!int_10h_feh_called) {
1.1.1.16  root     6638:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     6639:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6640:                return SHADOW_BUF_TOP;
                   6641:        } else {
1.1.1.14  root     6642:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     6643:        }
                   6644: }
                   6645: 
1.1.1.16  root     6646: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     6647: {
1.1.1.16  root     6648:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     6649: }
                   6650: 
1.1.1.16  root     6651: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     6652: {
1.1.1.14  root     6653:        // clear the existing screen, not just the new one
                   6654:        int clr_height = max(height, scr_height);
                   6655:        
1.1.1.16  root     6656:        if(scr_width != width || scr_height != height) {
                   6657:                change_console_size(width, height);
1.1.1.14  root     6658:        }
                   6659:        mem[0x462] = 0;
                   6660:        *(UINT16 *)(mem + 0x44e) = 0;
                   6661:        
1.1.1.16  root     6662:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   6663:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   6664:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   6665:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1       root     6666:        
1.1.1.23  root     6667:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     6668:        if(clr_screen) {
1.1.1.14  root     6669:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   6670:                        mem[ofs++] = 0x20;
                   6671:                        mem[ofs++] = 0x07;
                   6672:                }
                   6673:                
1.1.1.35  root     6674: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6675:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6676: #endif
1.1.1.14  root     6677:                for(int y = 0; y < clr_height; y++) {
                   6678:                        for(int x = 0; x < scr_width; x++) {
                   6679:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6680:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     6681:                        }
                   6682:                }
                   6683:                SMALL_RECT rect;
1.1.1.14  root     6684:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   6685:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6686:                vram_length_char = vram_last_length_char = 0;
                   6687:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     6688: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6689:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6690: #endif
1.1       root     6691:        }
1.1.1.14  root     6692:        COORD co;
                   6693:        co.X = 0;
                   6694:        co.Y = scr_top;
                   6695:        SetConsoleCursorPosition(hStdout, co);
                   6696:        cursor_moved = true;
                   6697:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     6698: }
                   6699: 
1.1.1.36  root     6700: void pcbios_update_cursor_position()
                   6701: {
                   6702:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6703:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   6704:        if(!restore_console_on_exit) {
                   6705:                scr_top = csbi.srWindow.Top;
                   6706:        }
                   6707:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   6708:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   6709: }
                   6710: 
1.1.1.16  root     6711: inline void pcbios_int_10h_00h()
                   6712: {
                   6713:        switch(REG8(AL) & 0x7f) {
                   6714:        case 0x70: // v-text mode
                   6715:        case 0x71: // extended cga v-text mode
                   6716:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   6717:                break;
                   6718:        default:
                   6719:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   6720:                break;
                   6721:        }
                   6722:        if(REG8(AL) & 0x80) {
                   6723:                mem[0x487] |= 0x80;
                   6724:        } else {
                   6725:                mem[0x487] &= ~0x80;
                   6726:        }
                   6727:        mem[0x449] = REG8(AL) & 0x7f;
                   6728: }
                   6729: 
1.1       root     6730: inline void pcbios_int_10h_01h()
                   6731: {
1.1.1.13  root     6732:        mem[0x460] = REG8(CL);
                   6733:        mem[0x461] = REG8(CH);
1.1.1.14  root     6734:        
1.1.1.23  root     6735:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6736:        CONSOLE_CURSOR_INFO ci;
                   6737:        GetConsoleCursorInfo(hStdout, &ci);
                   6738: //     ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
                   6739: //     if(ci.bVisible) {
                   6740:                int lines = max(8, REG8(CL) + 1);
                   6741:                ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
                   6742: //     }
                   6743:        SetConsoleCursorInfo(hStdout, &ci);
1.1       root     6744: }
                   6745: 
                   6746: inline void pcbios_int_10h_02h()
                   6747: {
1.1.1.14  root     6748:        // continuously setting the cursor effectively stops it blinking
                   6749:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     6750:                COORD co;
                   6751:                co.X = REG8(DL);
1.1.1.14  root     6752:                co.Y = REG8(DH) + scr_top;
                   6753:                
                   6754:                // some programs hide the cursor by moving it off screen
                   6755:                static bool hidden = false;
1.1.1.23  root     6756:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6757:                CONSOLE_CURSOR_INFO ci;
                   6758:                GetConsoleCursorInfo(hStdout, &ci);
                   6759:                
                   6760:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   6761:                        if(ci.bVisible) {
                   6762:                                ci.bVisible = FALSE;
                   6763: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6764:                                hidden = true;
                   6765:                        }
                   6766:                } else if(hidden) {
                   6767:                        if(!ci.bVisible) {
                   6768:                                ci.bVisible = TRUE;
                   6769: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6770:                        }
                   6771:                        hidden = false;
                   6772:                }
1.1       root     6773:        }
1.1.1.14  root     6774:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   6775:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     6776: }
                   6777: 
                   6778: inline void pcbios_int_10h_03h()
                   6779: {
1.1.1.14  root     6780:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6781:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6782:        REG8(CL) = mem[0x460];
                   6783:        REG8(CH) = mem[0x461];
                   6784: }
                   6785: 
                   6786: inline void pcbios_int_10h_05h()
                   6787: {
1.1.1.14  root     6788:        if(REG8(AL) >= vram_pages) {
                   6789:                return;
                   6790:        }
                   6791:        if(mem[0x462] != REG8(AL)) {
                   6792:                vram_flush();
                   6793:                
1.1.1.23  root     6794:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6795:                SMALL_RECT rect;
1.1.1.14  root     6796:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6797:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6798:                
1.1.1.16  root     6799:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     6800:                        for(int x = 0; x < scr_width; x++) {
                   6801:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6802:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6803:                        }
                   6804:                }
1.1.1.16  root     6805:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     6806:                        for(int x = 0; x < scr_width; x++) {
                   6807:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   6808:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     6809:                        }
                   6810:                }
1.1.1.14  root     6811:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6812:                
                   6813:                COORD co;
1.1.1.14  root     6814:                co.X = mem[0x450 + REG8(AL) * 2];
                   6815:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   6816:                if(co.Y < scr_top + scr_height) {
                   6817:                        SetConsoleCursorPosition(hStdout, co);
                   6818:                }
1.1       root     6819:        }
1.1.1.14  root     6820:        mem[0x462] = REG8(AL);
                   6821:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   6822:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     6823:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     6824:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     6825:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     6826:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1       root     6827: }
                   6828: 
                   6829: inline void pcbios_int_10h_06h()
                   6830: {
1.1.1.14  root     6831:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6832:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6833:                return;
                   6834:        }
                   6835:        vram_flush();
                   6836:        
1.1.1.23  root     6837:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6838:        SMALL_RECT rect;
1.1.1.14  root     6839:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6840:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6841:        
                   6842:        int right = min(REG8(DL), scr_width - 1);
                   6843:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6844:        
                   6845:        if(REG8(AL) == 0) {
1.1.1.14  root     6846:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6847:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6848:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6849:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6850:                        }
                   6851:                }
                   6852:        } else {
1.1.1.14  root     6853:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     6854:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6855:                                if(y2 <= bottom) {
                   6856:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6857:                                } else {
1.1.1.14  root     6858:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6859:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6860:                                }
1.1.1.14  root     6861:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6862:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6863:                        }
                   6864:                }
                   6865:        }
1.1.1.14  root     6866:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6867: }
                   6868: 
                   6869: inline void pcbios_int_10h_07h()
                   6870: {
1.1.1.14  root     6871:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6872:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6873:                return;
                   6874:        }
                   6875:        vram_flush();
                   6876:        
1.1.1.23  root     6877:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6878:        SMALL_RECT rect;
1.1.1.14  root     6879:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6880:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6881:        
                   6882:        int right = min(REG8(DL), scr_width - 1);
                   6883:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6884:        
                   6885:        if(REG8(AL) == 0) {
1.1.1.14  root     6886:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6887:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6888:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6889:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6890:                        }
                   6891:                }
                   6892:        } else {
1.1.1.14  root     6893:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     6894:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6895:                                if(y2 >= REG8(CH)) {
                   6896:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6897:                                } else {
1.1.1.14  root     6898:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6899:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6900:                                }
1.1.1.14  root     6901:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6902:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6903:                        }
                   6904:                }
                   6905:        }
1.1.1.14  root     6906:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6907: }
                   6908: 
                   6909: inline void pcbios_int_10h_08h()
                   6910: {
                   6911:        COORD co;
                   6912:        DWORD num;
                   6913:        
1.1.1.14  root     6914:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6915:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6916:        
                   6917:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6918:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6919:                co.Y += scr_top;
                   6920:                vram_flush();
1.1       root     6921:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   6922:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   6923:                REG8(AL) = scr_char[0];
                   6924:                REG8(AH) = scr_attr[0];
                   6925:        } else {
1.1.1.16  root     6926:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     6927:        }
                   6928: }
                   6929: 
                   6930: inline void pcbios_int_10h_09h()
                   6931: {
                   6932:        COORD co;
                   6933:        
1.1.1.14  root     6934:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6935:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6936:        
1.1.1.16  root     6937:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   6938:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     6939:        
                   6940:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6941: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6942:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6943: #endif
1.1.1.16  root     6944:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6945:                while(dest < end) {
                   6946:                        write_text_vram_char(dest - vram, REG8(AL));
                   6947:                        mem[dest++] = REG8(AL);
                   6948:                        write_text_vram_attr(dest - vram, REG8(BL));
                   6949:                        mem[dest++] = REG8(BL);
1.1       root     6950:                }
1.1.1.35  root     6951: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6952:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6953: #endif
1.1       root     6954:        } else {
1.1.1.14  root     6955:                while(dest < end) {
1.1       root     6956:                        mem[dest++] = REG8(AL);
                   6957:                        mem[dest++] = REG8(BL);
                   6958:                }
                   6959:        }
                   6960: }
                   6961: 
                   6962: inline void pcbios_int_10h_0ah()
                   6963: {
                   6964:        COORD co;
                   6965:        
1.1.1.14  root     6966:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6967:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6968:        
1.1.1.16  root     6969:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   6970:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     6971:        
                   6972:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6973: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6974:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6975: #endif
1.1.1.16  root     6976:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6977:                while(dest < end) {
                   6978:                        write_text_vram_char(dest - vram, REG8(AL));
                   6979:                        mem[dest++] = REG8(AL);
                   6980:                        dest++;
1.1       root     6981:                }
1.1.1.35  root     6982: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6983:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6984: #endif
1.1       root     6985:        } else {
1.1.1.14  root     6986:                while(dest < end) {
1.1       root     6987:                        mem[dest++] = REG8(AL);
                   6988:                        dest++;
                   6989:                }
                   6990:        }
                   6991: }
                   6992: 
1.1.1.40  root     6993: HDC get_console_window_device_context()
                   6994: {
                   6995:        static HWND hwndFound = 0;
                   6996:        
                   6997:        if(hwndFound == 0) {
                   6998:                // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
                   6999:                char pszNewWindowTitle[1024];
                   7000:                char pszOldWindowTitle[1024];
                   7001:                
                   7002:                GetConsoleTitle(pszOldWindowTitle, 1024);
                   7003:                wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
                   7004:                SetConsoleTitle(pszNewWindowTitle);
                   7005:                Sleep(100);
                   7006:                hwndFound = FindWindow(NULL, pszNewWindowTitle);
                   7007:                SetConsoleTitle(pszOldWindowTitle);
                   7008:        }
                   7009:        return GetDC(hwndFound);
                   7010: }
                   7011: 
                   7012: inline void pcbios_int_10h_0ch()
                   7013: {
                   7014:        HDC hdc = get_console_window_device_context();
                   7015:        
                   7016:        if(hdc != NULL) {
                   7017:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
                   7018:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
                   7019:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
                   7020:                
                   7021:                if(REG8(AL) & 0x80) {
                   7022:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7023:                        if(color != CLR_INVALID) {
                   7024:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7025:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7026:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
                   7027:                        }
                   7028:                }
                   7029:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
                   7030:        }
                   7031: }
                   7032: 
                   7033: inline void pcbios_int_10h_0dh()
                   7034: {
                   7035:        HDC hdc = get_console_window_device_context();
                   7036:        BYTE r = 0;
                   7037:        BYTE g = 0;
                   7038:        BYTE b = 0;
                   7039:        
                   7040:        if(hdc != NULL) {
                   7041:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7042:                if(color != CLR_INVALID) {
                   7043:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7044:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7045:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
                   7046:                }
                   7047:        }
                   7048:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
                   7049: }
                   7050: 
1.1       root     7051: inline void pcbios_int_10h_0eh()
                   7052: {
1.1.1.14  root     7053:        DWORD num;
                   7054:        COORD co;
                   7055:        
                   7056:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7057:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7058:        
                   7059:        if(REG8(AL) == 7) {
                   7060:                //MessageBeep(-1);
                   7061:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   7062:                if(REG8(AL) == 10) {
                   7063:                        vram_flush();
                   7064:                }
1.1.1.23  root     7065:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     7066:                cursor_moved = true;
                   7067:        } else {
1.1.1.16  root     7068:                int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
1.1.1.14  root     7069:                if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7070: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7071:                        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7072: #endif
1.1.1.16  root     7073:                        int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7074:                        write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     7075: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7076:                        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7077: #endif
1.1.1.14  root     7078:                        
1.1.1.23  root     7079:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7080:                        if(++co.X == scr_width) {
                   7081:                                co.X = 0;
                   7082:                                if(++co.Y == scr_height) {
                   7083:                                        vram_flush();
                   7084:                                        WriteConsole(hStdout, "\n", 1, &num, NULL);
                   7085:                                        cursor_moved = true;
                   7086:                                }
                   7087:                        }
                   7088:                        if(!cursor_moved) {
                   7089:                                co.Y += scr_top;
                   7090:                                SetConsoleCursorPosition(hStdout, co);
                   7091:                                cursor_moved = true;
                   7092:                        }
                   7093:                }
                   7094:                mem[dest] = REG8(AL);
                   7095:        }
1.1       root     7096: }
                   7097: 
                   7098: inline void pcbios_int_10h_0fh()
                   7099: {
                   7100:        REG8(AL) = mem[0x449];
                   7101:        REG8(AH) = mem[0x44a];
                   7102:        REG8(BH) = mem[0x462];
                   7103: }
                   7104: 
1.1.1.14  root     7105: inline void pcbios_int_10h_11h()
                   7106: {
                   7107:        switch(REG8(AL)) {
1.1.1.16  root     7108:        case 0x01:
1.1.1.14  root     7109:        case 0x11:
1.1.1.16  root     7110:                pcbios_set_console_size(80, 28, true);
1.1.1.14  root     7111:                break;
1.1.1.16  root     7112:        case 0x02:
1.1.1.14  root     7113:        case 0x12:
1.1.1.16  root     7114:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     7115:                break;
1.1.1.16  root     7116:        case 0x04:
1.1.1.14  root     7117:        case 0x14:
1.1.1.16  root     7118:                pcbios_set_console_size(80, 25, true);
                   7119:                break;
                   7120:        case 0x18:
                   7121:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     7122:                break;
                   7123:        case 0x30:
                   7124:                SREG(ES) = 0;
                   7125:                i386_load_segment_descriptor(ES);
                   7126:                REG16(BP) = 0;
                   7127:                REG16(CX) = mem[0x485];
                   7128:                REG8(DL) = mem[0x484];
                   7129:                break;
                   7130:        }
                   7131: }
                   7132: 
                   7133: inline void pcbios_int_10h_12h()
                   7134: {
1.1.1.16  root     7135:        switch(REG8(BL)) {
                   7136:        case 0x10:
1.1.1.14  root     7137:                REG16(BX) = 0x0003;
                   7138:                REG16(CX) = 0x0009;
1.1.1.16  root     7139:                break;
1.1.1.14  root     7140:        }
                   7141: }
                   7142: 
1.1       root     7143: inline void pcbios_int_10h_13h()
                   7144: {
1.1.1.3   root     7145:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     7146:        COORD co;
                   7147:        DWORD num;
                   7148:        
                   7149:        co.X = REG8(DL);
1.1.1.14  root     7150:        co.Y = REG8(DH) + scr_top;
                   7151:        
                   7152:        vram_flush();
1.1       root     7153:        
                   7154:        switch(REG8(AL)) {
                   7155:        case 0x00:
                   7156:        case 0x01:
                   7157:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7158:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7159:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7160:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7161:                        SetConsoleCursorPosition(hStdout, co);
                   7162:                        
                   7163:                        if(csbi.wAttributes != REG8(BL)) {
                   7164:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   7165:                        }
1.1.1.14  root     7166:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   7167:                        
1.1       root     7168:                        if(csbi.wAttributes != REG8(BL)) {
                   7169:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7170:                        }
                   7171:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     7172:                                if(!restore_console_on_exit) {
                   7173:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7174:                                        scr_top = csbi.srWindow.Top;
                   7175:                                }
1.1.1.14  root     7176:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7177:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7178:                                SetConsoleCursorPosition(hStdout, co);
                   7179:                        } else {
                   7180:                                cursor_moved = true;
                   7181:                        }
                   7182:                } else {
1.1.1.3   root     7183:                        m_CF = 1;
1.1       root     7184:                }
                   7185:                break;
                   7186:        case 0x02:
                   7187:        case 0x03:
                   7188:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7189:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7190:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7191:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7192:                        SetConsoleCursorPosition(hStdout, co);
                   7193:                        
                   7194:                        WORD wAttributes = csbi.wAttributes;
                   7195:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7196:                                if(wAttributes != mem[ofs + 1]) {
                   7197:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7198:                                        wAttributes = mem[ofs + 1];
                   7199:                                }
1.1.1.14  root     7200:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7201:                        }
                   7202:                        if(csbi.wAttributes != wAttributes) {
                   7203:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7204:                        }
                   7205:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7206:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7207:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7208:                                SetConsoleCursorPosition(hStdout, co);
                   7209:                        } else {
                   7210:                                cursor_moved = true;
                   7211:                        }
                   7212:                } else {
1.1.1.3   root     7213:                        m_CF = 1;
1.1       root     7214:                }
                   7215:                break;
                   7216:        case 0x10:
                   7217:        case 0x11:
                   7218:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7219:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7220:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   7221:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7222:                        for(int i = 0; i < num; i++) {
                   7223:                                mem[ofs++] = scr_char[i];
                   7224:                                mem[ofs++] = scr_attr[i];
1.1.1.45  root     7225:                                if(REG8(AL) & 0x01) {
1.1       root     7226:                                        mem[ofs++] = 0;
                   7227:                                        mem[ofs++] = 0;
                   7228:                                }
                   7229:                        }
                   7230:                } else {
1.1.1.16  root     7231:                        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     7232:                                mem[ofs++] = mem[src++];
                   7233:                                mem[ofs++] = mem[src++];
1.1.1.45  root     7234:                                if(REG8(AL) & 0x01) {
1.1       root     7235:                                        mem[ofs++] = 0;
                   7236:                                        mem[ofs++] = 0;
                   7237:                                }
1.1.1.14  root     7238:                                if(++co.X == scr_width) {
                   7239:                                        if(++co.Y == scr_height) {
1.1       root     7240:                                                break;
                   7241:                                        }
                   7242:                                        co.X = 0;
                   7243:                                }
                   7244:                        }
                   7245:                }
                   7246:                break;
1.1.1.45  root     7247:        case 0x12: // ???
                   7248:        case 0x13: // ???
1.1       root     7249:        case 0x20:
                   7250:        case 0x21:
                   7251:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7252:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7253:                        int len = min(REG16(CX), scr_width * scr_height);
                   7254:                        for(int i = 0; i < len; i++) {
1.1       root     7255:                                scr_char[i] = mem[ofs++];
                   7256:                                scr_attr[i] = mem[ofs++];
1.1.1.45  root     7257:                                if(REG8(AL) & 0x01) {
1.1       root     7258:                                        ofs += 2;
                   7259:                                }
                   7260:                        }
1.1.1.14  root     7261:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7262:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7263:                } else {
1.1.1.16  root     7264:                        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     7265:                                mem[dest++] = mem[ofs++];
                   7266:                                mem[dest++] = mem[ofs++];
1.1.1.45  root     7267:                                if(REG8(AL) & 0x01) {
1.1       root     7268:                                        ofs += 2;
                   7269:                                }
1.1.1.14  root     7270:                                if(++co.X == scr_width) {
                   7271:                                        if(++co.Y == scr_height) {
1.1       root     7272:                                                break;
                   7273:                                        }
                   7274:                                        co.X = 0;
                   7275:                                }
                   7276:                        }
                   7277:                }
                   7278:                break;
                   7279:        default:
1.1.1.22  root     7280:                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     7281:                m_CF = 1;
1.1       root     7282:                break;
                   7283:        }
                   7284: }
                   7285: 
1.1.1.30  root     7286: inline void pcbios_int_10h_18h()
                   7287: {
                   7288:        switch(REG8(AL)) {
                   7289:        case 0x00:
                   7290:        case 0x01:
                   7291: //             REG8(AL) = 0x86;
                   7292:                REG8(AL) = 0x00;
                   7293:                break;
                   7294:        default:
                   7295:                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));
                   7296:                m_CF = 1;
                   7297:                break;
                   7298:        }
                   7299: }
                   7300: 
1.1.1.14  root     7301: inline void pcbios_int_10h_1ah()
                   7302: {
                   7303:        switch(REG8(AL)) {
                   7304:        case 0x00:
                   7305:                REG8(AL) = 0x1a;
                   7306:                REG8(BL) = 0x08;
                   7307:                REG8(BH) = 0x00;
                   7308:                break;
                   7309:        default:
1.1.1.22  root     7310:                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     7311:                m_CF = 1;
                   7312:                break;
                   7313:        }
                   7314: }
                   7315: 
1.1       root     7316: inline void pcbios_int_10h_1dh()
                   7317: {
                   7318:        switch(REG8(AL)) {
1.1.1.43  root     7319:        case 0x00:
                   7320:                // DOS/V Shift Status Line Control is not supported
                   7321:                m_CF = 1;
                   7322:                break;
1.1       root     7323:        case 0x01:
                   7324:                break;
                   7325:        case 0x02:
                   7326:                REG16(BX) = 0;
                   7327:                break;
                   7328:        default:
1.1.1.22  root     7329:                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));
                   7330:                m_CF = 1;
                   7331:                break;
                   7332:        }
                   7333: }
                   7334: 
                   7335: inline void pcbios_int_10h_4fh()
                   7336: {
                   7337:        switch(REG8(AL)) {
                   7338:        case 0x00:
                   7339:                REG8(AH) = 0x02; // not supported
                   7340:                break;
                   7341:        case 0x01:
                   7342:        case 0x02:
                   7343:        case 0x03:
                   7344:        case 0x04:
                   7345:        case 0x05:
                   7346:        case 0x06:
                   7347:        case 0x07:
                   7348:        case 0x08:
                   7349:        case 0x09:
                   7350:        case 0x0a:
                   7351:        case 0x0b:
                   7352:        case 0x0c:
                   7353:                REG8(AH) = 0x01; // failed
                   7354:                break;
                   7355:        default:
                   7356:                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     7357:                m_CF = 1;
1.1       root     7358:                break;
                   7359:        }
                   7360: }
                   7361: 
                   7362: inline void pcbios_int_10h_82h()
                   7363: {
                   7364:        static UINT8 mode = 0;
                   7365:        
                   7366:        switch(REG8(AL)) {
1.1.1.22  root     7367:        case 0x00:
1.1       root     7368:                if(REG8(BL) != 0xff) {
                   7369:                        mode = REG8(BL);
                   7370:                }
                   7371:                REG8(AL) = mode;
                   7372:                break;
                   7373:        default:
1.1.1.22  root     7374:                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     7375:                m_CF = 1;
1.1       root     7376:                break;
                   7377:        }
                   7378: }
                   7379: 
1.1.1.22  root     7380: inline void pcbios_int_10h_83h()
                   7381: {
                   7382:        static UINT8 mode = 0;
                   7383:        
                   7384:        switch(REG8(AL)) {
                   7385:        case 0x00:
                   7386:                REG16(AX) = 0; // offset???
                   7387:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7388:                i386_load_segment_descriptor(ES);
                   7389:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7390:                break;
                   7391:        default:
                   7392:                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));
                   7393:                m_CF = 1;
                   7394:                break;
                   7395:        }
                   7396: }
                   7397: 
                   7398: inline void pcbios_int_10h_90h()
                   7399: {
                   7400:        REG8(AL) = mem[0x449];
                   7401: }
                   7402: 
                   7403: inline void pcbios_int_10h_91h()
                   7404: {
                   7405:        REG8(AL) = 0x04; // VGA
                   7406: }
                   7407: 
                   7408: inline void pcbios_int_10h_efh()
                   7409: {
                   7410:        REG16(DX) = 0xffff;
                   7411: }
                   7412: 
1.1       root     7413: inline void pcbios_int_10h_feh()
                   7414: {
                   7415:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7416:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7417:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7418:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7419:        }
                   7420:        int_10h_feh_called = true;
                   7421: }
                   7422: 
                   7423: inline void pcbios_int_10h_ffh()
                   7424: {
                   7425:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7426:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7427:                COORD co;
                   7428:                DWORD num;
                   7429:                
1.1.1.14  root     7430:                vram_flush();
                   7431:                
                   7432:                co.X = (REG16(DI) >> 1) % scr_width;
                   7433:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7434:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7435:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7436:                int len;
                   7437:                for(len = 0; ofs < end; len++) {
                   7438:                        scr_char[len] = mem[ofs++];
                   7439:                        scr_attr[len] = mem[ofs++];
                   7440:                }
                   7441:                co.Y += scr_top;
                   7442:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7443:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7444:        }
                   7445:        int_10h_ffh_called = true;
                   7446: }
                   7447: 
1.1.1.42  root     7448: int pcbios_update_drive_param(int drive_num, int force_update)
                   7449: {
                   7450:        if(drive_num >= 0 && drive_num < 26) {
                   7451:                drive_param_t *drive_param = &drive_params[drive_num];
                   7452:                
                   7453:                if(force_update || !drive_param->initialized) {
                   7454:                        drive_param->valid = 0;
                   7455:                        char dev[64];
                   7456:                        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7457:                        
                   7458:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   7459:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7460:                                DWORD dwSize;
                   7461:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
                   7462:                                        drive_param->valid = 1;
                   7463:                                }
                   7464:                                CloseHandle(hFile);
                   7465:                        }
                   7466:                        drive_param->initialized = 1;
                   7467:                }
                   7468:                return(drive_param->valid);
                   7469:        }
                   7470:        return(0);
                   7471: }
                   7472: 
                   7473: inline void pcbios_int_13h_00h()
                   7474: {
                   7475:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7476:        
                   7477:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7478:                REG8(AH) = 0x00; // successful completion
                   7479:        } else {
                   7480:                if(REG8(DL) & 0x80) {
                   7481:                        REG8(AH) = 0x05; // reset failed (hard disk)
                   7482:                } else {
                   7483:                        REG8(AH) = 0x80; // timeout (not ready)
                   7484:                }
                   7485:                m_CF = 1;
                   7486:        }
                   7487: }
                   7488: 
                   7489: inline void pcbios_int_13h_02h()
                   7490: {
                   7491:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7492:        
                   7493:        if(REG8(AL) == 0) {
                   7494:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7495:                m_CF = 1;
                   7496:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7497:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7498:                m_CF = 1;
                   7499:        } else {
                   7500:                drive_param_t *drive_param = &drive_params[drive_num];
                   7501:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7502:                char dev[64];
                   7503:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7504:                
                   7505:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7506:                if(hFile == INVALID_HANDLE_VALUE) {
                   7507:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7508:                        m_CF = 1;
                   7509:                } else {
                   7510:                        UINT32 sector_num = REG8(AL);
                   7511:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7512:                        UINT32 head = REG8(DH);
                   7513:                        UINT32 sector = REG8(CL) & 0x3f;
                   7514:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7515:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7516:                        DWORD dwSize;
                   7517:                        
                   7518: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7519: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7520: //                             m_CF = 1;
                   7521: //                     } else 
                   7522:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7523:                                REG8(AH) = 0x04; // sector not found/read error
                   7524:                                m_CF = 1;
                   7525:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7526:                                REG8(AH) = 0x04; // sector not found/read error
                   7527:                                m_CF = 1;
                   7528:                        } else {
                   7529:                                REG8(AH) = 0x00; // successful completion
                   7530:                        }
                   7531:                        CloseHandle(hFile);
                   7532:                }
                   7533:        }
                   7534: }
                   7535: 
                   7536: inline void pcbios_int_13h_03h()
                   7537: {
                   7538:        // this operation may cause serious damage for drives, so support only floppy disk...
                   7539:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7540:        
                   7541:        if(REG8(AL) == 0) {
                   7542:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7543:                m_CF = 1;
                   7544:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7545:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7546:                m_CF = 1;
                   7547:        } else if(!drive_params[drive_num].is_fdd()) {
                   7548:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7549:                m_CF = 1;
                   7550:        } else {
                   7551:                drive_param_t *drive_param = &drive_params[drive_num];
                   7552:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7553:                char dev[64];
                   7554:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7555:                
                   7556:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7557:                if(hFile == INVALID_HANDLE_VALUE) {
                   7558:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7559:                        m_CF = 1;
                   7560:                } else {
                   7561:                        UINT32 sector_num = REG8(AL);
                   7562:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7563:                        UINT32 head = REG8(DH);
                   7564:                        UINT32 sector = REG8(CL) & 0x3f;
                   7565:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7566:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7567:                        DWORD dwSize;
                   7568:                        
                   7569: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7570: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7571: //                             m_CF = 1;
                   7572: //                     } else 
                   7573:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7574:                                REG8(AH) = 0x04; // sector not found/read error
                   7575:                                m_CF = 1;
                   7576:                        } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7577:                                REG8(AH) = 0x04; // sector not found/read error
                   7578:                                m_CF = 1;
                   7579:                        } else {
                   7580:                                REG8(AH) = 0x00; // successful completion
                   7581:                        }
                   7582:                        CloseHandle(hFile);
                   7583:                }
                   7584:        }
                   7585: }
                   7586: 
                   7587: inline void pcbios_int_13h_04h()
                   7588: {
                   7589:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7590:        
                   7591:        if(REG8(AL) == 0) {
                   7592:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7593:                m_CF = 1;
                   7594:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7595:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7596:                m_CF = 1;
                   7597:        } else {
                   7598:                drive_param_t *drive_param = &drive_params[drive_num];
                   7599:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7600:                char dev[64];
                   7601:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7602:                
                   7603:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7604:                if(hFile == INVALID_HANDLE_VALUE) {
                   7605:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7606:                        m_CF = 1;
                   7607:                } else {
                   7608:                        UINT32 sector_num = REG8(AL);
                   7609:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7610:                        UINT32 head = REG8(DH);
                   7611:                        UINT32 sector = REG8(CL) & 0x3f;
                   7612:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7613:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7614:                        DWORD dwSize;
                   7615:                        UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
                   7616:                        
                   7617: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7618: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7619: //                             m_CF = 1;
                   7620: //                     } else 
                   7621:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7622:                                REG8(AH) = 0x04; // sector not found/read error
                   7623:                                m_CF = 1;
                   7624:                        } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7625:                                REG8(AH) = 0x04; // sector not found/read error
                   7626:                                m_CF = 1;
                   7627:                        } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
                   7628:                                REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
                   7629:                                m_CF = 1;
                   7630:                        } else {
                   7631:                                REG8(AH) = 0x00; // successful completion
                   7632:                        }
                   7633:                        free(tmp_buffer);
                   7634:                        CloseHandle(hFile);
                   7635:                }
                   7636:        }
                   7637: }
                   7638: 
                   7639: inline void pcbios_int_13h_08h()
                   7640: {
                   7641:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7642:        
                   7643:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7644:                drive_param_t *drive_param = &drive_params[drive_num];
                   7645:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7646:                
                   7647:                REG16(AX) = 0x0000;
                   7648:                switch(geo->MediaType) {
                   7649:                case F5_360_512:
                   7650:                case F5_320_512:
                   7651:                case F5_320_1024:
                   7652:                case F5_180_512:
                   7653:                case F5_160_512:
                   7654:                        REG8(BL) = 0x01; // 320K/360K disk
                   7655:                        break;
                   7656:                case F5_1Pt2_512:
                   7657:                case F3_1Pt2_512:
                   7658:                case F3_1Pt23_1024:
                   7659:                case F5_1Pt23_1024:
                   7660:                        REG8(BL) = 0x02; // 1.2M disk
                   7661:                        break;
                   7662:                case F3_720_512:
                   7663:                case F3_640_512:
                   7664:                case F5_640_512:
                   7665:                case F5_720_512:
                   7666:                        REG8(BL) = 0x03; // 720K disk
                   7667:                        break;
                   7668:                case F3_1Pt44_512:
                   7669:                        REG8(BL) = 0x04; // 1.44M disk
                   7670:                        break;
                   7671:                case F3_2Pt88_512:
                   7672:                        REG8(BL) = 0x06; // 2.88M disk
                   7673:                        break;
                   7674:                case RemovableMedia:
                   7675:                        REG8(BL) = 0x10; // ATAPI Removable Media Device
                   7676:                        break;
                   7677:                default:
                   7678:                        REG8(BL) = 0x00; // unknown
                   7679:                        break;
                   7680:                }
                   7681:                if(REG8(DL) & 0x80) {
                   7682:                        switch(GetLogicalDrives() & 0x0c) {
                   7683:                        case 0x00: REG8(DL) = 0x00; break;
                   7684:                        case 0x04:
                   7685:                        case 0x08: REG8(DL) = 0x01; break;
                   7686:                        case 0x0c: REG8(DL) = 0x02; break;
                   7687:                        }
                   7688:                } else {
                   7689:                        switch(GetLogicalDrives() & 0x03) {
                   7690:                        case 0x00: REG8(DL) = 0x00; break;
                   7691:                        case 0x01:
                   7692:                        case 0x02: REG8(DL) = 0x01; break;
                   7693:                        case 0x03: REG8(DL) = 0x02; break;
                   7694:                        }
                   7695:                }
                   7696:                REG8(DH) = drive_param->head_num();
                   7697:                int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
                   7698:                int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
                   7699:                REG8(CH) = cyl & 0xff;
                   7700:                REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
                   7701:        } else {
                   7702:                REG8(AH) = 0x07;
                   7703:                m_CF = 1;
                   7704:        }
                   7705: }
                   7706: 
                   7707: inline void pcbios_int_13h_10h()
                   7708: {
                   7709:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7710:        
                   7711:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7712:                REG8(AH) = 0x00; // successful completion
                   7713:        } else {
                   7714:                if(REG8(DL) & 0x80) {
                   7715:                        REG8(AH) = 0xaa; // drive not ready (hard disk)
                   7716:                } else {
                   7717:                        REG8(AH) = 0x80; // timeout (not ready)
                   7718:                }
                   7719:                m_CF = 1;
                   7720:        }
                   7721: }
                   7722: 
                   7723: inline void pcbios_int_13h_15h()
                   7724: {
                   7725:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7726:        
                   7727:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7728:                if(REG8(DL) & 0x80) {
                   7729:                        REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
                   7730:                } else {
                   7731:                        REG8(AH) = 0x03; // hard disk
                   7732:                }
                   7733:        } else {
                   7734:                REG8(AH) = 0x00; // no such drive
                   7735:        }
                   7736: }
                   7737: 
1.1.1.43  root     7738: inline void pcbios_int_13h_41h()
                   7739: {
                   7740:        if(REG16(BX) == 0x55aa) {
                   7741:                // IBM/MS INT 13 Extensions is not installed
                   7742:                REG8(AH) = 0x01;
                   7743:                m_CF = 1;
                   7744:        } else {
                   7745:                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));
                   7746:                REG8(AH) = 0x01;
                   7747:                m_CF = 1;
                   7748:        }
                   7749: }
                   7750: 
1.1.1.25  root     7751: inline void pcbios_int_14h_00h()
                   7752: {
1.1.1.29  root     7753:        if(REG16(DX) < 4) {
1.1.1.25  root     7754:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   7755:                UINT8 selector = sio_read(REG16(DX), 3);
                   7756:                selector &= ~0x3f;
                   7757:                selector |= REG8(AL) & 0x1f;
                   7758:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   7759:                sio_write(REG16(DX), 3, selector | 0x80);
                   7760:                sio_write(REG16(DX), 0, divisor & 0xff);
                   7761:                sio_write(REG16(DX), 1, divisor >> 8);
                   7762:                sio_write(REG16(DX), 3, selector);
                   7763:                REG8(AH) = sio_read(REG16(DX), 5);
                   7764:                REG8(AL) = sio_read(REG16(DX), 6);
                   7765:        } else {
                   7766:                REG8(AH) = 0x80;
                   7767:        }
                   7768: }
                   7769: 
                   7770: inline void pcbios_int_14h_01h()
                   7771: {
1.1.1.29  root     7772:        if(REG16(DX) < 4) {
1.1.1.25  root     7773:                UINT8 selector = sio_read(REG16(DX), 3);
                   7774:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7775:                sio_write(REG16(DX), 0, REG8(AL));
                   7776:                sio_write(REG16(DX), 3, selector);
                   7777:                REG8(AH) = sio_read(REG16(DX), 5);
                   7778:        } else {
                   7779:                REG8(AH) = 0x80;
                   7780:        }
                   7781: }
                   7782: 
                   7783: inline void pcbios_int_14h_02h()
                   7784: {
1.1.1.29  root     7785:        if(REG16(DX) < 4) {
1.1.1.25  root     7786:                UINT8 selector = sio_read(REG16(DX), 3);
                   7787:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7788:                REG8(AL) = sio_read(REG16(DX), 0);
                   7789:                sio_write(REG16(DX), 3, selector);
                   7790:                REG8(AH) = sio_read(REG16(DX), 5);
                   7791:        } else {
                   7792:                REG8(AH) = 0x80;
                   7793:        }
                   7794: }
                   7795: 
                   7796: inline void pcbios_int_14h_03h()
                   7797: {
1.1.1.29  root     7798:        if(REG16(DX) < 4) {
1.1.1.25  root     7799:                REG8(AH) = sio_read(REG16(DX), 5);
                   7800:                REG8(AL) = sio_read(REG16(DX), 6);
                   7801:        } else {
                   7802:                REG8(AH) = 0x80;
                   7803:        }
                   7804: }
                   7805: 
                   7806: inline void pcbios_int_14h_04h()
                   7807: {
1.1.1.29  root     7808:        if(REG16(DX) < 4) {
1.1.1.25  root     7809:                UINT8 selector = sio_read(REG16(DX), 3);
                   7810:                if(REG8(CH) <= 0x03) {
                   7811:                        selector = (selector & ~0x03) | REG8(CH);
                   7812:                }
                   7813:                if(REG8(BL) == 0x00) {
                   7814:                        selector &= ~0x04;
                   7815:                } else if(REG8(BL) == 0x01) {
                   7816:                        selector |= 0x04;
                   7817:                }
                   7818:                if(REG8(BH) == 0x00) {
                   7819:                        selector = (selector & ~0x38) | 0x00;
                   7820:                } else if(REG8(BH) == 0x01) {
                   7821:                        selector = (selector & ~0x38) | 0x08;
                   7822:                } else if(REG8(BH) == 0x02) {
                   7823:                        selector = (selector & ~0x38) | 0x18;
                   7824:                } else if(REG8(BH) == 0x03) {
                   7825:                        selector = (selector & ~0x38) | 0x28;
                   7826:                } else if(REG8(BH) == 0x04) {
                   7827:                        selector = (selector & ~0x38) | 0x38;
                   7828:                }
                   7829:                if(REG8(AL) == 0x00) {
                   7830:                        selector |= 0x40;
                   7831:                } else if(REG8(AL) == 0x01) {
                   7832:                        selector &= ~0x40;
                   7833:                }
                   7834:                if(REG8(CL) <= 0x0b) {
                   7835:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   7836:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   7837:                        sio_write(REG16(DX), 3, selector | 0x80);
                   7838:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   7839:                        sio_write(REG16(DX), 1, divisor >> 8);
                   7840:                }
                   7841:                sio_write(REG16(DX), 3, selector);
                   7842:                REG8(AH) = sio_read(REG16(DX), 5);
                   7843:                REG8(AL) = sio_read(REG16(DX), 6);
                   7844:        } else {
                   7845:                REG8(AH) = 0x80;
                   7846:        }
                   7847: }
                   7848: 
                   7849: inline void pcbios_int_14h_05h()
                   7850: {
1.1.1.29  root     7851:        if(REG16(DX) < 4) {
1.1.1.25  root     7852:                if(REG8(AL) == 0x00) {
                   7853:                        REG8(BL) = sio_read(REG16(DX), 4);
                   7854:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7855:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7856:                } else if(REG8(AL) == 0x01) {
                   7857:                        sio_write(REG16(DX), 4, REG8(BL));
                   7858:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7859:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7860:                } else {
                   7861:                        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));
                   7862:                }
                   7863:        } else {
                   7864:                REG8(AH) = 0x80;
                   7865:        }
                   7866: }
                   7867: 
1.1.1.14  root     7868: inline void pcbios_int_15h_10h()
                   7869: {
1.1.1.22  root     7870:        switch(REG8(AL)) {
                   7871:        case 0x00:
1.1.1.14  root     7872:                Sleep(10);
1.1.1.35  root     7873:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     7874:                break;
                   7875:        default:
                   7876:                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     7877:                REG8(AH) = 0x86;
                   7878:                m_CF = 1;
                   7879:        }
                   7880: }
                   7881: 
1.1       root     7882: inline void pcbios_int_15h_23h()
                   7883: {
                   7884:        switch(REG8(AL)) {
1.1.1.22  root     7885:        case 0x00:
1.1.1.8   root     7886:                REG8(CL) = cmos_read(0x2d);
                   7887:                REG8(CH) = cmos_read(0x2e);
1.1       root     7888:                break;
1.1.1.22  root     7889:        case 0x01:
1.1.1.8   root     7890:                cmos_write(0x2d, REG8(CL));
                   7891:                cmos_write(0x2e, REG8(CH));
1.1       root     7892:                break;
                   7893:        default:
1.1.1.22  root     7894:                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     7895:                REG8(AH) = 0x86;
1.1.1.3   root     7896:                m_CF = 1;
1.1       root     7897:                break;
                   7898:        }
                   7899: }
                   7900: 
                   7901: inline void pcbios_int_15h_24h()
                   7902: {
                   7903:        switch(REG8(AL)) {
1.1.1.22  root     7904:        case 0x00:
1.1.1.3   root     7905:                i386_set_a20_line(0);
1.1       root     7906:                REG8(AH) = 0;
                   7907:                break;
1.1.1.22  root     7908:        case 0x01:
1.1.1.3   root     7909:                i386_set_a20_line(1);
1.1       root     7910:                REG8(AH) = 0;
                   7911:                break;
1.1.1.22  root     7912:        case 0x02:
1.1       root     7913:                REG8(AH) = 0;
1.1.1.3   root     7914:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     7915:                REG16(CX) = 0;
                   7916:                break;
1.1.1.22  root     7917:        case 0x03:
1.1       root     7918:                REG16(AX) = 0;
                   7919:                REG16(BX) = 0;
                   7920:                break;
1.1.1.22  root     7921:        default:
                   7922:                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));
                   7923:                REG8(AH) = 0x86;
                   7924:                m_CF = 1;
                   7925:                break;
1.1       root     7926:        }
                   7927: }
                   7928: 
                   7929: inline void pcbios_int_15h_49h()
                   7930: {
1.1.1.27  root     7931:        REG8(AH) = 0x00;
                   7932:        REG8(BL) = 0x00; // DOS/V
1.1       root     7933: }
                   7934: 
1.1.1.22  root     7935: inline void pcbios_int_15h_50h()
                   7936: {
                   7937:        switch(REG8(AL)) {
                   7938:        case 0x00:
                   7939:        case 0x01:
                   7940:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   7941:                        REG8(AH) = 0x01; // invalid font type in bh
                   7942:                        m_CF = 1;
1.1.1.27  root     7943:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     7944:                        REG8(AH) = 0x02; // bl not zero
                   7945:                        m_CF = 1;
                   7946:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   7947:                        REG8(AH) = 0x04; // invalid code page
                   7948:                        m_CF = 1;
1.1.1.27  root     7949:                } else if(REG8(AL) == 0x01) {
                   7950:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     7951:                        m_CF = 1;
1.1.1.27  root     7952:                } else {
                   7953:                        // dummy font read routine is at fffd:000d
                   7954:                        SREG(ES) = 0xfffd;
                   7955:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     7956:                        REG16(BX) = 0x000d;
1.1.1.27  root     7957:                        REG8(AH) = 0x00; // success
1.1.1.22  root     7958:                }
                   7959:                break;
                   7960:        default:
                   7961:                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));
                   7962:                REG8(AH) = 0x86;
                   7963:                m_CF = 1;
                   7964:                break;
                   7965:        }
                   7966: }
                   7967: 
1.1.1.30  root     7968: inline void pcbios_int_15h_53h()
                   7969: {
                   7970:        switch(REG8(AL)) {
                   7971:        case 0x00:
                   7972:                // APM is not installed
                   7973:                REG8(AH) = 0x86;
                   7974:                m_CF = 1;
                   7975:                break;
                   7976:        default:
                   7977:                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));
                   7978:                REG8(AH) = 0x86;
                   7979:                m_CF = 1;
                   7980:                break;
                   7981:        }
                   7982: }
                   7983: 
1.1.1.43  root     7984: inline void pcbios_int_15h_84h()
                   7985: {
                   7986:        // joystick support (from DOSBox)
                   7987:        switch(REG16(DX)) {
                   7988:        case 0x00:
                   7989:                REG16(AX) = 0x00f0;
                   7990:                REG16(DX) = 0x0201;
                   7991:                m_CF = 1;
                   7992:                break;
                   7993:        case 0x01:
                   7994:                REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   7995:                m_CF = 1;
                   7996:                break;
                   7997:        default:
                   7998:                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));
                   7999:                REG8(AH) = 0x86;
                   8000:                m_CF = 1;
                   8001:                break;
                   8002:        }
                   8003: }
1.1.1.35  root     8004: 
                   8005: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     8006: {
                   8007:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     8008:        UINT32 msec = usec / 1000;
                   8009:        
1.1.1.35  root     8010:        while(msec && !m_halted) {
1.1.1.14  root     8011:                UINT32 tmp = min(msec, 100);
                   8012:                if(msec - tmp < 10) {
                   8013:                        tmp = msec;
                   8014:                }
                   8015:                Sleep(tmp);
                   8016:                msec -= tmp;
                   8017:        }
1.1.1.35  root     8018:        
                   8019: #ifdef USE_SERVICE_THREAD
                   8020:        service_exit = true;
                   8021: #endif
                   8022:        return(0);
                   8023: }
                   8024: 
                   8025: inline void pcbios_int_15h_86h()
                   8026: {
                   8027:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   8028: #ifdef USE_SERVICE_THREAD
                   8029:                start_service_loop(pcbios_int_15h_86h_thread);
                   8030: #else
                   8031:                pcbios_int_15h_86h_thread(NULL);
                   8032:                REQUEST_HARDWRE_UPDATE();
                   8033: #endif
                   8034:        }
1.1       root     8035: }
                   8036: 
                   8037: inline void pcbios_int_15h_87h()
                   8038: {
                   8039:        // copy extended memory (from DOSBox)
                   8040:        int len = REG16(CX) * 2;
1.1.1.3   root     8041:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     8042:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   8043:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   8044:        memcpy(mem + dst, mem + src, len);
                   8045:        REG16(AX) = 0x00;
                   8046: }
                   8047: 
                   8048: inline void pcbios_int_15h_88h()
                   8049: {
1.1.1.17  root     8050:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     8051: }
                   8052: 
                   8053: inline void pcbios_int_15h_89h()
                   8054: {
1.1.1.21  root     8055: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     8056:        // switch to protected mode (from DOSBox)
                   8057:        write_io_byte(0x20, 0x10);
                   8058:        write_io_byte(0x21, REG8(BH));
                   8059:        write_io_byte(0x21, 0x00);
                   8060:        write_io_byte(0xa0, 0x10);
                   8061:        write_io_byte(0xa1, REG8(BL));
                   8062:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     8063:        i386_set_a20_line(1);
                   8064:        int ofs = SREG_BASE(ES) + REG16(SI);
                   8065:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   8066:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   8067:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   8068:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   8069: #if defined(HAS_I386)
                   8070:        m_cr[0] |= 1;
                   8071: #else
                   8072:        m_msw |= 1;
                   8073: #endif
                   8074:        SREG(DS) = 0x18;
                   8075:        SREG(ES) = 0x20;
                   8076:        SREG(SS) = 0x28;
                   8077:        i386_load_segment_descriptor(DS);
                   8078:        i386_load_segment_descriptor(ES);
                   8079:        i386_load_segment_descriptor(SS);
1.1.1.21  root     8080:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     8081:        REG16(SP) += 6;
1.1.1.3   root     8082: #if defined(HAS_I386)
1.1.1.21  root     8083:        UINT32 flags = get_flags();
                   8084:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8085:        set_flags(flags);
1.1.1.3   root     8086: #else
1.1.1.21  root     8087:        UINT32 flags = CompressFlags();
                   8088:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8089:        ExpandFlags(flags);
1.1.1.3   root     8090: #endif
1.1       root     8091:        REG16(AX) = 0x00;
1.1.1.21  root     8092:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     8093: #else
1.1.1.21  root     8094:        // i86/i186/v30: protected mode is not supported
1.1       root     8095:        REG8(AH) = 0x86;
1.1.1.3   root     8096:        m_CF = 1;
1.1       root     8097: #endif
                   8098: }
                   8099: 
1.1.1.21  root     8100: inline void pcbios_int_15h_8ah()
                   8101: {
                   8102:        UINT32 size = MAX_MEM - 0x100000;
                   8103:        REG16(AX) = size & 0xffff;
                   8104:        REG16(DX) = size >> 16;
                   8105: }
                   8106: 
1.1.1.3   root     8107: #if defined(HAS_I386)
1.1       root     8108: inline void pcbios_int_15h_c9h()
                   8109: {
                   8110:        REG8(AH) = 0x00;
                   8111:        REG8(CH) = cpu_type;
                   8112:        REG8(CL) = cpu_step;
                   8113: }
1.1.1.3   root     8114: #endif
1.1       root     8115: 
                   8116: inline void pcbios_int_15h_cah()
                   8117: {
                   8118:        switch(REG8(AL)) {
1.1.1.22  root     8119:        case 0x00:
1.1       root     8120:                if(REG8(BL) > 0x3f) {
                   8121:                        REG8(AH) = 0x03;
1.1.1.3   root     8122:                        m_CF = 1;
1.1       root     8123:                } else if(REG8(BL) < 0x0e) {
                   8124:                        REG8(AH) = 0x04;
1.1.1.3   root     8125:                        m_CF = 1;
1.1       root     8126:                } else {
1.1.1.8   root     8127:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     8128:                }
                   8129:                break;
1.1.1.22  root     8130:        case 0x01:
1.1       root     8131:                if(REG8(BL) > 0x3f) {
                   8132:                        REG8(AH) = 0x03;
1.1.1.3   root     8133:                        m_CF = 1;
1.1       root     8134:                } else if(REG8(BL) < 0x0e) {
                   8135:                        REG8(AH) = 0x04;
1.1.1.3   root     8136:                        m_CF = 1;
1.1       root     8137:                } else {
1.1.1.8   root     8138:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     8139:                }
                   8140:                break;
                   8141:        default:
1.1.1.22  root     8142:                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     8143:                REG8(AH) = 0x86;
1.1.1.3   root     8144:                m_CF = 1;
1.1       root     8145:                break;
                   8146:        }
                   8147: }
                   8148: 
1.1.1.22  root     8149: inline void pcbios_int_15h_e8h()
1.1.1.17  root     8150: {
1.1.1.22  root     8151:        switch(REG8(AL)) {
                   8152: #if defined(HAS_I386)
                   8153:        case 0x01:
                   8154:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   8155:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8156:                break;
1.1.1.17  root     8157: #endif
1.1.1.22  root     8158:        default:
                   8159:                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));
                   8160:                REG8(AH) = 0x86;
                   8161:                m_CF = 1;
                   8162:                break;
                   8163:        }
                   8164: }
1.1.1.17  root     8165: 
1.1.1.33  root     8166: void pcbios_update_key_code(bool wait)
1.1       root     8167: {
1.1.1.32  root     8168:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8169: #ifdef USE_SERVICE_THREAD
                   8170:                EnterCriticalSection(&key_buf_crit_sect);
                   8171: #endif
                   8172:                bool empty = key_buf_char->empty();
                   8173: #ifdef USE_SERVICE_THREAD
                   8174:                LeaveCriticalSection(&key_buf_crit_sect);
                   8175: #endif
                   8176:                if(empty) {
1.1.1.32  root     8177:                        if(!update_key_buffer()) {
1.1.1.33  root     8178:                                if(wait) {
1.1.1.32  root     8179:                                        Sleep(10);
                   8180:                                } else {
                   8181:                                        maybe_idle();
                   8182:                                }
1.1.1.14  root     8183:                        }
                   8184:                }
1.1.1.34  root     8185:        }
                   8186:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8187: #ifdef USE_SERVICE_THREAD
                   8188:                EnterCriticalSection(&key_buf_crit_sect);
                   8189: #endif
1.1.1.32  root     8190:                if(key_buf_char->count() != 0) {
1.1.1.41  root     8191:                        int key_char = key_buf_char->read();
                   8192:                        int key_scan = key_buf_scan->read();
                   8193:                        key_code  = key_char << 0;
                   8194:                        key_code |= key_scan << 8;
1.1.1.35  root     8195:                        key_recv  = 0x0000ffff;
1.1.1.41  root     8196:                        // write to bottom of key buffer
                   8197:                        mem[0x43c] = (UINT8)key_char;
                   8198:                        mem[0x43d] = (UINT8)key_scan;
1.1.1.32  root     8199:                }
                   8200:                if(key_buf_char->count() != 0) {
1.1.1.41  root     8201:                        int key_char = key_buf_char->read();
                   8202:                        int key_scan = key_buf_scan->read();
                   8203:                        key_code |= key_char << 16;
                   8204:                        key_code |= key_scan << 24;
1.1.1.33  root     8205:                        key_recv |= 0xffff0000;
1.1.1.41  root     8206:                        // write to bottom of key buffer
                   8207:                        mem[0x43c] = (UINT8)key_char;
                   8208:                        mem[0x43d] = (UINT8)key_scan;
1.1.1.32  root     8209:                }
1.1.1.35  root     8210: #ifdef USE_SERVICE_THREAD
                   8211:                LeaveCriticalSection(&key_buf_crit_sect);
                   8212: #endif
1.1       root     8213:        }
                   8214: }
                   8215: 
1.1.1.35  root     8216: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     8217: {
1.1.1.33  root     8218:        while(key_recv == 0 && !m_halted) {
                   8219:                pcbios_update_key_code(true);
1.1       root     8220:        }
1.1.1.33  root     8221:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8222:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   8223:                        if(REG8(AH) == 0x10) {
                   8224:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   8225:                        } else {
                   8226:                                key_code = ((key_code >> 16) & 0xff00);
                   8227:                        }
                   8228:                        key_recv >>= 16;
1.1       root     8229:                }
                   8230:        }
                   8231:        REG16(AX) = key_code & 0xffff;
                   8232:        key_code >>= 16;
1.1.1.33  root     8233:        key_recv >>= 16;
1.1.1.35  root     8234:        
                   8235: #ifdef USE_SERVICE_THREAD
                   8236:        service_exit = true;
                   8237: #endif
                   8238:        return(0);
                   8239: }
                   8240: 
                   8241: inline void pcbios_int_16h_00h()
                   8242: {
                   8243: #ifdef USE_SERVICE_THREAD
                   8244:        start_service_loop(pcbios_int_16h_00h_thread);
                   8245: #else
                   8246:        pcbios_int_16h_00h_thread(NULL);
                   8247:        REQUEST_HARDWRE_UPDATE();
                   8248: #endif
1.1       root     8249: }
                   8250: 
                   8251: inline void pcbios_int_16h_01h()
                   8252: {
1.1.1.33  root     8253:        if(key_recv == 0) {
                   8254:                pcbios_update_key_code(false);
1.1.1.5   root     8255:        }
1.1.1.33  root     8256:        if(key_recv != 0) {
                   8257:                UINT32 key_code_tmp = key_code;
                   8258:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8259:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   8260:                                if(REG8(AH) == 0x11) {
                   8261:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   8262:                                } else {
                   8263:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   8264:                                }
                   8265:                        }
1.1       root     8266:                }
1.1.1.5   root     8267:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     8268: #if defined(HAS_I386)
1.1.1.33  root     8269:                m_ZF = 0;
                   8270: #else
                   8271:                m_ZeroVal = 1;
                   8272: #endif
                   8273:        } else {
                   8274: #if defined(HAS_I386)
                   8275:                m_ZF = 1;
1.1.1.3   root     8276: #else
1.1.1.33  root     8277:                m_ZeroVal = 0;
1.1.1.3   root     8278: #endif
1.1.1.33  root     8279:        }
1.1       root     8280: }
                   8281: 
                   8282: inline void pcbios_int_16h_02h()
                   8283: {
                   8284:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   8285:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   8286:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   8287:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   8288:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   8289:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   8290:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   8291:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   8292: }
                   8293: 
                   8294: inline void pcbios_int_16h_03h()
                   8295: {
                   8296:        static UINT16 status = 0;
                   8297:        
                   8298:        switch(REG8(AL)) {
                   8299:        case 0x05:
                   8300:                status = REG16(BX);
                   8301:                break;
                   8302:        case 0x06:
                   8303:                REG16(BX) = status;
                   8304:                break;
                   8305:        default:
1.1.1.3   root     8306:                m_CF = 1;
1.1       root     8307:                break;
                   8308:        }
                   8309: }
                   8310: 
                   8311: inline void pcbios_int_16h_05h()
                   8312: {
1.1.1.32  root     8313:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8314: #ifdef USE_SERVICE_THREAD
                   8315:                EnterCriticalSection(&key_buf_crit_sect);
                   8316: #endif
1.1.1.32  root     8317:                key_buf_char->write(REG8(CL));
                   8318:                key_buf_scan->write(REG8(CH));
1.1.1.35  root     8319: #ifdef USE_SERVICE_THREAD
                   8320:                LeaveCriticalSection(&key_buf_crit_sect);
                   8321: #endif
1.1.1.32  root     8322:        }
1.1       root     8323:        REG8(AL) = 0x00;
                   8324: }
                   8325: 
                   8326: inline void pcbios_int_16h_12h()
                   8327: {
                   8328:        pcbios_int_16h_02h();
                   8329:        
                   8330:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   8331:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   8332:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   8333:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   8334:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   8335:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   8336:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   8337:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   8338: }
                   8339: 
                   8340: inline void pcbios_int_16h_13h()
                   8341: {
                   8342:        static UINT16 status = 0;
                   8343:        
                   8344:        switch(REG8(AL)) {
                   8345:        case 0x00:
                   8346:                status = REG16(DX);
                   8347:                break;
                   8348:        case 0x01:
                   8349:                REG16(DX) = status;
                   8350:                break;
                   8351:        default:
1.1.1.22  root     8352:                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     8353:                m_CF = 1;
1.1       root     8354:                break;
                   8355:        }
                   8356: }
                   8357: 
                   8358: inline void pcbios_int_16h_14h()
                   8359: {
                   8360:        static UINT8 status = 0;
                   8361:        
                   8362:        switch(REG8(AL)) {
                   8363:        case 0x00:
                   8364:        case 0x01:
                   8365:                status = REG8(AL);
                   8366:                break;
                   8367:        case 0x02:
                   8368:                REG8(AL) = status;
                   8369:                break;
                   8370:        default:
1.1.1.22  root     8371:                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     8372:                m_CF = 1;
1.1       root     8373:                break;
                   8374:        }
                   8375: }
                   8376: 
1.1.1.24  root     8377: inline void pcbios_int_16h_55h()
                   8378: {
                   8379:        switch(REG8(AL)) {
                   8380:        case 0x00:
                   8381:                // keyboard tsr is not present
                   8382:                break;
                   8383:        case 0xfe:
                   8384:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   8385:                break;
                   8386:        case 0xff:
                   8387:                break;
                   8388:        default:
                   8389:                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));
                   8390:                m_CF = 1;
                   8391:                break;
                   8392:        }
                   8393: }
                   8394: 
1.1.1.30  root     8395: inline void pcbios_int_16h_6fh()
                   8396: {
                   8397:        switch(REG8(AL)) {
                   8398:        case 0x00:
                   8399:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   8400:                break;
                   8401:        default:
                   8402:                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));
                   8403:                m_CF = 1;
                   8404:                break;
                   8405:        }
                   8406: }
                   8407: 
1.1.1.37  root     8408: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   8409: {
                   8410:        UINT8 hi = jis >> 8;
                   8411:        UINT8 lo = jis & 0xff;
                   8412:        
                   8413:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   8414:        hi = (hi - 0x21) / 2 + 0x81;
                   8415:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   8416:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   8417:        
                   8418:        return((hi << 8) + lo);
                   8419: }
                   8420: 
                   8421: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   8422: {
                   8423:        UINT8 hi = sjis >> 8;
                   8424:        UINT8 lo = sjis & 0xff;
                   8425:        
                   8426:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   8427:                return(0x2121);
                   8428:        }
                   8429:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   8430:                return(0x2121);
                   8431:        }
                   8432:        if(hi >= 0xf0 && hi <= 0xf3) {
                   8433:                // gaiji
                   8434:                if(lo >= 0x40 && lo <= 0x7e) {
                   8435:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   8436:                }
                   8437:                if(lo >= 0x80 && lo <= 0x9e) {
                   8438:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   8439:                }
                   8440:                if(lo >= 0x9f && lo <= 0xfc) {
                   8441:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   8442:                }
                   8443:        }
                   8444:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   8445:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   8446:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   8447:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   8448:        
                   8449:        return((hi << 8) + lo);
                   8450: }
                   8451: 
1.1.1.38  root     8452: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   8453: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   8454: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     8455: 
                   8456: void pcbios_printer_out(int c, UINT8 data)
                   8457: {
                   8458:        if(pio[c].conv_mode) {
                   8459:                if(pio[c].sjis_hi != 0) {
                   8460:                        if(!pio[c].jis_mode) {
                   8461:                                printer_out(c, 0x1c);
                   8462:                                printer_out(c, 0x26);
                   8463:                                pio[c].jis_mode = true;
                   8464:                        }
                   8465:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   8466:                        printer_out(c, jis >> 8);
                   8467:                        printer_out(c, jis & 0xff);
                   8468:                        pio[c].sjis_hi = 0;
                   8469:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   8470:                        printer_out(c, data);
                   8471:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8472:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8473:                        }
                   8474:                        pio[c].esc_len++;
                   8475:                        
                   8476:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8477:                        case 0x33: // 1Bh 33h XX
                   8478:                        case 0x4a: // 1Bh 4Ah XX
                   8479:                        case 0x4e: // 1Bh 4Eh XX
                   8480:                        case 0x51: // 1Bh 51h XX
                   8481:                        case 0x55: // 1Bh 55h XX
                   8482:                        case 0x6c: // 1Bh 6Ch XX
                   8483:                        case 0x71: // 1Bh 71h XX
                   8484:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     8485:                                if(pio[c].esc_len == 3) {
                   8486:                                        pio[c].esc_buf[0] = 0x00;
                   8487:                                }
                   8488:                                break;
1.1.1.38  root     8489:                        case 0x24: // 1Bh 24h XX XX
                   8490:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     8491:                                if(pio[c].esc_len == 4) {
                   8492:                                        pio[c].esc_buf[0] = 0x00;
                   8493:                                }
                   8494:                                break;
1.1.1.38  root     8495:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     8496:                                if(pio[c].esc_len >= 3) {
                   8497:                                        switch(pio[c].esc_buf[2]) {
                   8498:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   8499:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   8500:                                                        pio[c].esc_buf[0] = 0x00;
                   8501:                                                }
                   8502:                                                break;
                   8503:                                        case 32: case 33: case 38: case 39: case 40:
                   8504:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   8505:                                                        pio[c].esc_buf[0] = 0x00;
                   8506:                                                }
                   8507:                                                break;
1.1.1.38  root     8508:                                        case 71: case 72: case 73:
                   8509:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   8510:                                                        pio[c].esc_buf[0] = 0x00;
                   8511:                                                }
                   8512:                                                break;
1.1.1.37  root     8513:                                        default:
                   8514:                                                pio[c].esc_buf[0] = 0x00;
                   8515:                                                break;
                   8516:                                        }
                   8517:                                }
                   8518:                                break;
1.1.1.38  root     8519:                        case 0x40: // 1Bh 40h
1.1.1.37  root     8520:                                if(pio[c].jis_mode) {
                   8521:                                        printer_out(c, 0x1c);
                   8522:                                        printer_out(c, 0x2e);
                   8523:                                        pio[c].jis_mode = false;
                   8524:                                }
                   8525:                                pio[c].esc_buf[0] = 0x00;
                   8526:                                break;
1.1.1.38  root     8527:                        case 0x42: // 1Bh 42h data 00h
                   8528:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8529:                                if(pio[c].esc_len >= 3 && data == 0) {
                   8530:                                        pio[c].esc_buf[0] = 0x00;
                   8531:                                }
                   8532:                                break;
1.1.1.38  root     8533:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     8534:                                if(pio[c].esc_len >= 3 && data != 0) {
                   8535:                                        pio[c].esc_buf[0] = 0x00;
                   8536:                                }
                   8537:                                break;
1.1.1.38  root     8538:                        default: // 1Bh XX
1.1.1.37  root     8539:                                pio[c].esc_buf[0] = 0x00;
                   8540:                                break;
                   8541:                        }
                   8542:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   8543:                        printer_out(c, data);
                   8544:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8545:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8546:                        }
                   8547:                        pio[c].esc_len++;
                   8548:                        
                   8549:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8550:                        case 0x21: // 1Ch 21h XX
                   8551:                        case 0x2d: // 1Ch 2Dh XX
                   8552:                        case 0x57: // 1Ch 57h XX
                   8553:                        case 0x6b: // 1Ch 6Bh XX
                   8554:                        case 0x72: // 1Ch 72h XX
                   8555:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     8556:                                if(pio[c].esc_len == 3) {
                   8557:                                        pio[c].esc_buf[0] = 0x00;
                   8558:                                }
                   8559:                                break;
1.1.1.38  root     8560:                        case 0x26: // 1Ch 26h
1.1.1.37  root     8561:                                pio[c].jis_mode = true;
                   8562:                                pio[c].esc_buf[0] = 0x00;
                   8563:                                break;
1.1.1.38  root     8564:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     8565:                                pio[c].jis_mode = false;
                   8566:                                pio[c].esc_buf[0] = 0x00;
                   8567:                                break;
1.1.1.38  root     8568:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     8569:                                if(pio[c].esc_len == 76) {
                   8570:                                        pio[c].esc_buf[0] = 0x00;
                   8571:                                }
                   8572:                                break;
1.1.1.38  root     8573:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8574:                                if(pio[c].esc_len == 6) {
                   8575:                                        pio[c].esc_buf[0] = 0x00;
                   8576:                                }
                   8577:                                break;
1.1.1.38  root     8578:                        case 0x53: // 1Ch 53h XX XX
                   8579:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     8580:                                if(pio[c].esc_len == 4) {
                   8581:                                        pio[c].esc_buf[0] = 0x00;
                   8582:                                }
                   8583:                                break;
1.1.1.38  root     8584:                        default: // 1Ch XX
1.1.1.37  root     8585:                                pio[c].esc_buf[0] = 0x00;
                   8586:                                break;
                   8587:                        }
                   8588:                } else if(data == 0x1b || data == 0x1c) {
                   8589:                        printer_out(c, data);
                   8590:                        pio[c].esc_buf[0] = data;
                   8591:                        pio[c].esc_len = 1;
                   8592:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   8593:                        pio[c].sjis_hi = data;
                   8594:                } else {
                   8595:                        if(pio[c].jis_mode) {
                   8596:                                printer_out(c, 0x1c);
                   8597:                                printer_out(c, 0x2e);
                   8598:                                pio[c].jis_mode = false;
                   8599:                        }
                   8600:                        printer_out(c, data);
                   8601:                }
                   8602:        } else {
                   8603:                if(pio[c].jis_mode) {
                   8604:                        printer_out(c, 0x1c);
                   8605:                        printer_out(c, 0x2e);
                   8606:                        pio[c].jis_mode = false;
                   8607:                }
                   8608:                printer_out(c, data);
                   8609:        }
                   8610: }
                   8611: 
                   8612: inline void pcbios_int_17h_00h()
                   8613: {
                   8614:        if(REG16(DX) < 3) {
                   8615:                pcbios_printer_out(REG16(DX), REG8(AL));
                   8616:                REG8(AH) = 0xd0;
                   8617:        }
                   8618: }
                   8619: 
                   8620: inline void pcbios_int_17h_01h()
                   8621: {
                   8622:        if(REG16(DX) < 3) {
                   8623:                REG8(AH) = 0xd0;
                   8624:        }
                   8625: }
                   8626: 
                   8627: inline void pcbios_int_17h_02h()
                   8628: {
                   8629:        if(REG16(DX) < 3) {
                   8630:                REG8(AH) = 0xd0;
                   8631:        }
                   8632: }
                   8633: 
                   8634: inline void pcbios_int_17h_03h()
                   8635: {
                   8636:        switch(REG8(AL)) {
                   8637:        case 0x00:
                   8638:                if(REG16(DX) < 3) {
                   8639:                        if(pio[REG16(DX)].jis_mode) {
                   8640:                                printer_out(REG16(DX), 0x1c);
                   8641:                                printer_out(REG16(DX), 0x2e);
                   8642:                                pio[REG16(DX)].jis_mode = false;
                   8643:                        }
                   8644:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   8645:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   8646:                        }
                   8647:                        REG16(CX) = 0x0000;
                   8648:                        REG8(AH) = 0xd0;
                   8649:                }
                   8650:                break;
                   8651:        default:
                   8652:                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));
                   8653:                break;
                   8654:        }
                   8655: }
                   8656: 
                   8657: inline void pcbios_int_17h_50h()
                   8658: {
                   8659:        switch(REG8(AL)) {
                   8660:        case 0x00:
                   8661:                if(REG16(DX) < 3) {
                   8662:                        if(REG16(BX) = 0x0001) {
                   8663:                                pio[REG16(DX)].conv_mode = false;
                   8664:                                REG8(AL) = 0x00;
                   8665:                        } else if(REG16(BX) = 0x0051) {
                   8666:                                pio[REG16(DX)].conv_mode = true;
                   8667:                                REG8(AL) = 0x00;
                   8668:                        } else {
                   8669:                                REG8(AL) = 0x01;
                   8670:                        }
                   8671:                } else {
                   8672:                        REG8(AL) = 0x02;
                   8673:                }
                   8674:                break;
                   8675:        case 0x01:
                   8676:                if(REG16(DX) < 3) {
                   8677:                        if(pio[REG16(DX)].conv_mode) {
                   8678:                                REG16(BX) = 0x0051;
                   8679:                        } else {
                   8680:                                REG16(BX) = 0x0001;
                   8681:                        }
                   8682:                        REG8(AL) = 0x00;
                   8683:                } else {
                   8684:                        REG8(AL) = 0x02;
                   8685:                }
                   8686:                break;
                   8687:        default:
                   8688:                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));
                   8689:                break;
                   8690:        }
                   8691: }
                   8692: 
                   8693: inline void pcbios_int_17h_51h()
                   8694: {
                   8695:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   8696:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   8697:        } else {
                   8698:                REG16(DX) = 0x0000;
                   8699:        }
                   8700: }
                   8701: 
                   8702: inline void pcbios_int_17h_52h()
                   8703: {
                   8704:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   8705:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   8706:        } else {
                   8707:                REG16(DX) = 0x0000;
                   8708:        }
                   8709: }
                   8710: 
                   8711: inline void pcbios_int_17h_84h()
                   8712: {
                   8713:        if(REG16(DX) < 3) {
                   8714:                if(pio[REG16(DX)].jis_mode) {
                   8715:                        printer_out(REG16(DX), 0x1c);
                   8716:                        printer_out(REG16(DX), 0x2e);
                   8717:                        pio[REG16(DX)].jis_mode = false;
                   8718:                }
                   8719:                printer_out(REG16(DX), REG8(AL));
                   8720:                REG8(AH) = 0xd0;
                   8721:        }
                   8722: }
                   8723: 
                   8724: inline void pcbios_int_17h_85h()
                   8725: {
                   8726:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   8727: }
                   8728: 
1.1       root     8729: inline void pcbios_int_1ah_00h()
                   8730: {
1.1.1.19  root     8731:        pcbios_update_daily_timer_counter(timeGetTime());
                   8732:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   8733:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   8734:        REG8(AL) = mem[0x470];
                   8735:        mem[0x470] = 0;
1.1       root     8736: }
                   8737: 
                   8738: inline int to_bcd(int t)
                   8739: {
                   8740:        int u = (t % 100) / 10;
                   8741:        return (u << 4) | (t % 10);
                   8742: }
                   8743: 
                   8744: inline void pcbios_int_1ah_02h()
                   8745: {
                   8746:        SYSTEMTIME time;
                   8747:        
                   8748:        GetLocalTime(&time);
                   8749:        REG8(CH) = to_bcd(time.wHour);
                   8750:        REG8(CL) = to_bcd(time.wMinute);
                   8751:        REG8(DH) = to_bcd(time.wSecond);
                   8752:        REG8(DL) = 0x00;
                   8753: }
                   8754: 
                   8755: inline void pcbios_int_1ah_04h()
                   8756: {
                   8757:        SYSTEMTIME time;
                   8758:        
                   8759:        GetLocalTime(&time);
                   8760:        REG8(CH) = to_bcd(time.wYear / 100);
                   8761:        REG8(CL) = to_bcd(time.wYear);
                   8762:        REG8(DH) = to_bcd(time.wMonth);
                   8763:        REG8(DL) = to_bcd(time.wDay);
                   8764: }
                   8765: 
                   8766: inline void pcbios_int_1ah_0ah()
                   8767: {
                   8768:        SYSTEMTIME time;
                   8769:        FILETIME file_time;
                   8770:        WORD dos_date, dos_time;
                   8771:        
                   8772:        GetLocalTime(&time);
                   8773:        SystemTimeToFileTime(&time, &file_time);
                   8774:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   8775:        REG16(CX) = dos_date;
                   8776: }
                   8777: 
                   8778: // msdos system call
                   8779: 
1.1.1.43  root     8780: inline void msdos_int_21h_56h(int lfn);
                   8781: 
1.1       root     8782: inline void msdos_int_21h_00h()
                   8783: {
1.1.1.3   root     8784:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     8785: }
                   8786: 
1.1.1.35  root     8787: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     8788: {
                   8789:        REG8(AL) = msdos_getche();
1.1.1.33  root     8790:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8791:        
1.1.1.35  root     8792: #ifdef USE_SERVICE_THREAD
                   8793:        service_exit = true;
                   8794: #endif
                   8795:        return(0);
                   8796: }
                   8797: 
                   8798: inline void msdos_int_21h_01h()
                   8799: {
                   8800: #ifdef USE_SERVICE_THREAD
                   8801:        start_service_loop(msdos_int_21h_01h_thread);
                   8802: #else
                   8803:        msdos_int_21h_01h_thread(NULL);
                   8804:        REQUEST_HARDWRE_UPDATE();
                   8805: #endif
1.1       root     8806: }
                   8807: 
                   8808: inline void msdos_int_21h_02h()
                   8809: {
1.1.1.33  root     8810:        UINT8 data = REG8(DL);
                   8811:        msdos_putch(data);
                   8812:        REG8(AL) = data;
                   8813:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8814: }
                   8815: 
                   8816: inline void msdos_int_21h_03h()
                   8817: {
                   8818:        REG8(AL) = msdos_aux_in();
                   8819: }
                   8820: 
                   8821: inline void msdos_int_21h_04h()
                   8822: {
                   8823:        msdos_aux_out(REG8(DL));
                   8824: }
                   8825: 
                   8826: inline void msdos_int_21h_05h()
                   8827: {
                   8828:        msdos_prn_out(REG8(DL));
                   8829: }
                   8830: 
                   8831: inline void msdos_int_21h_06h()
                   8832: {
                   8833:        if(REG8(DL) == 0xff) {
                   8834:                if(msdos_kbhit()) {
                   8835:                        REG8(AL) = msdos_getch();
1.1.1.3   root     8836: #if defined(HAS_I386)
                   8837:                        m_ZF = 0;
                   8838: #else
                   8839:                        m_ZeroVal = 1;
                   8840: #endif
1.1       root     8841:                } else {
                   8842:                        REG8(AL) = 0;
1.1.1.3   root     8843: #if defined(HAS_I386)
                   8844:                        m_ZF = 1;
                   8845: #else
                   8846:                        m_ZeroVal = 0;
                   8847: #endif
1.1.1.14  root     8848:                        maybe_idle();
1.1       root     8849:                }
                   8850:        } else {
1.1.1.33  root     8851:                UINT8 data = REG8(DL);
                   8852:                msdos_putch(data);
                   8853:                REG8(AL) = data;
1.1       root     8854:        }
                   8855: }
                   8856: 
1.1.1.35  root     8857: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     8858: {
                   8859:        REG8(AL) = msdos_getch();
1.1.1.26  root     8860:        
1.1.1.35  root     8861: #ifdef USE_SERVICE_THREAD
                   8862:        service_exit = true;
                   8863: #endif
                   8864:        return(0);
1.1       root     8865: }
                   8866: 
1.1.1.35  root     8867: inline void msdos_int_21h_07h()
                   8868: {
                   8869: #ifdef USE_SERVICE_THREAD
                   8870:        start_service_loop(msdos_int_21h_07h_thread);
                   8871: #else
                   8872:        msdos_int_21h_07h_thread(NULL);
                   8873:        REQUEST_HARDWRE_UPDATE();
                   8874: #endif
                   8875: }
                   8876: 
                   8877: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     8878: {
                   8879:        REG8(AL) = msdos_getch();
1.1.1.33  root     8880:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8881:        
1.1.1.35  root     8882: #ifdef USE_SERVICE_THREAD
                   8883:        service_exit = true;
                   8884: #endif
                   8885:        return(0);
                   8886: }
                   8887: 
                   8888: inline void msdos_int_21h_08h()
                   8889: {
                   8890: #ifdef USE_SERVICE_THREAD
                   8891:        start_service_loop(msdos_int_21h_08h_thread);
                   8892: #else
                   8893:        msdos_int_21h_08h_thread(NULL);
                   8894:        REQUEST_HARDWRE_UPDATE();
                   8895: #endif
1.1       root     8896: }
                   8897: 
                   8898: inline void msdos_int_21h_09h()
                   8899: {
1.1.1.21  root     8900:        msdos_stdio_reopen();
                   8901:        
1.1.1.20  root     8902:        process_t *process = msdos_process_info_get(current_psp);
                   8903:        int fd = msdos_psp_get_file_table(1, current_psp);
                   8904:        
1.1.1.14  root     8905:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   8906:        int len = 0;
1.1       root     8907:        
1.1.1.14  root     8908:        while(str[len] != '$' && len < 0x10000) {
                   8909:                len++;
                   8910:        }
1.1.1.20  root     8911:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     8912:                // stdout is redirected to file
1.1.1.20  root     8913:                msdos_write(fd, str, len);
1.1       root     8914:        } else {
                   8915:                for(int i = 0; i < len; i++) {
1.1.1.14  root     8916:                        msdos_putch(str[i]);
1.1       root     8917:                }
                   8918:        }
1.1.1.33  root     8919:        REG8(AL) = '$';
                   8920:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8921: }
                   8922: 
1.1.1.35  root     8923: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     8924: {
1.1.1.3   root     8925:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     8926:        int max = mem[ofs] - 1;
                   8927:        UINT8 *buf = mem + ofs + 2;
                   8928:        int chr, p = 0;
                   8929:        
                   8930:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     8931:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     8932:                        p = 0;
1.1.1.33  root     8933:                        msdos_putch(0x03);
                   8934:                        msdos_putch(0x0d);
                   8935:                        msdos_putch(0x0a);
1.1.1.26  root     8936:                        break;
1.1.1.33  root     8937:                } else if(ctrl_break_pressed) {
                   8938:                        // skip this byte
1.1.1.26  root     8939:                } else if(chr == 0x00) {
1.1       root     8940:                        // skip 2nd byte
                   8941:                        msdos_getch();
                   8942:                } else if(chr == 0x08) {
                   8943:                        // back space
                   8944:                        if(p > 0) {
                   8945:                                p--;
1.1.1.20  root     8946:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     8947:                                        msdos_putch(0x08);
                   8948:                                        msdos_putch(0x08);
                   8949:                                        msdos_putch(0x20);
                   8950:                                        msdos_putch(0x20);
                   8951:                                        msdos_putch(0x08);
                   8952:                                        msdos_putch(0x08);
1.1.1.36  root     8953:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   8954:                                        p--;
                   8955:                                        msdos_putch(0x08);
                   8956:                                        msdos_putch(0x08);
                   8957:                                        msdos_putch(0x20);
                   8958:                                        msdos_putch(0x20);
                   8959:                                        msdos_putch(0x08);
                   8960:                                        msdos_putch(0x08);
1.1.1.34  root     8961:                                } else {
                   8962:                                        msdos_putch(0x08);
                   8963:                                        msdos_putch(0x20);
                   8964:                                        msdos_putch(0x08);
                   8965:                                }
                   8966:                        }
                   8967:                } else if(chr == 0x1b) {
                   8968:                        // escape
                   8969:                        while(p > 0) {
                   8970:                                p--;
                   8971:                                if(msdos_ctrl_code_check(buf[p])) {
                   8972:                                        msdos_putch(0x08);
                   8973:                                        msdos_putch(0x08);
                   8974:                                        msdos_putch(0x20);
                   8975:                                        msdos_putch(0x20);
                   8976:                                        msdos_putch(0x08);
                   8977:                                        msdos_putch(0x08);
1.1.1.20  root     8978:                                } else {
1.1.1.34  root     8979:                                        msdos_putch(0x08);
                   8980:                                        msdos_putch(0x20);
                   8981:                                        msdos_putch(0x08);
1.1.1.20  root     8982:                                }
1.1       root     8983:                        }
                   8984:                } else if(p < max) {
                   8985:                        buf[p++] = chr;
                   8986:                        msdos_putch(chr);
                   8987:                }
                   8988:        }
                   8989:        buf[p] = 0x0d;
                   8990:        mem[ofs + 1] = p;
1.1.1.33  root     8991:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8992:        
1.1.1.35  root     8993: #ifdef USE_SERVICE_THREAD
                   8994:        service_exit = true;
                   8995: #endif
                   8996:        return(0);
                   8997: }
                   8998: 
                   8999: inline void msdos_int_21h_0ah()
                   9000: {
                   9001:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   9002: #ifdef USE_SERVICE_THREAD
                   9003:                start_service_loop(msdos_int_21h_0ah_thread);
                   9004: #else
                   9005:                msdos_int_21h_0ah_thread(NULL);
                   9006:                REQUEST_HARDWRE_UPDATE();
                   9007: #endif
                   9008:        }
1.1       root     9009: }
                   9010: 
                   9011: inline void msdos_int_21h_0bh()
                   9012: {
                   9013:        if(msdos_kbhit()) {
                   9014:                REG8(AL) = 0xff;
                   9015:        } else {
                   9016:                REG8(AL) = 0x00;
1.1.1.14  root     9017:                maybe_idle();
1.1       root     9018:        }
1.1.1.33  root     9019:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9020: }
                   9021: 
                   9022: inline void msdos_int_21h_0ch()
                   9023: {
                   9024:        // clear key buffer
1.1.1.21  root     9025:        msdos_stdio_reopen();
                   9026:        
1.1.1.20  root     9027:        process_t *process = msdos_process_info_get(current_psp);
                   9028:        int fd = msdos_psp_get_file_table(0, current_psp);
                   9029:        
                   9030:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9031:                // stdin is redirected to file
                   9032:        } else {
                   9033:                while(msdos_kbhit()) {
                   9034:                        msdos_getch();
                   9035:                }
                   9036:        }
                   9037:        
                   9038:        switch(REG8(AL)) {
                   9039:        case 0x01:
                   9040:                msdos_int_21h_01h();
                   9041:                break;
                   9042:        case 0x06:
                   9043:                msdos_int_21h_06h();
                   9044:                break;
                   9045:        case 0x07:
                   9046:                msdos_int_21h_07h();
                   9047:                break;
                   9048:        case 0x08:
                   9049:                msdos_int_21h_08h();
                   9050:                break;
                   9051:        case 0x0a:
                   9052:                msdos_int_21h_0ah();
                   9053:                break;
                   9054:        default:
1.1.1.22  root     9055: //             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));
                   9056: //             REG16(AX) = 0x01;
                   9057: //             m_CF = 1;
1.1       root     9058:                break;
                   9059:        }
                   9060: }
                   9061: 
                   9062: inline void msdos_int_21h_0dh()
                   9063: {
                   9064: }
                   9065: 
                   9066: inline void msdos_int_21h_0eh()
                   9067: {
                   9068:        if(REG8(DL) < 26) {
                   9069:                _chdrive(REG8(DL) + 1);
                   9070:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9071:                msdos_sda_update(current_psp);
1.1       root     9072:        }
                   9073:        REG8(AL) = 26; // zdrive
                   9074: }
                   9075: 
1.1.1.14  root     9076: inline void msdos_int_21h_0fh()
                   9077: {
                   9078:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9079:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9080:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9081:        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     9082:        
1.1.1.14  root     9083:        if(hFile == INVALID_HANDLE_VALUE) {
                   9084:                REG8(AL) = 0xff;
                   9085:        } else {
                   9086:                REG8(AL) = 0;
                   9087:                fcb->current_block = 0;
                   9088:                fcb->record_size = 128;
                   9089:                fcb->file_size = GetFileSize(hFile, NULL);
                   9090:                fcb->handle = hFile;
                   9091:                fcb->cur_record = 0;
                   9092:        }
                   9093: }
                   9094: 
                   9095: inline void msdos_int_21h_10h()
                   9096: {
                   9097:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9098:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9099:        
                   9100:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9101: }
                   9102: 
1.1       root     9103: inline void msdos_int_21h_11h()
                   9104: {
1.1.1.3   root     9105:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9106:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9107:        
                   9108:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9109:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9110:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9111:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45  root     9112:        const char *path = msdos_fcb_path(fcb);
1.1       root     9113:        WIN32_FIND_DATA fd;
                   9114:        
1.1.1.13  root     9115:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9116:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9117:                FindClose(dtainfo->find_handle);
                   9118:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9119:        }
                   9120:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9121:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9122:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9123:        
1.1.1.14  root     9124:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9125:                dtainfo->allowable_mask &= ~8;
1.1       root     9126:        }
1.1.1.14  root     9127:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   9128:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9129:                      !msdos_find_file_has_8dot3name(&fd)) {
                   9130:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9131:                                FindClose(dtainfo->find_handle);
                   9132:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9133:                                break;
                   9134:                        }
                   9135:                }
                   9136:        }
1.1.1.13  root     9137:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9138:                if(ext_fcb->flag == 0xff) {
                   9139:                        ext_find->flag = 0xff;
                   9140:                        memset(ext_find->reserved, 0, 5);
                   9141:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9142:                }
                   9143:                find->drive = _getdrive();
1.1.1.13  root     9144:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9145:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9146:                find->nt_res = 0;
                   9147:                msdos_find_file_conv_local_time(&fd);
                   9148:                find->create_time_ms = 0;
                   9149:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9150:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9151:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9152:                find->cluster_hi = find->cluster_lo = 0;
                   9153:                find->file_size = fd.nFileSizeLow;
                   9154:                REG8(AL) = 0x00;
1.1.1.14  root     9155:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9156:                if(ext_fcb->flag == 0xff) {
                   9157:                        ext_find->flag = 0xff;
                   9158:                        memset(ext_find->reserved, 0, 5);
                   9159:                        ext_find->attribute = 8;
                   9160:                }
                   9161:                find->drive = _getdrive();
                   9162:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9163:                find->attribute = 8;
                   9164:                find->nt_res = 0;
                   9165:                msdos_find_file_conv_local_time(&fd);
                   9166:                find->create_time_ms = 0;
                   9167:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9168:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9169:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9170:                find->cluster_hi = find->cluster_lo = 0;
                   9171:                find->file_size = 0;
1.1.1.14  root     9172:                dtainfo->allowable_mask &= ~8;
1.1       root     9173:                REG8(AL) = 0x00;
                   9174:        } else {
                   9175:                REG8(AL) = 0xff;
                   9176:        }
                   9177: }
                   9178: 
                   9179: inline void msdos_int_21h_12h()
                   9180: {
1.1.1.3   root     9181:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     9182: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9183:        
                   9184:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9185:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9186:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9187:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9188:        WIN32_FIND_DATA fd;
                   9189:        
1.1.1.13  root     9190:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9191:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9192:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     9193:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9194:                              !msdos_find_file_has_8dot3name(&fd)) {
                   9195:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9196:                                        FindClose(dtainfo->find_handle);
                   9197:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9198:                                        break;
                   9199:                                }
                   9200:                        }
                   9201:                } else {
1.1.1.13  root     9202:                        FindClose(dtainfo->find_handle);
                   9203:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9204:                }
                   9205:        }
1.1.1.13  root     9206:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9207:                if(ext_fcb->flag == 0xff) {
                   9208:                        ext_find->flag = 0xff;
                   9209:                        memset(ext_find->reserved, 0, 5);
                   9210:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9211:                }
                   9212:                find->drive = _getdrive();
1.1.1.13  root     9213:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9214:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9215:                find->nt_res = 0;
                   9216:                msdos_find_file_conv_local_time(&fd);
                   9217:                find->create_time_ms = 0;
                   9218:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9219:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9220:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9221:                find->cluster_hi = find->cluster_lo = 0;
                   9222:                find->file_size = fd.nFileSizeLow;
                   9223:                REG8(AL) = 0x00;
1.1.1.14  root     9224:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9225:                if(ext_fcb->flag == 0xff) {
                   9226:                        ext_find->flag = 0xff;
                   9227:                        memset(ext_find->reserved, 0, 5);
                   9228:                        ext_find->attribute = 8;
                   9229:                }
                   9230:                find->drive = _getdrive();
                   9231:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9232:                find->attribute = 8;
                   9233:                find->nt_res = 0;
                   9234:                msdos_find_file_conv_local_time(&fd);
                   9235:                find->create_time_ms = 0;
                   9236:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9237:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9238:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9239:                find->cluster_hi = find->cluster_lo = 0;
                   9240:                find->file_size = 0;
1.1.1.14  root     9241:                dtainfo->allowable_mask &= ~8;
1.1       root     9242:                REG8(AL) = 0x00;
                   9243:        } else {
                   9244:                REG8(AL) = 0xff;
                   9245:        }
                   9246: }
                   9247: 
                   9248: inline void msdos_int_21h_13h()
                   9249: {
1.1.1.3   root     9250:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     9251:                REG8(AL) = 0xff;
                   9252:        } else {
                   9253:                REG8(AL) = 0x00;
                   9254:        }
                   9255: }
                   9256: 
1.1.1.16  root     9257: inline void msdos_int_21h_14h()
                   9258: {
                   9259:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9260:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9261:        process_t *process = msdos_process_info_get(current_psp);
                   9262:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9263:        DWORD num = 0;
                   9264:        
                   9265:        memset(mem + dta_laddr, 0, fcb->record_size);
                   9266:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9267:                REG8(AL) = 1;
                   9268:        } else {
                   9269:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9270:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9271:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9272:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9273:        }
                   9274: }
                   9275: 
                   9276: inline void msdos_int_21h_15h()
1.1.1.14  root     9277: {
                   9278:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9279:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     9280:        process_t *process = msdos_process_info_get(current_psp);
                   9281:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9282:        DWORD num = 0;
1.1.1.14  root     9283:        
1.1.1.16  root     9284:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9285:                REG8(AL) = 1;
                   9286:        } else {
                   9287:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9288:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9289:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9290:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9291:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9292:        }
                   9293: }
                   9294: 
                   9295: inline void msdos_int_21h_16h()
                   9296: {
                   9297:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9298:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9299:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9300:        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     9301:        
1.1.1.14  root     9302:        if(hFile == INVALID_HANDLE_VALUE) {
                   9303:                REG8(AL) = 0xff;
                   9304:        } else {
                   9305:                REG8(AL) = 0;
                   9306:                fcb->current_block = 0;
                   9307:                fcb->record_size = 128;
                   9308:                fcb->file_size = 0;
                   9309:                fcb->handle = hFile;
                   9310:                fcb->cur_record = 0;
                   9311:        }
                   9312: }
                   9313: 
1.1.1.16  root     9314: inline void msdos_int_21h_17h()
                   9315: {
                   9316:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9317:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45  root     9318: //     const char *path_src = msdos_fcb_path(fcb_src);
                   9319:        char path_src[MAX_PATH];
                   9320:        strcpy(path_src, msdos_fcb_path(fcb_src));
                   9321:        
1.1.1.16  root     9322:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   9323:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45  root     9324: //     const char *path_dst = msdos_fcb_path(fcb_dst);
                   9325:        char path_dst[MAX_PATH];
                   9326:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     9327:        
                   9328:        if(rename(path_src, path_dst)) {
                   9329:                REG8(AL) = 0xff;
                   9330:        } else {
                   9331:                REG8(AL) = 0;
                   9332:        }
                   9333: }
                   9334: 
1.1       root     9335: inline void msdos_int_21h_18h()
                   9336: {
                   9337:        REG8(AL) = 0x00;
                   9338: }
                   9339: 
                   9340: inline void msdos_int_21h_19h()
                   9341: {
                   9342:        REG8(AL) = _getdrive() - 1;
                   9343: }
                   9344: 
                   9345: inline void msdos_int_21h_1ah()
                   9346: {
                   9347:        process_t *process = msdos_process_info_get(current_psp);
                   9348:        
                   9349:        process->dta.w.l = REG16(DX);
1.1.1.3   root     9350:        process->dta.w.h = SREG(DS);
1.1.1.23  root     9351:        msdos_sda_update(current_psp);
1.1       root     9352: }
                   9353: 
                   9354: inline void msdos_int_21h_1bh()
                   9355: {
                   9356:        int drive_num = _getdrive() - 1;
                   9357:        UINT16 seg, ofs;
                   9358:        
                   9359:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9360:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9361:                REG8(AL) = dpb->highest_sector_num + 1;
                   9362:                REG16(CX) = dpb->bytes_per_sector;
                   9363:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9364:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9365:        } else {
                   9366:                REG8(AL) = 0xff;
1.1.1.3   root     9367:                m_CF = 1;
1.1       root     9368:        }
                   9369: 
                   9370: }
                   9371: 
                   9372: inline void msdos_int_21h_1ch()
                   9373: {
1.1.1.41  root     9374:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     9375:        UINT16 seg, ofs;
                   9376:        
                   9377:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9378:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9379:                REG8(AL) = dpb->highest_sector_num + 1;
                   9380:                REG16(CX) = dpb->bytes_per_sector;
                   9381:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9382:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9383:        } else {
                   9384:                REG8(AL) = 0xff;
1.1.1.3   root     9385:                m_CF = 1;
1.1       root     9386:        }
                   9387: 
                   9388: }
                   9389: 
                   9390: inline void msdos_int_21h_1dh()
                   9391: {
                   9392:        REG8(AL) = 0;
                   9393: }
                   9394: 
                   9395: inline void msdos_int_21h_1eh()
                   9396: {
                   9397:        REG8(AL) = 0;
                   9398: }
                   9399: 
                   9400: inline void msdos_int_21h_1fh()
                   9401: {
                   9402:        int drive_num = _getdrive() - 1;
                   9403:        UINT16 seg, ofs;
                   9404:        
                   9405:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9406:                REG8(AL) = 0;
1.1.1.3   root     9407:                SREG(DS) = seg;
                   9408:                i386_load_segment_descriptor(DS);
1.1       root     9409:                REG16(BX) = ofs;
                   9410:        } else {
                   9411:                REG8(AL) = 0xff;
1.1.1.3   root     9412:                m_CF = 1;
1.1       root     9413:        }
                   9414: }
                   9415: 
                   9416: inline void msdos_int_21h_20h()
                   9417: {
                   9418:        REG8(AL) = 0;
                   9419: }
                   9420: 
1.1.1.14  root     9421: inline void msdos_int_21h_21h()
                   9422: {
                   9423:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9424:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9425:        
                   9426:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9427:                REG8(AL) = 1;
                   9428:        } else {
                   9429:                process_t *process = msdos_process_info_get(current_psp);
                   9430:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9431:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     9432:                DWORD num = 0;
1.1.1.14  root     9433:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9434:                        REG8(AL) = 1;
                   9435:                } else {
                   9436:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9437:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9438:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     9439:                }
                   9440:        }
                   9441: }
                   9442: 
                   9443: inline void msdos_int_21h_22h()
                   9444: {
                   9445:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9446:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9447:        
                   9448:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9449:                REG8(AL) = 0xff;
                   9450:        } else {
                   9451:                process_t *process = msdos_process_info_get(current_psp);
                   9452:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     9453:                DWORD num = 0;
1.1.1.14  root     9454:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   9455:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9456:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9457:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9458:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     9459:        }
                   9460: }
                   9461: 
1.1.1.16  root     9462: inline void msdos_int_21h_23h()
                   9463: {
                   9464:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9465:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9466:        const char *path = msdos_fcb_path(fcb);
1.1.1.16  root     9467:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9468:        
                   9469:        if(hFile == INVALID_HANDLE_VALUE) {
                   9470:                REG8(AL) = 0xff;
                   9471:        } else {
                   9472:                UINT32 size = GetFileSize(hFile, NULL);
                   9473:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   9474:                REG8(AL) = 0;
                   9475:        }
                   9476: }
                   9477: 
                   9478: inline void msdos_int_21h_24h()
                   9479: {
                   9480:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9481:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9482:        
                   9483:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   9484: }
                   9485: 
1.1       root     9486: inline void msdos_int_21h_25h()
                   9487: {
                   9488:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     9489:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     9490: }
                   9491: 
                   9492: inline void msdos_int_21h_26h()
                   9493: {
                   9494:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   9495:        
                   9496:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   9497:        psp->first_mcb = REG16(DX) + 16;
                   9498:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   9499:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   9500:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   9501:        psp->parent_psp = 0;
                   9502: }
                   9503: 
1.1.1.16  root     9504: inline void msdos_int_21h_27h()
                   9505: {
                   9506:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9507:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9508:        
                   9509:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9510:                REG8(AL) = 1;
                   9511:        } else {
                   9512:                process_t *process = msdos_process_info_get(current_psp);
                   9513:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9514:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   9515:                DWORD num = 0;
                   9516:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   9517:                        REG8(AL) = 1;
                   9518:                } else {
                   9519:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9520:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9521:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9522:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9523:                }
                   9524:        }
                   9525: }
                   9526: 
                   9527: inline void msdos_int_21h_28h()
                   9528: {
                   9529:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9530:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9531:        
                   9532:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9533:                REG8(AL) = 0xff;
                   9534:        } else {
                   9535:                process_t *process = msdos_process_info_get(current_psp);
                   9536:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9537:                DWORD num = 0;
                   9538:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   9539:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9540:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9541:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9542:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9543:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9544:        }
                   9545: }
                   9546: 
1.1       root     9547: inline void msdos_int_21h_29h()
                   9548: {
1.1.1.20  root     9549:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   9550:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     9551:        UINT8 drv = 0;
                   9552:        char sep_chars[] = ":.;,=+";
                   9553:        char end_chars[] = "\\<>|/\"[]";
                   9554:        char spc_chars[] = " \t";
                   9555:        
1.1.1.20  root     9556:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   9557:        buffer[1023] = 0;
                   9558:        memset(name, 0x20, sizeof(name));
                   9559:        memset(ext, 0x20, sizeof(ext));
                   9560:        
1.1       root     9561:        if(REG8(AL) & 1) {
1.1.1.20  root     9562:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   9563:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     9564:                        ofs++;
                   9565:                }
                   9566:        }
1.1.1.20  root     9567:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     9568:        
1.1.1.24  root     9569:        if(buffer[ofs + 1] == ':') {
                   9570:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   9571:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     9572:                        ofs += 2;
1.1.1.24  root     9573:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9574:                                ofs++;
                   9575:                        }
                   9576:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   9577:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     9578:                        ofs += 2;
1.1.1.24  root     9579:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9580:                                ofs++;
                   9581:                        }
1.1       root     9582:                }
                   9583:        }
1.1.1.20  root     9584:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9585:                UINT8 c = buffer[ofs];
                   9586:                if(is_kanji) {
                   9587:                        is_kanji = 0;
                   9588:                } else if(msdos_lead_byte_check(c)) {
                   9589:                        is_kanji = 1;
                   9590:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9591:                        break;
                   9592:                } else if(c >= 'a' && c <= 'z') {
                   9593:                        c -= 0x20;
                   9594:                }
                   9595:                ofs++;
                   9596:                name[i] = c;
                   9597:        }
1.1.1.20  root     9598:        if(buffer[ofs] == '.') {
1.1       root     9599:                ofs++;
1.1.1.20  root     9600:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9601:                        UINT8 c = buffer[ofs];
                   9602:                        if(is_kanji) {
                   9603:                                is_kanji = 0;
                   9604:                        } else if(msdos_lead_byte_check(c)) {
                   9605:                                is_kanji = 1;
                   9606:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9607:                                break;
                   9608:                        } else if(c >= 'a' && c <= 'z') {
                   9609:                                c -= 0x20;
                   9610:                        }
                   9611:                        ofs++;
                   9612:                        ext[i] = c;
                   9613:                }
                   9614:        }
1.1.1.20  root     9615:        int si = REG16(SI) + ofs;
1.1.1.3   root     9616:        int ds = SREG(DS);
1.1       root     9617:        while(si > 0xffff) {
                   9618:                si -= 0x10;
                   9619:                ds++;
                   9620:        }
                   9621:        REG16(SI) = si;
1.1.1.3   root     9622:        SREG(DS) = ds;
                   9623:        i386_load_segment_descriptor(DS);
1.1       root     9624:        
1.1.1.3   root     9625:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     9626:        if(!(REG8(AL) & 2) || drv != 0) {
                   9627:                fcb[0] = drv;
                   9628:        }
                   9629:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   9630:                memcpy(fcb + 1, name, 8);
                   9631:        }
                   9632:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   9633:                memcpy(fcb + 9, ext, 3);
                   9634:        }
                   9635:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     9636:                if(fcb[i] == '*') {
                   9637:                        found_star = 1;
                   9638:                }
                   9639:                if(found_star) {
                   9640:                        fcb[i] = '?';
                   9641:                }
                   9642:        }
1.1.1.20  root     9643:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     9644:                if(fcb[i] == '*') {
                   9645:                        found_star = 1;
                   9646:                }
                   9647:                if(found_star) {
                   9648:                        fcb[i] = '?';
                   9649:                }
                   9650:        }
                   9651:        
1.1.1.44  root     9652:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     9653:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   9654:                        REG8(AL) = 0x01;
1.1.1.20  root     9655:                } else {
                   9656:                        REG8(AL) = 0x00;
1.1       root     9657:                }
                   9658:        } else {
                   9659:                REG8(AL) = 0xff;
                   9660:        }
                   9661: }
                   9662: 
                   9663: inline void msdos_int_21h_2ah()
                   9664: {
                   9665:        SYSTEMTIME sTime;
                   9666:        
                   9667:        GetLocalTime(&sTime);
                   9668:        REG16(CX) = sTime.wYear;
                   9669:        REG8(DH) = (UINT8)sTime.wMonth;
                   9670:        REG8(DL) = (UINT8)sTime.wDay;
                   9671:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   9672: }
                   9673: 
                   9674: inline void msdos_int_21h_2bh()
                   9675: {
1.1.1.14  root     9676:        REG8(AL) = 0xff;
1.1       root     9677: }
                   9678: 
                   9679: inline void msdos_int_21h_2ch()
                   9680: {
                   9681:        SYSTEMTIME sTime;
                   9682:        
                   9683:        GetLocalTime(&sTime);
                   9684:        REG8(CH) = (UINT8)sTime.wHour;
                   9685:        REG8(CL) = (UINT8)sTime.wMinute;
                   9686:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     9687:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     9688: }
                   9689: 
                   9690: inline void msdos_int_21h_2dh()
                   9691: {
                   9692:        REG8(AL) = 0x00;
                   9693: }
                   9694: 
                   9695: inline void msdos_int_21h_2eh()
                   9696: {
                   9697:        process_t *process = msdos_process_info_get(current_psp);
                   9698:        
                   9699:        process->verify = REG8(AL);
                   9700: }
                   9701: 
                   9702: inline void msdos_int_21h_2fh()
                   9703: {
                   9704:        process_t *process = msdos_process_info_get(current_psp);
                   9705:        
                   9706:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     9707:        SREG(ES) = process->dta.w.h;
                   9708:        i386_load_segment_descriptor(ES);
1.1       root     9709: }
                   9710: 
                   9711: inline void msdos_int_21h_30h()
                   9712: {
                   9713:        // Version Flag / OEM
1.1.1.27  root     9714:        if(REG8(AL) == 0x01) {
1.1.1.29  root     9715: #ifdef SUPPORT_HMA
                   9716:                REG16(BX) = 0x0000;
                   9717: #else
                   9718:                REG16(BX) = 0x1000; // DOS is in HMA
                   9719: #endif
1.1       root     9720:        } else {
1.1.1.27  root     9721:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   9722:                // but this is not correct on Windows 98 SE
                   9723: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   9724:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     9725:        }
1.1.1.27  root     9726:        REG16(CX) = 0x0000;
1.1.1.30  root     9727:        REG8(AL) = dos_major_version;   // 7
                   9728:        REG8(AH) = dos_minor_version;   // 10
1.1       root     9729: }
                   9730: 
                   9731: inline void msdos_int_21h_31h()
                   9732: {
1.1.1.29  root     9733:        try {
                   9734:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9735:        } catch(...) {
                   9736:                // recover the broken mcb
                   9737:                int mcb_seg = current_psp - 1;
                   9738:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     9739:                
1.1.1.29  root     9740:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     9741:                        mcb->mz = 'M';
                   9742:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   9743:                        
1.1.1.29  root     9744:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     9745:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     9746:                        } else {
1.1.1.39  root     9747:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     9748:                        }
                   9749:                } else {
                   9750:                        mcb->mz = 'Z';
1.1.1.30  root     9751:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     9752:                }
                   9753:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9754:        }
1.1       root     9755:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   9756: }
                   9757: 
                   9758: inline void msdos_int_21h_32h()
                   9759: {
                   9760:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   9761:        UINT16 seg, ofs;
                   9762:        
                   9763:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9764:                REG8(AL) = 0;
1.1.1.3   root     9765:                SREG(DS) = seg;
                   9766:                i386_load_segment_descriptor(DS);
1.1       root     9767:                REG16(BX) = ofs;
                   9768:        } else {
                   9769:                REG8(AL) = 0xff;
1.1.1.3   root     9770:                m_CF = 1;
1.1       root     9771:        }
                   9772: }
                   9773: 
                   9774: inline void msdos_int_21h_33h()
                   9775: {
                   9776:        char path[MAX_PATH];
                   9777:        
                   9778:        switch(REG8(AL)) {
                   9779:        case 0x00:
1.1.1.33  root     9780:                REG8(DL) = ctrl_break_checking;
1.1       root     9781:                break;
                   9782:        case 0x01:
1.1.1.33  root     9783:                ctrl_break_checking = REG8(DL);
                   9784:                break;
                   9785:        case 0x02:
                   9786:                {
                   9787:                        UINT8 old = ctrl_break_checking;
                   9788:                        ctrl_break_checking = REG8(DL);
                   9789:                        REG8(DL) = old;
                   9790:                }
                   9791:                break;
                   9792:        case 0x03:
                   9793:        case 0x04:
                   9794:                // DOS 4.0+ - Unused
1.1       root     9795:                break;
                   9796:        case 0x05:
                   9797:                GetSystemDirectory(path, MAX_PATH);
                   9798:                if(path[0] >= 'a' && path[0] <= 'z') {
                   9799:                        REG8(DL) = path[0] - 'a' + 1;
                   9800:                } else {
                   9801:                        REG8(DL) = path[0] - 'A' + 1;
                   9802:                }
                   9803:                break;
                   9804:        case 0x06:
1.1.1.2   root     9805:                // MS-DOS version (7.10)
1.1       root     9806:                REG8(BL) = 7;
1.1.1.2   root     9807:                REG8(BH) = 10;
1.1       root     9808:                REG8(DL) = 0;
1.1.1.29  root     9809: #ifdef SUPPORT_HMA
                   9810:                REG8(DH) = 0x00;
                   9811: #else
                   9812:                REG8(DH) = 0x10; // DOS is in HMA
                   9813: #endif
1.1       root     9814:                break;
1.1.1.6   root     9815:        case 0x07:
                   9816:                if(REG8(DL) == 0) {
                   9817:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   9818:                } else if(REG8(DL) == 1) {
                   9819:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   9820:                }
                   9821:                break;
1.1       root     9822:        default:
1.1.1.22  root     9823:                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     9824:                REG16(AX) = 0x01;
1.1.1.3   root     9825:                m_CF = 1;
1.1       root     9826:                break;
                   9827:        }
                   9828: }
                   9829: 
1.1.1.23  root     9830: inline void msdos_int_21h_34h()
                   9831: {
                   9832:        SREG(ES) = SDA_TOP >> 4;
                   9833:        i386_load_segment_descriptor(ES);
                   9834:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   9835: }
                   9836: 
1.1       root     9837: inline void msdos_int_21h_35h()
                   9838: {
                   9839:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     9840:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   9841:        i386_load_segment_descriptor(ES);
1.1       root     9842: }
                   9843: 
                   9844: inline void msdos_int_21h_36h()
                   9845: {
                   9846:        struct _diskfree_t df = {0};
                   9847:        
                   9848:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   9849:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   9850:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     9851:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   9852:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     9853:        } else {
                   9854:                REG16(AX) = 0xffff;
                   9855:        }
                   9856: }
                   9857: 
                   9858: inline void msdos_int_21h_37h()
                   9859: {
1.1.1.22  root     9860:        static UINT8 dev_flag = 0xff;
1.1       root     9861:        
                   9862:        switch(REG8(AL)) {
                   9863:        case 0x00:
1.1.1.22  root     9864:                {
                   9865:                        process_t *process = msdos_process_info_get(current_psp);
                   9866:                        REG8(AL) = 0x00;
                   9867:                        REG8(DL) = process->switchar;
                   9868:                }
1.1       root     9869:                break;
                   9870:        case 0x01:
1.1.1.22  root     9871:                {
                   9872:                        process_t *process = msdos_process_info_get(current_psp);
                   9873:                        REG8(AL) = 0x00;
                   9874:                        process->switchar = REG8(DL);
1.1.1.23  root     9875:                        msdos_sda_update(current_psp);
1.1.1.22  root     9876:                }
                   9877:                break;
                   9878:        case 0x02:
                   9879:                REG8(DL) = dev_flag;
                   9880:                break;
                   9881:        case 0x03:
                   9882:                dev_flag = REG8(DL);
                   9883:                break;
                   9884:        case 0xd0:
                   9885:        case 0xd1:
                   9886:        case 0xd2:
                   9887:        case 0xd3:
                   9888:        case 0xd4:
                   9889:        case 0xd5:
                   9890:        case 0xd6:
                   9891:        case 0xd7:
                   9892:        case 0xdc:
                   9893:        case 0xdd:
                   9894:        case 0xde:
                   9895:        case 0xdf:
                   9896:                // diet ???
                   9897:                REG16(AX) = 1;
1.1       root     9898:                break;
                   9899:        default:
1.1.1.22  root     9900:                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     9901:                REG16(AX) = 1;
                   9902:                break;
                   9903:        }
                   9904: }
                   9905: 
1.1.1.42  root     9906: int get_country_info(country_info_t *ci, LCID locale)
1.1.1.17  root     9907: {
                   9908:        char LCdata[80];
                   9909:        
1.1.1.19  root     9910:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.42  root     9911:        GetLocaleInfo(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     9912:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.42  root     9913:        GetLocaleInfo(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     9914:        ci->currency_format = *LCdata - '0';
1.1.1.42  root     9915:        GetLocaleInfo(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     9916:        ci->date_format = *LCdata - '0';
1.1.1.42  root     9917:        GetLocaleInfo(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     9918:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.42  root     9919:        GetLocaleInfo(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     9920:        *ci->date_sep = *LCdata;
1.1.1.42  root     9921:        GetLocaleInfo(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     9922:        *ci->dec_sep = *LCdata;
1.1.1.42  root     9923:        GetLocaleInfo(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     9924:        *ci->list_sep = *LCdata;
1.1.1.42  root     9925:        GetLocaleInfo(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     9926:        *ci->thou_sep = *LCdata;
1.1.1.42  root     9927:        GetLocaleInfo(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     9928:        *ci->time_sep = *LCdata;
1.1.1.42  root     9929:        GetLocaleInfo(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     9930:        if(strchr(LCdata, 'H') != NULL) {
                   9931:                ci->time_format = 1;
                   9932:        }
1.1.1.27  root     9933:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffd:000a
1.1.1.24  root     9934:        ci->case_map.w.h = 0xfffd;
1.1.1.42  root     9935:        GetLocaleInfo(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     9936:        return atoi(LCdata);
                   9937: }
                   9938: 
1.1.1.42  root     9939: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   9940: {
                   9941:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   9942: }
                   9943: 
                   9944: int get_country_info(country_info_t *ci)
                   9945: {
                   9946:        return get_country_info(ci, LOCALE_USER_DEFAULT);
                   9947: }
                   9948: 
1.1.1.43  root     9949: void set_country_info(country_info_t *ci, int size)
                   9950: {
                   9951:        char LCdata[80];
                   9952:        
                   9953:        if(size >= 0x00 + 2) {
                   9954:                memset(LCdata, 0, sizeof(LCdata));
                   9955:                *LCdata = '0' + ci->date_format;
                   9956:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
                   9957:        }
                   9958:        if(size >= 0x02 + 5) {
                   9959:                memset(LCdata, 0, sizeof(LCdata));
                   9960:                memcpy(LCdata, &ci->currency_symbol, 4);
                   9961:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
                   9962:        }
                   9963:        if(size >= 0x07 + 2) {
                   9964:                memset(LCdata, 0, sizeof(LCdata));
                   9965:                *LCdata = *ci->thou_sep;
                   9966:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
                   9967:        }
                   9968:        if(size >= 0x09 + 2) {
                   9969:                memset(LCdata, 0, sizeof(LCdata));
                   9970:                *LCdata = *ci->dec_sep;
                   9971:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
                   9972:        }
                   9973:        if(size >= 0x0b + 2) {
                   9974:                memset(LCdata, 0, sizeof(LCdata));
                   9975:                *LCdata = *ci->date_sep;
                   9976:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
                   9977:        }
                   9978:        if(size >= 0x0d + 2) {
                   9979:                memset(LCdata, 0, sizeof(LCdata));
                   9980:                *LCdata = *ci->time_sep;
                   9981:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
                   9982:        }
                   9983:        if(size >= 0x0f + 1) {
                   9984:                memset(LCdata, 0, sizeof(LCdata));
                   9985:                *LCdata = '0' + ci->currency_format;
                   9986:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
                   9987:        }
                   9988:        if(size >= 0x10 + 1) {
                   9989:                sprintf(LCdata, "%d", ci->currency_dec_digits);
                   9990:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
                   9991:        }
                   9992:        if(size >= 0x11 + 1) {
                   9993:                // FIXME: is time format always H/h:mm:ss ???
                   9994:                if(ci->time_format & 1) {
                   9995:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   9996:                } else {
                   9997:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   9998:                }
                   9999:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
                   10000:        }
                   10001:        if(size >= 0x12 + 4) {
                   10002:                // 12h  DWORD   address of case map routine
                   10003:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   10004:        }
                   10005:        if(size >= 0x16 + 2) {
                   10006:                memset(LCdata, 0, sizeof(LCdata));
                   10007:                *LCdata = *ci->list_sep;
                   10008:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
                   10009:        }
                   10010: }
                   10011: 
1.1.1.42  root     10012: #ifndef SUBLANG_SWAHILI
                   10013:        #define SUBLANG_SWAHILI 0x01
                   10014: #endif
                   10015: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10016:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10017: #endif
                   10018: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10019:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10020: #endif
                   10021: #ifndef LANG_BANGLA
                   10022:        #define LANG_BANGLA 0x45
                   10023: #endif
                   10024: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10025:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10026: #endif
                   10027: 
                   10028: static const struct {
                   10029:        int code;
                   10030:        USHORT usPrimaryLanguage;
                   10031:        USHORT usSubLanguage;
                   10032: } country_table[] = {
                   10033:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10034:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10035:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10036:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10037:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10038:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10039: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10040: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10041: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10042: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10043: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10044:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10045:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10046: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10047:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10048: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10049:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10050:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10051:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10052:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10053:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10054:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10055:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10056: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10057: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10058:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10059:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10060:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10061:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10062:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10063: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10064: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10065: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10066:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10067: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10068: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10069: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10070: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10071:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10072:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10073:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10074: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10075:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10076:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10077:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10078:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10079:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10080:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10081:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10082:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10083:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10084:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10085:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10086:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10087:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10088: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10089:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10090:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10091:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10092:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10093:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10094:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10095:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10096:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10097:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10098:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10099: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10100:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10101: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10102:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10103:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10104:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10105:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10106:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10107:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10108:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10109:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10110: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10111:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10112: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10113: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10114:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   10115: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   10116:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   10117:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   10118:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   10119:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   10120:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   10121:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   10122:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   10123: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   10124: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   10125:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   10126: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   10127:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   10128:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   10129:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   10130:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   10131: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   10132: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   10133: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   10134:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   10135:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   10136:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   10137:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   10138:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   10139:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   10140:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   10141:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   10142:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   10143:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   10144:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   10145:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   10146:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   10147:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   10148:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   10149:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   10150:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   10151:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   10152:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   10153:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   10154:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     10155: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     10156:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   10157: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   10158:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   10159:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   10160:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   10161:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   10162:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   10163:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   10164:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   10165:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   10166:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   10167:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   10168:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   10169:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   10170:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   10171:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   10172:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   10173:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   10174:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   10175:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   10176:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   10177:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   10178:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   10179:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   10180:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   10181:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   10182:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   10183: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   10184:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   10185:        {-1, 0, 0},
                   10186: };
                   10187: 
1.1.1.14  root     10188: inline void msdos_int_21h_38h()
                   10189: {
                   10190:        switch(REG8(AL)) {
                   10191:        case 0x00:
1.1.1.19  root     10192:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     10193:                break;
                   10194:        default:
1.1.1.42  root     10195:                for(int i = 0;; i++) {
                   10196:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   10197:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   10198:                                break;
                   10199:                        } else if(country_table[i].code == -1) {
                   10200: //                             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));
                   10201: //                             REG16(AX) = 2;
                   10202: //                             m_CF = 1;
                   10203:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   10204:                                break;
                   10205:                        }
                   10206:                }
1.1.1.14  root     10207:                break;
                   10208:        }
                   10209: }
                   10210: 
1.1       root     10211: inline void msdos_int_21h_39h(int lfn)
                   10212: {
1.1.1.3   root     10213:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10214:                REG16(AX) = errno;
1.1.1.3   root     10215:                m_CF = 1;
1.1       root     10216:        }
                   10217: }
                   10218: 
                   10219: inline void msdos_int_21h_3ah(int lfn)
                   10220: {
1.1.1.3   root     10221:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10222:                REG16(AX) = errno;
1.1.1.3   root     10223:                m_CF = 1;
1.1       root     10224:        }
                   10225: }
                   10226: 
                   10227: inline void msdos_int_21h_3bh(int lfn)
                   10228: {
1.1.1.45  root     10229:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     10230:        
                   10231:        if(_chdir(path)) {
1.1.1.17  root     10232:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     10233:                m_CF = 1;
1.1.1.44  root     10234:        } else {
                   10235:                int drv = _getdrive() - 1;
                   10236:                if(path[1] == ':') {
                   10237:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   10238:                                drv = path[0] - 'A';
                   10239:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   10240:                                drv = path[0] - 'a';
                   10241:                        }
                   10242:                }
                   10243:                msdos_cds_update(drv, path);
1.1       root     10244:        }
                   10245: }
                   10246: 
                   10247: inline void msdos_int_21h_3ch()
                   10248: {
1.1.1.45  root     10249:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10250:        int attr = GetFileAttributes(path);
1.1.1.37  root     10251:        int fd = -1;
                   10252:        int sio_port = 0;
                   10253:        int lpt_port = 0;
1.1       root     10254:        
1.1.1.45  root     10255:        if(msdos_is_device_path(path)) {
                   10256:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     10257:        } else {
                   10258:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10259:        }
                   10260:        if(fd != -1) {
                   10261:                if(attr == -1) {
                   10262:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   10263:                }
                   10264:                SetFileAttributes(path, attr);
                   10265:                REG16(AX) = fd;
1.1.1.45  root     10266:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10267:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10268:        } else {
                   10269:                REG16(AX) = errno;
1.1.1.3   root     10270:                m_CF = 1;
1.1       root     10271:        }
                   10272: }
                   10273: 
                   10274: inline void msdos_int_21h_3dh()
                   10275: {
1.1.1.45  root     10276:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10277:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     10278:        int fd = -1;
                   10279:        int sio_port = 0;
                   10280:        int lpt_port = 0;
1.1       root     10281:        
                   10282:        if(mode < 0x03) {
1.1.1.45  root     10283:                if(msdos_is_device_path(path)) {
                   10284:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     10285:                } else {
1.1.1.13  root     10286:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     10287:                }
1.1       root     10288:                if(fd != -1) {
                   10289:                        REG16(AX) = fd;
1.1.1.45  root     10290:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10291:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10292:                } else {
                   10293:                        REG16(AX) = errno;
1.1.1.3   root     10294:                        m_CF = 1;
1.1       root     10295:                }
                   10296:        } else {
                   10297:                REG16(AX) = 0x0c;
1.1.1.3   root     10298:                m_CF = 1;
1.1       root     10299:        }
                   10300: }
                   10301: 
                   10302: inline void msdos_int_21h_3eh()
                   10303: {
                   10304:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10305:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10306:        
1.1.1.20  root     10307:        if(fd < process->max_files && file_handler[fd].valid) {
                   10308:                _close(fd);
                   10309:                msdos_file_handler_close(fd);
                   10310:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     10311:        } else {
                   10312:                REG16(AX) = 0x06;
1.1.1.3   root     10313:                m_CF = 1;
1.1       root     10314:        }
                   10315: }
                   10316: 
1.1.1.35  root     10317: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   10318: {
                   10319:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   10320:        int max = REG16(CX);
                   10321:        int p = 0;
                   10322:        
                   10323:        while(max > p) {
                   10324:                int chr = msdos_getch();
                   10325:                
                   10326:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   10327:                        p = 0;
                   10328:                        buf[p++] = 0x0d;
                   10329:                        if(max > p) {
                   10330:                                buf[p++] = 0x0a;
                   10331:                        }
                   10332:                        msdos_putch(0x03);
                   10333:                        msdos_putch(0x0d);
                   10334:                        msdos_putch(0x0a);
                   10335:                        break;
                   10336:                } else if(ctrl_break_pressed) {
                   10337:                        // skip this byte
                   10338:                } else if(chr == 0x00) {
                   10339:                        // skip 2nd byte
                   10340:                        msdos_getch();
                   10341:                } else if(chr == 0x0d) {
                   10342:                        // carriage return
                   10343:                        buf[p++] = 0x0d;
                   10344:                        if(max > p) {
                   10345:                                buf[p++] = 0x0a;
                   10346:                        }
                   10347:                        msdos_putch('\n');
                   10348:                        break;
                   10349:                } else if(chr == 0x08) {
                   10350:                        // back space
                   10351:                        if(p > 0) {
                   10352:                                p--;
                   10353:                                if(msdos_ctrl_code_check(buf[p])) {
                   10354:                                        msdos_putch(0x08);
                   10355:                                        msdos_putch(0x08);
                   10356:                                        msdos_putch(0x20);
                   10357:                                        msdos_putch(0x20);
                   10358:                                        msdos_putch(0x08);
                   10359:                                        msdos_putch(0x08);
1.1.1.36  root     10360:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   10361:                                        p--;
                   10362:                                        msdos_putch(0x08);
                   10363:                                        msdos_putch(0x08);
                   10364:                                        msdos_putch(0x20);
                   10365:                                        msdos_putch(0x20);
                   10366:                                        msdos_putch(0x08);
                   10367:                                        msdos_putch(0x08);
1.1.1.35  root     10368:                                } else {
                   10369:                                        msdos_putch(0x08);
                   10370:                                        msdos_putch(0x20);
                   10371:                                        msdos_putch(0x08);
                   10372:                                }
                   10373:                        }
                   10374:                } else if(chr == 0x1b) {
                   10375:                        // escape
                   10376:                        while(p > 0) {
                   10377:                                p--;
                   10378:                                if(msdos_ctrl_code_check(buf[p])) {
                   10379:                                        msdos_putch(0x08);
                   10380:                                        msdos_putch(0x08);
                   10381:                                        msdos_putch(0x20);
                   10382:                                        msdos_putch(0x20);
                   10383:                                        msdos_putch(0x08);
                   10384:                                        msdos_putch(0x08);
                   10385:                                } else {
                   10386:                                        msdos_putch(0x08);
                   10387:                                        msdos_putch(0x20);
                   10388:                                        msdos_putch(0x08);
                   10389:                                }
                   10390:                        }
                   10391:                } else {
                   10392:                        buf[p++] = chr;
                   10393:                        msdos_putch(chr);
                   10394:                }
                   10395:        }
                   10396:        REG16(AX) = p;
                   10397:        
                   10398: #ifdef USE_SERVICE_THREAD
                   10399:        service_exit = true;
                   10400: #endif
                   10401:        return(0);
                   10402: }
                   10403: 
1.1       root     10404: inline void msdos_int_21h_3fh()
                   10405: {
                   10406:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10407:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10408:        
1.1.1.20  root     10409:        if(fd < process->max_files && file_handler[fd].valid) {
                   10410:                if(file_mode[file_handler[fd].mode].in) {
                   10411:                        if(file_handler[fd].atty) {
1.1       root     10412:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     10413:                                if(REG16(CX) != 0) {
                   10414: #ifdef USE_SERVICE_THREAD
                   10415:                                        start_service_loop(msdos_int_21h_3fh_thread);
                   10416: #else
                   10417:                                        msdos_int_21h_3fh_thread(NULL);
                   10418:                                        REQUEST_HARDWRE_UPDATE();
                   10419: #endif
                   10420:                                } else {
                   10421:                                        REG16(AX) = 0;
1.1       root     10422:                                }
                   10423:                        } else {
1.1.1.37  root     10424:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10425:                        }
                   10426:                } else {
                   10427:                        REG16(AX) = 0x05;
1.1.1.3   root     10428:                        m_CF = 1;
1.1       root     10429:                }
                   10430:        } else {
                   10431:                REG16(AX) = 0x06;
1.1.1.3   root     10432:                m_CF = 1;
1.1       root     10433:        }
                   10434: }
                   10435: 
                   10436: inline void msdos_int_21h_40h()
                   10437: {
                   10438:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10439:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10440:        
1.1.1.20  root     10441:        if(fd < process->max_files && file_handler[fd].valid) {
                   10442:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     10443:                        if(REG16(CX)) {
1.1.1.20  root     10444:                                if(file_handler[fd].atty) {
1.1       root     10445:                                        // BX is stdout/stderr or is redirected to stdout
                   10446:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     10447:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     10448:                                        }
                   10449:                                        REG16(AX) = REG16(CX);
                   10450:                                } else {
1.1.1.20  root     10451:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10452:                                }
                   10453:                        } else {
1.1.1.20  root     10454:                                UINT32 pos = _tell(fd);
                   10455:                                _lseek(fd, 0, SEEK_END);
                   10456:                                UINT32 size = _tell(fd);
1.1.1.12  root     10457:                                if(pos < size) {
1.1.1.20  root     10458:                                        _lseek(fd, pos, SEEK_SET);
                   10459:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     10460:                                } else {
                   10461:                                        for(UINT32 i = size; i < pos; i++) {
                   10462:                                                UINT8 tmp = 0;
1.1.1.23  root     10463:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     10464:                                        }
1.1.1.20  root     10465:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     10466:                                }
1.1.1.23  root     10467:                                REG16(AX) = 0;
1.1       root     10468:                        }
                   10469:                } else {
                   10470:                        REG16(AX) = 0x05;
1.1.1.3   root     10471:                        m_CF = 1;
1.1       root     10472:                }
                   10473:        } else {
                   10474:                REG16(AX) = 0x06;
1.1.1.3   root     10475:                m_CF = 1;
1.1       root     10476:        }
                   10477: }
                   10478: 
                   10479: inline void msdos_int_21h_41h(int lfn)
                   10480: {
1.1.1.3   root     10481:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10482:                REG16(AX) = errno;
1.1.1.3   root     10483:                m_CF = 1;
1.1       root     10484:        }
                   10485: }
                   10486: 
                   10487: inline void msdos_int_21h_42h()
                   10488: {
                   10489:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10490:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10491:        
1.1.1.20  root     10492:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     10493:                if(REG8(AL) < 0x03) {
1.1.1.35  root     10494:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     10495:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   10496:                        UINT32 pos = _tell(fd);
1.1       root     10497:                        REG16(AX) = pos & 0xffff;
                   10498:                        REG16(DX) = (pos >> 16);
                   10499:                } else {
                   10500:                        REG16(AX) = 0x01;
1.1.1.3   root     10501:                        m_CF = 1;
1.1       root     10502:                }
                   10503:        } else {
                   10504:                REG16(AX) = 0x06;
1.1.1.3   root     10505:                m_CF = 1;
1.1       root     10506:        }
                   10507: }
                   10508: 
                   10509: inline void msdos_int_21h_43h(int lfn)
                   10510: {
1.1.1.45  root     10511:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     10512:        int attr;
                   10513:        
1.1.1.14  root     10514:        if(!lfn && REG8(AL) > 2) {
                   10515:                REG16(AX) = 0x01;
                   10516:                m_CF = 1;
                   10517:                return;
                   10518:        }
                   10519:        switch(REG8(lfn ? BL : AL)) {
1.1       root     10520:        case 0x00:
                   10521:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     10522:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   10523:                } else {
                   10524:                        REG16(AX) = (UINT16)GetLastError();
                   10525:                        m_CF = 1;
                   10526:                }
                   10527:                break;
                   10528:        case 0x01:
                   10529:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   10530:                        REG16(AX) = (UINT16)GetLastError();
                   10531:                        m_CF = 1;
                   10532:                }
                   10533:                break;
                   10534:        case 0x02:
                   10535:                {
1.1.1.45  root     10536:                        DWORD compressed_size = GetCompressedFileSize(path, NULL), file_size = 0;
                   10537:                        if(compressed_size != INVALID_FILE_SIZE) {
                   10538:                                if(compressed_size != 0) {
                   10539:                                        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10540:                                        if(hFile != INVALID_HANDLE_VALUE) {
                   10541:                                                file_size = GetFileSize(hFile, NULL);
                   10542:                                                CloseHandle(hFile);
                   10543:                                        }
                   10544:                                        if(compressed_size == file_size) {
                   10545:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   10546:                                                // this isn't correct if the file is in the NTFS MFT
                   10547:                                                if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   10548:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   10549:                                                }
1.1.1.14  root     10550:                                        }
                   10551:                                }
1.1.1.45  root     10552:                                REG16(AX) = LOWORD(compressed_size);
                   10553:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     10554:                        } else {
                   10555:                                REG16(AX) = (UINT16)GetLastError();
                   10556:                                m_CF = 1;
1.1       root     10557:                        }
1.1.1.14  root     10558:                }
                   10559:                break;
                   10560:        case 0x03:
                   10561:        case 0x05:
                   10562:        case 0x07:
                   10563:                {
                   10564:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10565:                        if(hFile != INVALID_HANDLE_VALUE) {
                   10566:                                FILETIME local, time;
                   10567:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   10568:                                if(REG8(BL) == 7) {
                   10569:                                        ULARGE_INTEGER hund;
                   10570:                                        hund.LowPart = local.dwLowDateTime;
                   10571:                                        hund.HighPart = local.dwHighDateTime;
                   10572:                                        hund.QuadPart += REG16(SI) * 100000;
                   10573:                                        local.dwLowDateTime = hund.LowPart;
                   10574:                                        local.dwHighDateTime = hund.HighPart;
                   10575:                                }
                   10576:                                LocalFileTimeToFileTime(&local, &time);
                   10577:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   10578:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   10579:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   10580:                                        REG16(AX) = (UINT16)GetLastError();
                   10581:                                        m_CF = 1;
                   10582:                                }
                   10583:                                CloseHandle(hFile);
                   10584:                        } else {
                   10585:                                REG16(AX) = (UINT16)GetLastError();
                   10586:                                m_CF = 1;
1.1       root     10587:                        }
1.1.1.14  root     10588:                }
                   10589:                break;
                   10590:        case 0x04:
                   10591:        case 0x06:
                   10592:        case 0x08:
                   10593:                {
                   10594:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   10595:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   10596:                                FILETIME *time, local;
                   10597:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   10598:                                                   0x06 ? &fad.ftLastAccessTime :
                   10599:                                                          &fad.ftCreationTime;
                   10600:                                FileTimeToLocalFileTime(time, &local);
                   10601:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   10602:                                if(REG8(BL) == 0x08) {
                   10603:                                        ULARGE_INTEGER hund;
                   10604:                                        hund.LowPart = local.dwLowDateTime;
                   10605:                                        hund.HighPart = local.dwHighDateTime;
                   10606:                                        hund.QuadPart /= 100000;
                   10607:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   10608:                                }
                   10609:                        } else {
                   10610:                                REG16(AX) = (UINT16)GetLastError();
                   10611:                                m_CF = 1;
1.1       root     10612:                        }
1.1.1.14  root     10613:                }
                   10614:                break;
1.1.1.43  root     10615:        case 0xff:
                   10616:                if(REG16(BP) == 0x5053) {
                   10617:                        if(REG8(CL) == 0x39) {
                   10618:                                msdos_int_21h_39h(1);
                   10619:                                break;
                   10620:                        } else if(REG8(CL) == 0x56) {
                   10621:                                msdos_int_21h_56h(1);
                   10622:                                break;
                   10623:                        }
                   10624:                }
1.1.1.14  root     10625:        default:
1.1.1.22  root     10626:                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     10627:                REG16(AX) = 0x01;
                   10628:                m_CF = 1;
                   10629:                break;
                   10630:        }
                   10631: }
                   10632: 
                   10633: inline void msdos_int_21h_44h()
                   10634: {
1.1.1.22  root     10635:        static UINT16 iteration_count = 0;
                   10636:        
1.1.1.44  root     10637:        process_t *process;
                   10638:        int fd, drv;
1.1.1.14  root     10639:        
                   10640:        switch(REG8(AL)) {
                   10641:        case 0x00:
                   10642:        case 0x01:
                   10643:        case 0x02:
                   10644:        case 0x03:
                   10645:        case 0x04:
                   10646:        case 0x05:
                   10647:        case 0x06:
                   10648:        case 0x07:
1.1.1.44  root     10649:                process = msdos_process_info_get(current_psp);
                   10650:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     10651:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   10652:                        REG16(AX) = 0x06;
                   10653:                        m_CF = 1;
                   10654:                        return;
1.1.1.14  root     10655:                }
                   10656:                break;
                   10657:        case 0x08:
                   10658:        case 0x09:
1.1.1.44  root     10659:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   10660:                if(!msdos_is_valid_drive(drv)) {
                   10661:                        // invalid drive
1.1.1.14  root     10662:                        REG16(AX) = 0x0f;
                   10663:                        m_CF = 1;
                   10664:                        return;
1.1       root     10665:                }
                   10666:                break;
                   10667:        }
                   10668:        switch(REG8(AL)) {
                   10669:        case 0x00: // get ioctrl data
1.1.1.20  root     10670:                REG16(DX) = file_handler[fd].info;
1.1       root     10671:                break;
                   10672:        case 0x01: // set ioctrl data
1.1.1.45  root     10673:                if(REG8(DH) != 0) {
                   10674: //                     REG16(AX) = 0x0d; // data invalid
                   10675: //                     m_CF = 1;
                   10676:                        file_handler[fd].info = REG16(DX);
                   10677:                } else {
                   10678:                        file_handler[fd].info &= 0xff00;
                   10679:                        file_handler[fd].info |= REG8(DL);
                   10680:                }
1.1       root     10681:                break;
                   10682:        case 0x02: // recv from character device
1.1.1.45  root     10683:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
                   10684:                        // from DOSBox
                   10685:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
                   10686:                        case 0x00:
                   10687:                                if(REG16(CX) >= 6) {
                   10688:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
                   10689:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
                   10690:                                        REG16(AX) = 6; // number of bytes actually read
                   10691:                                } else {
                   10692:                                        REG16(AX) = 0x0d; // data invalid
                   10693:                                        m_CF = 1;
                   10694:                                }
                   10695:                                break;
                   10696:                        case 0x01:
                   10697:                                if(REG16(CX) >= 6) {
                   10698:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
                   10699:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
                   10700:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
                   10701:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
                   10702:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
                   10703:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
                   10704:                                                        int page = (addr - EMS_TOP) / 0x4000;
                   10705:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
                   10706:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   10707:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
                   10708:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
                   10709:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
                   10710:                                                } else {
                   10711:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
                   10712:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   10713:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
                   10714:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
                   10715:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
                   10716:                                                }
                   10717:                                        }
                   10718:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
                   10719:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
                   10720:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
                   10721:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
                   10722:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
                   10723:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
                   10724:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
                   10725:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
                   10726:                                        
                   10727:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
                   10728:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
                   10729:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
                   10730:                                        REG16(AX) = 6; // number of bytes actually read
                   10731:                                } else {
                   10732:                                        REG16(AX) = 0x0d; // data invalid
                   10733:                                        m_CF = 1;
                   10734:                                }
                   10735:                                break;
                   10736:                        case 0x02:
                   10737:                                if(REG16(CX) >= 2) {
                   10738:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
                   10739:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
                   10740:                                        REG16(AX) = 2; // number of bytes actually read
                   10741:                                } else {
                   10742:                                        REG16(AX) = 0x0d; // data invalid
                   10743:                                        m_CF = 1;
                   10744:                                }
                   10745:                                break;
                   10746:                        case 0x03:
                   10747:                                if(REG16(CX) >= 4) {
                   10748:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
                   10749:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
                   10750:                                        REG16(AX) = 4; // number of bytes actually read
                   10751:                                } else {
                   10752:                                        REG16(AX) = 0x0d; // data invalid
                   10753:                                        m_CF = 1;
                   10754:                                }
                   10755:                                break;
                   10756:                        default:
                   10757:                                REG16(AX) = 0x01; // function number invalid
                   10758:                                m_CF = 1;
                   10759:                        }
                   10760:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
                   10761:                        if(REG16(CX) >= 5) {
                   10762:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
                   10763:                                REG16(AX) = 5; // number of bytes actually read
                   10764:                        } else {
                   10765:                                REG16(AX) = 0x0d; // data invalid
                   10766:                                m_CF = 1;
                   10767:                        }
                   10768:                } else {
                   10769: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
                   10770: //                     REG16(AX) = REG16(CX);
                   10771:                        REG16(AX) = 0x05; // access denied
                   10772:                        m_CF = 1;
                   10773:                }
                   10774:                break;
1.1       root     10775:        case 0x03: // send to character device
1.1.1.45  root     10776: //             REG16(AX) = 0x05;
                   10777: //             m_CF = 1;
                   10778:                REG16(AX) = 0x00; // success
                   10779:                break;
1.1       root     10780:        case 0x04: // recv from block device
                   10781:        case 0x05: // send to block device
                   10782:                REG16(AX) = 0x05;
1.1.1.3   root     10783:                m_CF = 1;
1.1       root     10784:                break;
                   10785:        case 0x06: // get read status
1.1.1.20  root     10786:                if(file_mode[file_handler[fd].mode].in) {
                   10787:                        if(file_handler[fd].atty) {
1.1.1.14  root     10788:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     10789:                        } else {
1.1.1.20  root     10790:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     10791:                        }
1.1.1.14  root     10792:                } else {
                   10793:                        REG8(AL) = 0x00;
1.1       root     10794:                }
                   10795:                break;
                   10796:        case 0x07: // get write status
1.1.1.20  root     10797:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     10798:                        REG8(AL) = 0xff;
                   10799:                } else {
                   10800:                        REG8(AL) = 0x00;
1.1       root     10801:                }
                   10802:                break;
                   10803:        case 0x08: // check removable drive
1.1.1.44  root     10804:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     10805:                        // removable drive
                   10806:                        REG16(AX) = 0x00;
1.1       root     10807:                } else {
1.1.1.14  root     10808:                        // fixed drive
                   10809:                        REG16(AX) = 0x01;
1.1       root     10810:                }
                   10811:                break;
                   10812:        case 0x09: // check remote drive
1.1.1.44  root     10813:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     10814:                        // remote drive
                   10815:                        REG16(DX) = 0x1000;
1.1.1.44  root     10816:                } else if(msdos_is_subst_drive(drv)) {
                   10817:                        // subst drive
                   10818:                        REG16(DX) = 0x8000;
1.1       root     10819:                } else {
1.1.1.14  root     10820:                        // local drive
1.1.1.44  root     10821:                        REG16(DX) = 0x0000;
1.1       root     10822:                }
                   10823:                break;
1.1.1.21  root     10824:        case 0x0a: // check remote handle
1.1.1.45  root     10825:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
                   10826:                        REG16(DX) = 0x8000;
                   10827:                } else {
                   10828:                        REG16(DX) = 0x0000;
                   10829:                }
1.1.1.21  root     10830:                break;
1.1       root     10831:        case 0x0b: // set retry count
                   10832:                break;
1.1.1.22  root     10833:        case 0x0c: // generic character device request
                   10834:                if(REG8(CL) == 0x45) {
                   10835:                        // set iteration (retry) count
                   10836:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   10837:                } else if(REG8(CL) == 0x4a) {
                   10838:                        // select code page
                   10839:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   10840:                        msdos_nls_tables_update();
1.1.1.44  root     10841:                } else if(REG8(CL) == 0x4c) {
                   10842:                        // start code-page preparation
                   10843:                        int ids[3] = {437, 0, 0}; // 437: US English
                   10844:                        int count = 1, offset = 0;
                   10845:                        if(active_code_page != 437) {
                   10846:                                ids[count++] = active_code_page;
                   10847:                        }
                   10848:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   10849:                                ids[count++] = system_code_page;
                   10850:                        }
                   10851:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   10852:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   10853:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   10854:                        for(int i = 0; i < count; i++) {
                   10855:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   10856:                        }
                   10857:                } else if(REG8(CL) == 0x4d) {
                   10858:                        // end code-page preparation
                   10859:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   10860:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.22  root     10861:                } else if(REG8(CL) == 0x65) {
                   10862:                        // get iteration (retry) count
                   10863:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   10864:                } else if(REG8(CL) == 0x6a) {
                   10865:                        // query selected code page
                   10866:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   10867:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   10868:                        
                   10869:                        CPINFO info;
                   10870:                        GetCPInfo(active_code_page, &info);
                   10871:                        
                   10872:                        if(info.MaxCharSize != 1) {
                   10873:                                for(int i = 0;; i++) {
                   10874:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   10875:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   10876:                                        
                   10877:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   10878:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   10879:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   10880:                                        
                   10881:                                        if(lo == 0 && hi == 0) {
                   10882:                                                break;
                   10883:                                        }
                   10884:                                }
                   10885:                        }
1.1.1.44  root     10886:                } else if(REG8(CL) == 0x6b) {
                   10887:                        // query prepare list
                   10888:                        int ids[3] = {437, 0, 0}; // 437: US English
                   10889:                        int count = 1, offset = 0;
                   10890:                        if(active_code_page != 437) {
                   10891:                                ids[count++] = active_code_page;
                   10892:                        }
                   10893:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   10894:                                ids[count++] = system_code_page;
                   10895:                        }
                   10896:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   10897:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   10898:                        for(int i = 0; i < count; i++) {
                   10899:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   10900:                        }
                   10901:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   10902:                        for(int i = 0; i < count; i++) {
                   10903:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   10904:                        }
1.1.1.22  root     10905:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     10906:                        // get display information
1.1.1.22  root     10907:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;
                   10908:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;
                   10909:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;
                   10910:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;
                   10911:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;
                   10912:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;
                   10913:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;
                   10914:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a));
                   10915:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);
                   10916:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a);
                   10917:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;
                   10918:                } else {
                   10919:                        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));
                   10920:                        REG16(AX) = 0x01; // invalid function
                   10921:                        m_CF = 1;
                   10922:                }
                   10923:                break;
                   10924:        case 0x0d: // generic block device request
                   10925:                if(REG8(CL) == 0x40) {
                   10926:                        // set device parameters
                   10927:                } else if(REG8(CL) == 0x46) {
                   10928:                        // set volume serial number
                   10929:                } else if(REG8(CL) == 0x4a) {
                   10930:                        // lock logical volume
                   10931:                } else if(REG8(CL) == 0x4b) {
                   10932:                        // lock physical volume
                   10933:                } else if(REG8(CL) == 0x60) {
                   10934:                        // get device parameters
1.1.1.42  root     10935:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     10936:                        
1.1.1.42  root     10937:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   10938:                                drive_param_t *drive_param = &drive_params[drive_num];
                   10939:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   10940:                                
                   10941:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   10942:                                switch(geo->MediaType) {
                   10943:                                case F5_360_512:
                   10944:                                case F5_320_512:
                   10945:                                case F5_320_1024:
                   10946:                                case F5_180_512:
                   10947:                                case F5_160_512:
                   10948:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   10949:                                        break;
                   10950:                                case F5_1Pt2_512:
                   10951:                                case F3_1Pt2_512:
                   10952:                                case F3_1Pt23_1024:
                   10953:                                case F5_1Pt23_1024:
                   10954:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   10955:                                        break;
                   10956:                                case F3_720_512:
                   10957:                                case F3_640_512:
                   10958:                                case F5_640_512:
                   10959:                                case F5_720_512:
                   10960:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   10961:                                        break;
                   10962:                                case F8_256_128:
                   10963:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   10964:                                        break;
                   10965:                                case FixedMedia:
                   10966:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   10967:                                        break;
                   10968:                                case F3_1Pt44_512:
                   10969:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   10970:                                        break;
                   10971:                                case F3_2Pt88_512:
                   10972:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   10973:                                        break;
                   10974:                                default:
                   10975:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   10976: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   10977:                                        break;
1.1.1.22  root     10978:                                }
1.1.1.42  root     10979:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   10980:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   10981:                                switch(geo->MediaType) {
                   10982:                                case F5_360_512:
                   10983:                                case F5_320_512:
                   10984:                                case F5_320_1024:
                   10985:                                case F5_180_512:
                   10986:                                case F5_160_512:
                   10987:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   10988:                                        break;
                   10989:                                default:
                   10990:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   10991:                                        break;
                   10992:                                }
                   10993:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   10994:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   10995:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   10996:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   10997:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   10998:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   10999:                                switch(geo->MediaType) {
                   11000:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   11001:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   11002:                                        break;
                   11003:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   11004:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   11005:                                        break;
                   11006:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   11007:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11008:                                        break;
                   11009:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11010:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11011:                                        break;
                   11012:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11013:                                case F3_1Pt2_512:
                   11014:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11015:                                case F5_720_512:
                   11016:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11017:                                        break;
                   11018:                                case FixedMedia:        // hard disk
                   11019:                                case RemovableMedia:
                   11020:                                case Unknown:
                   11021:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11022:                                        break;
                   11023:                                default:
                   11024:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11025:                                        break;
                   11026:                                }
                   11027:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11028:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11029:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11030:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11031:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11032:                                // 21h  BYTE    device type
                   11033:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11034:                        } else {
                   11035:                                REG16(AX) = 0x0f; // invalid drive
                   11036:                                m_CF = 1;
                   11037:                        }
                   11038:                } else if(REG8(CL) == 0x66) {
                   11039:                        // get volume serial number
                   11040:                        char path[] = "A:\\";
                   11041:                        char volume_label[MAX_PATH];
                   11042:                        DWORD serial_number = 0;
                   11043:                        char file_system[MAX_PATH];
                   11044:                        
                   11045:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11046:                        
                   11047:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   11048:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11049:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11050:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11051:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11052:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11053:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11054:                        } else {
                   11055:                                REG16(AX) = 0x0f; // invalid drive
                   11056:                                m_CF = 1;
                   11057:                        }
                   11058:                } else if(REG8(CL) == 0x67) {
                   11059:                        // get access flag
                   11060:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11061:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11062:                } else if(REG8(CL) == 0x68) {
                   11063:                        // sense media type
1.1.1.42  root     11064:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11065:                        
1.1.1.42  root     11066:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11067:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11068:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11069:                                
                   11070:                                switch(geo->MediaType) {
                   11071:                                case F3_720_512:
                   11072:                                case F5_720_512:
                   11073:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11074:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   11075:                                        break;
                   11076:                                case F3_1Pt44_512:
                   11077:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11078:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   11079:                                        break;
                   11080:                                case F3_2Pt88_512:
                   11081:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11082:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   11083:                                        break;
                   11084:                                default:
                   11085:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   11086:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   11087:                                        break;
1.1.1.22  root     11088:                                }
                   11089:                        } else {
                   11090:                                REG16(AX) = 0x0f; // invalid drive
                   11091:                                m_CF = 1;
                   11092:                        }
                   11093:                } else if(REG8(CL) == 0x6a) {
                   11094:                        // unlock logical volume
                   11095:                } else if(REG8(CL) == 0x6b) {
                   11096:                        // unlock physical volume
                   11097:                } else {
                   11098:                        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));
                   11099:                        REG16(AX) = 0x01; // invalid function
                   11100:                        m_CF = 1;
                   11101:                }
                   11102:                break;
                   11103:        case 0x0e: // get logical drive map
1.1.1.44  root     11104:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11105:                        REG16(AX) = 0x0f; // invalid drive
                   11106:                        m_CF = 1;
                   11107:                } else {
                   11108:                        REG8(AL) = 0;
1.1.1.22  root     11109:                }
                   11110:                break;
                   11111:        case 0x0f: // set logical drive map
1.1.1.44  root     11112:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11113:                        REG16(AX) = 0x0f; // invalid drive
                   11114:                        m_CF = 1;
1.1.1.22  root     11115:                }
                   11116:                break;
                   11117:        case 0x10: // query generic ioctrl capability (handle)
                   11118:                switch(REG8(CL)) {
                   11119:                case 0x45:
                   11120:                case 0x4a:
                   11121:                case 0x65:
                   11122:                case 0x6a:
                   11123:                case 0x7f:
                   11124:                        REG16(AX) = 0x0000; // supported
                   11125:                        break;
                   11126:                default:
                   11127:                        REG8(AL) = 0x01; // ioctl capability not available
                   11128:                        m_CF = 1;
                   11129:                        break;
                   11130:                }
                   11131:                break;
                   11132:        case 0x11: // query generic ioctrl capability (drive)
                   11133:                switch(REG8(CL)) {
                   11134:                case 0x40:
                   11135:                case 0x46:
                   11136:                case 0x4a:
                   11137:                case 0x4b:
                   11138:                case 0x60:
                   11139:                case 0x66:
                   11140:                case 0x67:
                   11141:                case 0x68:
                   11142:                case 0x6a:
                   11143:                case 0x6b:
                   11144:                        REG16(AX) = 0x0000; // supported
                   11145:                        break;
                   11146:                default:
                   11147:                        REG8(AL) = 0x01; // ioctl capability not available
                   11148:                        m_CF = 1;
                   11149:                        break;
                   11150:                }
                   11151:                break;
                   11152:        case 0x12: // determine dos type
                   11153:        case 0x51: // concurrent dos v3.2+ - installation check
                   11154:        case 0x52: // determine dos type/get dr dos versuin
                   11155:                REG16(AX) = 0x01; // this  is not DR-DOS
                   11156:                m_CF = 1;
                   11157:                break;
1.1       root     11158:        default:
1.1.1.22  root     11159:                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     11160:                REG16(AX) = 0x01;
1.1.1.3   root     11161:                m_CF = 1;
1.1       root     11162:                break;
                   11163:        }
                   11164: }
                   11165: 
                   11166: inline void msdos_int_21h_45h()
                   11167: {
                   11168:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11169:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11170:        
1.1.1.20  root     11171:        if(fd < process->max_files && file_handler[fd].valid) {
                   11172:                int dup_fd = _dup(fd);
                   11173:                if(dup_fd != -1) {
                   11174:                        REG16(AX) = dup_fd;
                   11175:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11176: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11177:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11178:                } else {
                   11179:                        REG16(AX) = errno;
1.1.1.3   root     11180:                        m_CF = 1;
1.1       root     11181:                }
                   11182:        } else {
                   11183:                REG16(AX) = 0x06;
1.1.1.3   root     11184:                m_CF = 1;
1.1       root     11185:        }
                   11186: }
                   11187: 
                   11188: inline void msdos_int_21h_46h()
                   11189: {
                   11190:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11191:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11192:        int dup_fd = REG16(CX);
                   11193:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     11194:        
1.1.1.20  root     11195:        if(REG16(BX) == REG16(CX)) {
                   11196:                REG16(AX) = 0x06;
                   11197:                m_CF = 1;
                   11198:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   11199:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   11200:                        _close(tmp_fd);
                   11201:                        msdos_file_handler_close(tmp_fd);
                   11202:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   11203:                }
                   11204:                if(_dup2(fd, dup_fd) != -1) {
                   11205:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11206: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11207:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11208:                } else {
                   11209:                        REG16(AX) = errno;
1.1.1.3   root     11210:                        m_CF = 1;
1.1       root     11211:                }
                   11212:        } else {
                   11213:                REG16(AX) = 0x06;
1.1.1.3   root     11214:                m_CF = 1;
1.1       root     11215:        }
                   11216: }
                   11217: 
                   11218: inline void msdos_int_21h_47h(int lfn)
                   11219: {
                   11220:        char path[MAX_PATH];
                   11221:        
                   11222:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45  root     11223:                if(!lfn) {
                   11224:                        strcpy(path, msdos_short_path(path));
                   11225:                }
1.1       root     11226:                if(path[1] == ':') {
                   11227:                        // the returned path does not include a drive or the initial backslash
1.1.1.45  root     11228:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     11229:                } else {
1.1.1.45  root     11230:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     11231:                }
                   11232:        } else {
                   11233:                REG16(AX) = errno;
1.1.1.3   root     11234:                m_CF = 1;
1.1       root     11235:        }
                   11236: }
                   11237: 
                   11238: inline void msdos_int_21h_48h()
                   11239: {
1.1.1.19  root     11240:        int seg, umb_linked;
1.1       root     11241:        
1.1.1.8   root     11242:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     11243:                // unlink umb not to allocate memory in umb
                   11244:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   11245:                        msdos_mem_unlink_umb();
                   11246:                }
1.1.1.8   root     11247:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11248:                        REG16(AX) = seg;
                   11249:                } else {
                   11250:                        REG16(AX) = 0x08;
                   11251:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   11252:                        m_CF = 1;
                   11253:                }
1.1.1.19  root     11254:                if(umb_linked != 0) {
                   11255:                        msdos_mem_link_umb();
                   11256:                }
1.1.1.8   root     11257:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   11258:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11259:                        REG16(AX) = seg;
                   11260:                } else {
                   11261:                        REG16(AX) = 0x08;
                   11262:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   11263:                        m_CF = 1;
                   11264:                }
                   11265:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   11266:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11267:                        REG16(AX) = seg;
                   11268:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11269:                        REG16(AX) = seg;
                   11270:                } else {
                   11271:                        REG16(AX) = 0x08;
                   11272:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   11273:                        m_CF = 1;
                   11274:                }
1.1       root     11275:        }
                   11276: }
                   11277: 
                   11278: inline void msdos_int_21h_49h()
                   11279: {
1.1.1.14  root     11280:        int mcb_seg = SREG(ES) - 1;
                   11281:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   11282:        
                   11283:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11284:                msdos_mem_free(SREG(ES));
                   11285:        } else {
1.1.1.33  root     11286:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     11287:                m_CF = 1;
                   11288:        }
1.1       root     11289: }
                   11290: 
                   11291: inline void msdos_int_21h_4ah()
                   11292: {
1.1.1.14  root     11293:        int mcb_seg = SREG(ES) - 1;
                   11294:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     11295:        int max_paragraphs;
                   11296:        
1.1.1.14  root     11297:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11298:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   11299:                        REG16(AX) = 0x08;
                   11300:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   11301:                        m_CF = 1;
                   11302:                }
                   11303:        } else {
1.1.1.33  root     11304:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     11305:                m_CF = 1;
1.1       root     11306:        }
                   11307: }
                   11308: 
                   11309: inline void msdos_int_21h_4bh()
                   11310: {
1.1.1.3   root     11311:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11312:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     11313:        
                   11314:        switch(REG8(AL)) {
                   11315:        case 0x00:
                   11316:        case 0x01:
                   11317:                if(msdos_process_exec(command, param, REG8(AL))) {
                   11318:                        REG16(AX) = 0x02;
1.1.1.3   root     11319:                        m_CF = 1;
1.1       root     11320:                }
                   11321:                break;
1.1.1.14  root     11322:        case 0x03:
                   11323:                {
                   11324:                        int fd;
                   11325:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   11326:                                REG16(AX) = 0x02;
                   11327:                                m_CF = 1;
                   11328:                                break;
                   11329:                        }
                   11330:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   11331:                        _close(fd);
                   11332:                        
                   11333:                        UINT16 *overlay = (UINT16 *)param;
                   11334:                        
                   11335:                        // check exe header
                   11336:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   11337:                        int header_size = 0;
                   11338:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   11339:                                header_size = header->header_size * 16;
                   11340:                                // relocation
                   11341:                                int start_seg = overlay[1];
                   11342:                                for(int i = 0; i < header->relocations; i++) {
                   11343:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   11344:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   11345:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   11346:                                }
                   11347:                        }
                   11348:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   11349:                }
                   11350:                break;
1.1.1.43  root     11351:        case 0xfd:
                   11352:        case 0xfe:
                   11353:                // unknown function called in FreeCOM
                   11354:                REG16(AX) = 0x01;
                   11355:                m_CF = 1;
                   11356:                break;
1.1       root     11357:        default:
1.1.1.22  root     11358:                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     11359:                REG16(AX) = 0x01;
1.1.1.3   root     11360:                m_CF = 1;
1.1       root     11361:                break;
                   11362:        }
                   11363: }
                   11364: 
                   11365: inline void msdos_int_21h_4ch()
                   11366: {
                   11367:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   11368: }
                   11369: 
                   11370: inline void msdos_int_21h_4dh()
                   11371: {
                   11372:        REG16(AX) = retval;
                   11373: }
                   11374: 
                   11375: inline void msdos_int_21h_4eh()
                   11376: {
                   11377:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11378:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11379:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45  root     11380:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11381:        WIN32_FIND_DATA fd;
                   11382:        
1.1.1.14  root     11383:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   11384:        find->find_magic = FIND_MAGIC;
                   11385:        find->dta_index = dtainfo - dtalist;
1.1       root     11386:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     11387:        dtainfo->allowable_mask = REG8(CL);
                   11388:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     11389:        
1.1.1.14  root     11390:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   11391:                dtainfo->allowable_mask &= ~8;
1.1       root     11392:        }
1.1.1.14  root     11393:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   11394:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11395:                      !msdos_find_file_has_8dot3name(&fd)) {
                   11396:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11397:                                FindClose(dtainfo->find_handle);
                   11398:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11399:                                break;
                   11400:                        }
                   11401:                }
                   11402:        }
1.1.1.13  root     11403:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11404:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11405:                msdos_find_file_conv_local_time(&fd);
                   11406:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11407:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11408:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11409:                REG16(AX) = 0;
1.1.1.14  root     11410:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11411:                find->attrib = 8;
                   11412:                find->size = 0;
                   11413:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11414:                dtainfo->allowable_mask &= ~8;
1.1       root     11415:                REG16(AX) = 0;
                   11416:        } else {
                   11417:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     11418:                m_CF = 1;
1.1       root     11419:        }
                   11420: }
                   11421: 
                   11422: inline void msdos_int_21h_4fh()
                   11423: {
                   11424:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11425:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11426:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     11427:        WIN32_FIND_DATA fd;
                   11428:        
1.1.1.14  root     11429:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   11430:                REG16(AX) = 0x12;
                   11431:                m_CF = 1;
                   11432:                return;
                   11433:        }
                   11434:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     11435:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11436:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     11437:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11438:                              !msdos_find_file_has_8dot3name(&fd)) {
                   11439:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11440:                                        FindClose(dtainfo->find_handle);
                   11441:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11442:                                        break;
                   11443:                                }
                   11444:                        }
                   11445:                } else {
1.1.1.13  root     11446:                        FindClose(dtainfo->find_handle);
                   11447:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11448:                }
                   11449:        }
1.1.1.13  root     11450:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11451:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11452:                msdos_find_file_conv_local_time(&fd);
                   11453:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11454:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11455:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11456:                REG16(AX) = 0;
1.1.1.14  root     11457:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11458:                find->attrib = 8;
                   11459:                find->size = 0;
                   11460:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11461:                dtainfo->allowable_mask &= ~8;
1.1       root     11462:                REG16(AX) = 0;
                   11463:        } else {
                   11464:                REG16(AX) = 0x12;
1.1.1.3   root     11465:                m_CF = 1;
1.1       root     11466:        }
                   11467: }
                   11468: 
                   11469: inline void msdos_int_21h_50h()
                   11470: {
1.1.1.8   root     11471:        if(current_psp != REG16(BX)) {
                   11472:                process_t *process = msdos_process_info_get(current_psp);
                   11473:                if(process != NULL) {
                   11474:                        process->psp = REG16(BX);
                   11475:                }
                   11476:                current_psp = REG16(BX);
1.1.1.23  root     11477:                msdos_sda_update(current_psp);
1.1.1.8   root     11478:        }
1.1       root     11479: }
                   11480: 
                   11481: inline void msdos_int_21h_51h()
                   11482: {
                   11483:        REG16(BX) = current_psp;
                   11484: }
                   11485: 
                   11486: inline void msdos_int_21h_52h()
                   11487: {
1.1.1.25  root     11488:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     11489:        i386_load_segment_descriptor(ES);
1.1.1.25  root     11490:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     11491: }
                   11492: 
1.1.1.43  root     11493: inline void msdos_int_21h_53h()
                   11494: {
                   11495:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   11496:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   11497:        
                   11498:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   11499:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   11500:        dpb->shift_count = 0;
                   11501:        dpb->reserved_sectors = 0;
                   11502:        dpb->fat_num = bpb->fat_num;
                   11503:        dpb->root_entries = bpb->root_entries;
                   11504:        dpb->first_data_sector = 0;
                   11505:        if(bpb->sectors_per_cluster != 0) {
                   11506:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   11507:        } else {
                   11508:                dpb->highest_cluster_num = 0;
                   11509:        }
                   11510:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   11511:        dpb->first_dir_sector = 0;
                   11512:        dpb->device_driver_header = 0;
                   11513:        dpb->media_type = bpb->media_type;
                   11514:        dpb->drive_accessed = 0;
                   11515:        dpb->next_dpb_ofs = 0xffff;
                   11516:        dpb->next_dpb_seg = 0xffff;
                   11517:        dpb->first_free_cluster = 0;
                   11518:        dpb->free_clusters = 0xffff;
                   11519: }
                   11520: 
1.1       root     11521: inline void msdos_int_21h_54h()
                   11522: {
                   11523:        process_t *process = msdos_process_info_get(current_psp);
                   11524:        
                   11525:        REG8(AL) = process->verify;
                   11526: }
                   11527: 
                   11528: inline void msdos_int_21h_55h()
                   11529: {
                   11530:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   11531:        
                   11532:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   11533:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   11534:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   11535:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   11536:        psp->parent_psp = current_psp;
                   11537: }
                   11538: 
                   11539: inline void msdos_int_21h_56h(int lfn)
                   11540: {
                   11541:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     11542:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   11543:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     11544:        
                   11545:        if(rename(src, dst)) {
                   11546:                REG16(AX) = errno;
1.1.1.3   root     11547:                m_CF = 1;
1.1       root     11548:        }
                   11549: }
                   11550: 
                   11551: inline void msdos_int_21h_57h()
                   11552: {
                   11553:        FILETIME time, local;
1.1.1.14  root     11554:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     11555:        HANDLE hHandle;
1.1       root     11556:        
1.1.1.21  root     11557:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     11558:                REG16(AX) = (UINT16)GetLastError();
                   11559:                m_CF = 1;
                   11560:                return;
                   11561:        }
                   11562:        ctime = atime = mtime = NULL;
                   11563:        
1.1       root     11564:        switch(REG8(AL)) {
                   11565:        case 0x00:
1.1.1.6   root     11566:        case 0x01:
1.1.1.14  root     11567:                mtime = &time;
1.1.1.6   root     11568:                break;
                   11569:        case 0x04:
                   11570:        case 0x05:
1.1.1.14  root     11571:                atime = &time;
1.1       root     11572:                break;
1.1.1.6   root     11573:        case 0x06:
                   11574:        case 0x07:
1.1.1.14  root     11575:                ctime = &time;
                   11576:                break;
                   11577:        default:
1.1.1.22  root     11578:                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     11579:                REG16(AX) = 0x01;
                   11580:                m_CF = 1;
                   11581:                return;
                   11582:        }
                   11583:        if(REG8(AL) & 1) {
1.1       root     11584:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   11585:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     11586:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     11587:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11588:                        m_CF = 1;
1.1       root     11589:                }
1.1.1.14  root     11590:        } else {
1.1.1.21  root     11591:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     11592:                        // assume a device and use the current time
                   11593:                        GetSystemTimeAsFileTime(&time);
                   11594:                }
                   11595:                FileTimeToLocalFileTime(&time, &local);
                   11596:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     11597:        }
                   11598: }
                   11599: 
                   11600: inline void msdos_int_21h_58h()
                   11601: {
                   11602:        switch(REG8(AL)) {
                   11603:        case 0x00:
1.1.1.7   root     11604:                REG16(AX) = malloc_strategy;
                   11605:                break;
                   11606:        case 0x01:
1.1.1.24  root     11607: //             switch(REG16(BX)) {
                   11608:                switch(REG8(BL)) {
1.1.1.7   root     11609:                case 0x0000:
                   11610:                case 0x0001:
                   11611:                case 0x0002:
                   11612:                case 0x0040:
                   11613:                case 0x0041:
                   11614:                case 0x0042:
                   11615:                case 0x0080:
                   11616:                case 0x0081:
                   11617:                case 0x0082:
                   11618:                        malloc_strategy = REG16(BX);
1.1.1.23  root     11619:                        msdos_sda_update(current_psp);
1.1.1.7   root     11620:                        break;
                   11621:                default:
1.1.1.22  root     11622:                        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     11623:                        REG16(AX) = 0x01;
                   11624:                        m_CF = 1;
                   11625:                        break;
                   11626:                }
                   11627:                break;
                   11628:        case 0x02:
1.1.1.19  root     11629:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     11630:                break;
                   11631:        case 0x03:
1.1.1.24  root     11632: //             switch(REG16(BX)) {
                   11633:                switch(REG8(BL)) {
1.1.1.7   root     11634:                case 0x0000:
1.1.1.19  root     11635:                        msdos_mem_unlink_umb();
                   11636:                        break;
1.1.1.7   root     11637:                case 0x0001:
1.1.1.19  root     11638:                        msdos_mem_link_umb();
1.1.1.7   root     11639:                        break;
                   11640:                default:
1.1.1.22  root     11641:                        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     11642:                        REG16(AX) = 0x01;
                   11643:                        m_CF = 1;
                   11644:                        break;
                   11645:                }
1.1       root     11646:                break;
                   11647:        default:
1.1.1.22  root     11648:                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     11649:                REG16(AX) = 0x01;
1.1.1.3   root     11650:                m_CF = 1;
1.1       root     11651:                break;
                   11652:        }
                   11653: }
                   11654: 
                   11655: inline void msdos_int_21h_59h()
                   11656: {
1.1.1.23  root     11657:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   11658:        
                   11659:        REG16(AX) = sda->extended_error_code;
1.1.1.43  root     11660:        REG8(BL)  = sda->suggested_action;
                   11661:        REG8(BH)  = sda->error_class;
                   11662:        REG8(CL)  = sda->byte_reserved_1;
                   11663:        REG8(CH)  = sda->locus_of_last_error;
                   11664:        REG16(DX) = sda->word_reserved_1;
                   11665:        REG16(SI) = sda->word_reserved_2;
                   11666:        REG16(DI) = sda->last_error_pointer.w.l;
                   11667:        SREG(DS)  = sda->word_reserved_3;
                   11668:        i386_load_segment_descriptor(DS);
                   11669:        SREG(ES)  = sda->last_error_pointer.w.h;
                   11670:        i386_load_segment_descriptor(ES);
1.1       root     11671: }
                   11672: 
                   11673: inline void msdos_int_21h_5ah()
                   11674: {
1.1.1.3   root     11675:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11676:        int len = strlen(path);
                   11677:        char tmp[MAX_PATH];
                   11678:        
                   11679:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   11680:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11681:                
                   11682:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11683:                REG16(AX) = fd;
                   11684:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11685:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11686:                
                   11687:                strcpy(path, tmp);
                   11688:                int dx = REG16(DX) + len;
1.1.1.3   root     11689:                int ds = SREG(DS);
1.1       root     11690:                while(dx > 0xffff) {
                   11691:                        dx -= 0x10;
                   11692:                        ds++;
                   11693:                }
                   11694:                REG16(DX) = dx;
1.1.1.3   root     11695:                SREG(DS) = ds;
                   11696:                i386_load_segment_descriptor(DS);
1.1       root     11697:        } else {
                   11698:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11699:                m_CF = 1;
1.1       root     11700:        }
                   11701: }
                   11702: 
                   11703: inline void msdos_int_21h_5bh()
                   11704: {
1.1.1.45  root     11705:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11706:        
1.1.1.45  root     11707: //     if(msdos_is_existing_file(path)) {
                   11708:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     11709:                // already exists
                   11710:                REG16(AX) = 0x50;
1.1.1.3   root     11711:                m_CF = 1;
1.1       root     11712:        } else {
                   11713:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11714:                
                   11715:                if(fd != -1) {
                   11716:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11717:                        REG16(AX) = fd;
                   11718:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11719:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11720:                } else {
                   11721:                        REG16(AX) = errno;
1.1.1.3   root     11722:                        m_CF = 1;
1.1       root     11723:                }
                   11724:        }
                   11725: }
                   11726: 
                   11727: inline void msdos_int_21h_5ch()
                   11728: {
                   11729:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11730:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11731:        
1.1.1.20  root     11732:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     11733:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     11734:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     11735:                        UINT32 pos = _tell(fd);
                   11736:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   11737:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     11738:                                REG16(AX) = errno;
1.1.1.3   root     11739:                                m_CF = 1;
1.1       root     11740:                        }
1.1.1.20  root     11741:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     11742:                        
1.1       root     11743:                        // some seconds may be passed in _locking()
1.1.1.35  root     11744:                        REQUEST_HARDWRE_UPDATE();
1.1       root     11745:                } else {
                   11746:                        REG16(AX) = 0x01;
1.1.1.3   root     11747:                        m_CF = 1;
1.1       root     11748:                }
                   11749:        } else {
                   11750:                REG16(AX) = 0x06;
1.1.1.3   root     11751:                m_CF = 1;
1.1       root     11752:        }
                   11753: }
                   11754: 
1.1.1.22  root     11755: inline void msdos_int_21h_5dh()
                   11756: {
                   11757:        switch(REG8(AL)) {
1.1.1.45  root     11758:        case 0x00:
                   11759:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
                   11760:                        // current system
                   11761:                        static bool reenter = false;
                   11762:                        if(!reenter) {
                   11763:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
                   11764:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
                   11765:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
                   11766:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
                   11767:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
                   11768:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
                   11769:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
                   11770:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
                   11771:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
                   11772:                                i386_load_segment_descriptor(DS);
                   11773:                                i386_load_segment_descriptor(ES);
                   11774:                                reenter = true;
                   11775:                                try {
                   11776:                                        msdos_syscall(0x21);
                   11777:                                } catch(...) {
                   11778:                                }
                   11779:                                reenter = false;
                   11780:                        }
                   11781:                } else {
                   11782:                        REG16(AX) = 0x49; //  network software not installed
                   11783:                        m_CF = 1;
                   11784:                }
                   11785:                break;
1.1.1.22  root     11786:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     11787:                SREG(DS) = (SDA_TOP >> 4);
                   11788:                i386_load_segment_descriptor(DS);
                   11789:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   11790:                REG16(CX) = 0x80;
                   11791:                REG16(DX) = 0x1a;
                   11792:                break;
1.1.1.45  root     11793:        case 0x07: // get redirected printer mode
                   11794:        case 0x08: // set redirected printer mode
                   11795:        case 0x09: // flush redirected printer output
                   11796:                REG16(AX) = 0x49; //  network software not installed
                   11797:                m_CF = 1;
                   11798:                break;
1.1.1.43  root     11799:        case 0x0a: // set extended error information
                   11800:                {
                   11801:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   11802:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
                   11803:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   11804:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
                   11805:                        sda->byte_reserved_1        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
                   11806:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
                   11807:                        sda->word_reserved_1        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   11808:                        sda->word_reserved_2        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
                   11809:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
                   11810:                        sda->word_reserved_3        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
                   11811:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   11812:                }
                   11813:                break;
1.1.1.23  root     11814:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     11815:                REG16(AX) = 0x01;
                   11816:                m_CF = 1;
                   11817:                break;
                   11818:        default:
                   11819:                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));
                   11820:                REG16(AX) = 0x01;
                   11821:                m_CF = 1;
                   11822:                break;
                   11823:        }
                   11824: }
                   11825: 
1.1.1.42  root     11826: inline void msdos_int_21h_5eh()
                   11827: {
                   11828:        switch(REG8(AL)) {
                   11829:        case 0x00:
                   11830:                {
                   11831:                        char name[256] = {0};
                   11832:                        DWORD dwSize = 256;
                   11833:                        
                   11834:                        if(GetComputerName(name, &dwSize)) {
                   11835:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11836:                                for(int i = 0; i < 15; i++) {
                   11837:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   11838:                                }
                   11839:                                dest[15] = '\0';
                   11840:                                REG8(CH) = 0x01; // nonzero valid
                   11841:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   11842:                        } else {
                   11843:                                REG16(AX) = 0x01;
                   11844:                                m_CF = 1;
                   11845:                        }
                   11846:                }
                   11847:                break;
                   11848:        default:
1.1.1.45  root     11849: //             unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   11850: //             REG16(AX) = 0x01;
                   11851:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     11852:                m_CF = 1;
                   11853:                break;
                   11854:        }
                   11855: }
                   11856: 
1.1.1.30  root     11857: inline void msdos_int_21h_5fh()
                   11858: {
                   11859:        switch(REG8(AL)) {
1.1.1.42  root     11860:        case 0x05:
1.1.1.44  root     11861:                REG16(BP) = 0;
                   11862:                for(int i = 0; i < 26; i++) {
                   11863:                        if(msdos_is_remote_drive(i)) {
                   11864:                                REG16(BP)++;
1.1.1.42  root     11865:                        }
                   11866:                }
1.1.1.30  root     11867:        case 0x02:
1.1.1.44  root     11868:                for(int i = 0, index = 0; i < 26; i++) {
                   11869:                        if(msdos_is_remote_drive(i)) {
                   11870:                                if(index == REG16(BX)) {
                   11871:                                        char volume[] = "A:";
1.1.1.30  root     11872:                                        volume[0] = 'A' + i;
1.1.1.44  root     11873:                                        DWORD dwSize = 128;
                   11874:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   11875:                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   11876:                                        REG8(BH) = 0x00; // valid
                   11877:                                        REG8(BL) = 0x04; // disk drive
                   11878:                                        REG16(CX) = 0x00; // LANtastic
                   11879:                                        return;
1.1.1.30  root     11880:                                }
1.1.1.44  root     11881:                                index++;
1.1.1.30  root     11882:                        }
                   11883:                }
                   11884:                REG16(AX) = 0x12; // no more files
                   11885:                m_CF = 1;
                   11886:                break;
1.1.1.44  root     11887:        case 0x07:
                   11888:                if(msdos_is_valid_drive(REG8(DL))) {
                   11889:                        msdos_cds_update(REG8(DL));
                   11890:                } else {
                   11891:                        REG16(AX) = 0x0f; // invalid drive
                   11892:                        m_CF = 1;
                   11893:                }
                   11894:                break;
                   11895:        case 0x08:
                   11896:                if(msdos_is_valid_drive(REG8(DL))) {
                   11897:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   11898:                        cds->drive_attrib = 0x0000;
                   11899:                } else {
                   11900:                        REG16(AX) = 0x0f; // invalid drive
                   11901:                        m_CF = 1;
                   11902:                }
                   11903:                break;
1.1.1.30  root     11904:        default:
1.1.1.45  root     11905: //             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));
                   11906: //             REG16(AX) = 0x01;
                   11907:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     11908:                m_CF = 1;
                   11909:                break;
                   11910:        }
                   11911: }
                   11912: 
1.1       root     11913: inline void msdos_int_21h_60h(int lfn)
                   11914: {
1.1.1.45  root     11915:        char full[MAX_PATH];
                   11916:        const char *path = NULL;
1.1.1.14  root     11917:        
1.1       root     11918:        if(lfn) {
1.1.1.14  root     11919:                char *name;
                   11920:                *full = '\0';
1.1.1.3   root     11921:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     11922:                switch(REG8(CL)) {
                   11923:                case 1:
                   11924:                        GetShortPathName(full, full, MAX_PATH);
                   11925:                        my_strupr(full);
                   11926:                        break;
                   11927:                case 2:
                   11928:                        GetLongPathName(full, full, MAX_PATH);
                   11929:                        break;
                   11930:                }
                   11931:                path = full;
                   11932:        } else {
                   11933:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   11934:        }
                   11935:        if(*path != '\0') {
                   11936:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     11937:        } else {
1.1.1.14  root     11938:                REG16(AX) = (UINT16)GetLastError();
                   11939:                m_CF = 1;
1.1       root     11940:        }
                   11941: }
                   11942: 
                   11943: inline void msdos_int_21h_61h()
                   11944: {
                   11945:        REG8(AL) = 0;
                   11946: }
                   11947: 
                   11948: inline void msdos_int_21h_62h()
                   11949: {
                   11950:        REG16(BX) = current_psp;
                   11951: }
                   11952: 
                   11953: inline void msdos_int_21h_63h()
                   11954: {
                   11955:        switch(REG8(AL)) {
                   11956:        case 0x00:
1.1.1.3   root     11957:                SREG(DS) = (DBCS_TABLE >> 4);
                   11958:                i386_load_segment_descriptor(DS);
1.1       root     11959:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   11960:                REG8(AL) = 0x00;
                   11961:                break;
1.1.1.22  root     11962:        case 0x01: // set korean input mode
                   11963:        case 0x02: // get korean input mode
                   11964:                REG8(AL) = 0xff; // not supported
                   11965:                break;
1.1       root     11966:        default:
1.1.1.22  root     11967:                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     11968:                REG16(AX) = 0x01;
1.1.1.3   root     11969:                m_CF = 1;
1.1       root     11970:                break;
                   11971:        }
                   11972: }
                   11973: 
1.1.1.25  root     11974: UINT16 get_extended_country_info(UINT8 func)
1.1       root     11975: {
1.1.1.25  root     11976:        switch(func) {
1.1.1.17  root     11977:        case 0x01:
                   11978:                if(REG16(CX) >= 5) {
1.1.1.19  root     11979:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     11980:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   11981:                                REG16(CX) = sizeof(data);
                   11982:                        ZeroMemory(data, sizeof(data));
                   11983:                        data[0] = 0x01;
                   11984:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     11985:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     11986:                        *(UINT16 *)(data + 5) = active_code_page;
                   11987:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     11988: //                     REG16(AX) = active_code_page;
1.1.1.17  root     11989:                } else {
1.1.1.25  root     11990:                        return(0x08); // insufficient memory
1.1.1.17  root     11991:                }
                   11992:                break;
                   11993:        case 0x02:
                   11994:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   11995:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   11996:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     11997: //             REG16(AX) = active_code_page;
1.1.1.17  root     11998:                REG16(CX) = 0x05;
                   11999:                break;
1.1.1.23  root     12000:        case 0x03:
                   12001:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12002:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   12003:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     12004: //             REG16(AX) = active_code_page;
1.1.1.23  root     12005:                REG16(CX) = 0x05;
                   12006:                break;
1.1.1.17  root     12007:        case 0x04:
                   12008:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   12009:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   12010:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     12011: //             REG16(AX) = active_code_page;
1.1.1.17  root     12012:                REG16(CX) = 0x05;
                   12013:                break;
                   12014:        case 0x05:
                   12015:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   12016:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   12017:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     12018: //             REG16(AX) = active_code_page;
1.1.1.17  root     12019:                REG16(CX) = 0x05;
                   12020:                break;
                   12021:        case 0x06:
                   12022:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   12023:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   12024:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     12025: //             REG16(AX) = active_code_page;
1.1.1.17  root     12026:                REG16(CX) = 0x05;
                   12027:                break;
1.1       root     12028:        case 0x07:
1.1.1.3   root     12029:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   12030:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   12031:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     12032: //             REG16(AX) = active_code_page;
1.1       root     12033:                REG16(CX) = 0x05;
                   12034:                break;
1.1.1.25  root     12035:        default:
                   12036:                return(0x01); // function number invalid
                   12037:        }
                   12038:        return(0x00);
                   12039: }
                   12040: 
                   12041: inline void msdos_int_21h_65h()
                   12042: {
                   12043:        char tmp[0x10000];
                   12044:        
                   12045:        switch(REG8(AL)) {
1.1.1.43  root     12046:        case 0x00:
                   12047:                if(REG16(CX) >= 7) {
                   12048:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   12049:                        REG16(AX) = system_code_page;
                   12050:                } else {
                   12051:                        REG16(AX) = 0x0c;
                   12052:                        m_CF = 1;
                   12053:                }
                   12054:                break;
1.1.1.25  root     12055:        case 0x01:
                   12056:        case 0x02:
                   12057:        case 0x03:
                   12058:        case 0x04:
                   12059:        case 0x05:
                   12060:        case 0x06:
                   12061:        case 0x07:
                   12062:                {
                   12063:                        UINT16 result = get_extended_country_info(REG8(AL));
                   12064:                        if(result) {
                   12065:                                REG16(AX) = result;
                   12066:                                m_CF = 1;
                   12067:                        } else {
                   12068:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   12069:                        }
                   12070:                }
                   12071:                break;
1.1       root     12072:        case 0x20:
1.1.1.25  root     12073:        case 0xa0:
1.1.1.19  root     12074:                memset(tmp, 0, sizeof(tmp));
                   12075:                tmp[0] = REG8(DL);
1.1       root     12076:                my_strupr(tmp);
                   12077:                REG8(DL) = tmp[0];
                   12078:                break;
                   12079:        case 0x21:
1.1.1.25  root     12080:        case 0xa1:
1.1       root     12081:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12082:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     12083:                my_strupr(tmp);
1.1.1.3   root     12084:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     12085:                break;
                   12086:        case 0x22:
1.1.1.25  root     12087:        case 0xa2:
1.1.1.3   root     12088:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     12089:                break;
1.1.1.25  root     12090:        case 0x23:
                   12091:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45  root     12092:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     12093:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45  root     12094:                        REG16(AX) = 0x00;
                   12095:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   12096:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     12097:                        REG16(AX) = 0x01;
                   12098:                } else {
                   12099:                        REG16(AX) = 0x02;
                   12100:                }
                   12101:                break;
1.1       root     12102:        default:
1.1.1.22  root     12103:                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     12104:                REG16(AX) = 0x01;
1.1.1.3   root     12105:                m_CF = 1;
1.1       root     12106:                break;
                   12107:        }
                   12108: }
                   12109: 
                   12110: inline void msdos_int_21h_66h()
                   12111: {
                   12112:        switch(REG8(AL)) {
                   12113:        case 0x01:
                   12114:                REG16(BX) = active_code_page;
                   12115:                REG16(DX) = system_code_page;
                   12116:                break;
                   12117:        case 0x02:
                   12118:                if(active_code_page == REG16(BX)) {
                   12119:                        REG16(AX) = 0xeb41;
                   12120:                } else if(_setmbcp(REG16(BX)) == 0) {
                   12121:                        active_code_page = REG16(BX);
1.1.1.17  root     12122:                        msdos_nls_tables_update();
1.1       root     12123:                        REG16(AX) = 0xeb41;
1.1.1.32  root     12124:                        SetConsoleCP(active_code_page);
                   12125:                        SetConsoleOutputCP(active_code_page);
1.1       root     12126:                } else {
                   12127:                        REG16(AX) = 0x25;
1.1.1.3   root     12128:                        m_CF = 1;
1.1       root     12129:                }
                   12130:                break;
                   12131:        default:
1.1.1.22  root     12132:                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     12133:                REG16(AX) = 0x01;
1.1.1.3   root     12134:                m_CF = 1;
1.1       root     12135:                break;
                   12136:        }
                   12137: }
                   12138: 
                   12139: inline void msdos_int_21h_67h()
                   12140: {
                   12141:        process_t *process = msdos_process_info_get(current_psp);
                   12142:        
                   12143:        if(REG16(BX) <= MAX_FILES) {
                   12144:                process->max_files = max(REG16(BX), 20);
                   12145:        } else {
                   12146:                REG16(AX) = 0x08;
1.1.1.3   root     12147:                m_CF = 1;
1.1       root     12148:        }
                   12149: }
                   12150: 
                   12151: inline void msdos_int_21h_68h()
                   12152: {
                   12153:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12154:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12155:        
1.1.1.20  root     12156:        if(fd < process->max_files && file_handler[fd].valid) {
                   12157:                // fflush(_fdopen(fd, ""));
1.1       root     12158:        } else {
                   12159:                REG16(AX) = 0x06;
1.1.1.3   root     12160:                m_CF = 1;
1.1       root     12161:        }
                   12162: }
                   12163: 
                   12164: inline void msdos_int_21h_69h()
                   12165: {
1.1.1.3   root     12166:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12167:        char path[] = "A:\\";
                   12168:        char volume_label[MAX_PATH];
                   12169:        DWORD serial_number = 0;
                   12170:        char file_system[MAX_PATH];
                   12171:        
                   12172:        if(REG8(BL) == 0) {
                   12173:                path[0] = 'A' + _getdrive() - 1;
                   12174:        } else {
                   12175:                path[0] = 'A' + REG8(BL) - 1;
                   12176:        }
                   12177:        
                   12178:        switch(REG8(AL)) {
                   12179:        case 0x00:
                   12180:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   12181:                        info->info_level = 0;
                   12182:                        info->serial_number = serial_number;
                   12183:                        memset(info->volume_label, 0x20, 11);
                   12184:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   12185:                        memset(info->file_system, 0x20, 8);
                   12186:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   12187:                } else {
                   12188:                        REG16(AX) = errno;
1.1.1.3   root     12189:                        m_CF = 1;
1.1       root     12190:                }
                   12191:                break;
                   12192:        case 0x01:
                   12193:                REG16(AX) = 0x03;
1.1.1.3   root     12194:                m_CF = 1;
1.1.1.45  root     12195:                break;
                   12196:        default:
                   12197:                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));
                   12198:                REG16(AX) = 0x01;
                   12199:                m_CF = 1;
                   12200:                break;
1.1       root     12201:        }
                   12202: }
                   12203: 
                   12204: inline void msdos_int_21h_6ah()
                   12205: {
                   12206:        REG8(AH) = 0x68;
                   12207:        msdos_int_21h_68h();
                   12208: }
                   12209: 
                   12210: inline void msdos_int_21h_6bh()
                   12211: {
1.1.1.45  root     12212:        REG8(AL) = 0x00;
1.1       root     12213: }
                   12214: 
                   12215: inline void msdos_int_21h_6ch(int lfn)
                   12216: {
1.1.1.45  root     12217:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     12218:        int mode = REG8(BL) & 0x03;
                   12219:        
                   12220:        if(mode < 0x03) {
1.1.1.29  root     12221:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12222:                        // file exists
                   12223:                        if(REG8(DL) & 1) {
1.1.1.37  root     12224:                                int fd = -1;
                   12225:                                int sio_port = 0;
                   12226:                                int lpt_port = 0;
1.1       root     12227:                                
1.1.1.45  root     12228:                                if(msdos_is_device_path(path)) {
                   12229:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     12230:                                } else {
1.1.1.13  root     12231:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     12232:                                }
1.1       root     12233:                                if(fd != -1) {
                   12234:                                        REG16(AX) = fd;
                   12235:                                        REG16(CX) = 1;
1.1.1.45  root     12236:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12237:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12238:                                } else {
                   12239:                                        REG16(AX) = errno;
1.1.1.3   root     12240:                                        m_CF = 1;
1.1       root     12241:                                }
                   12242:                        } else if(REG8(DL) & 2) {
                   12243:                                int attr = GetFileAttributes(path);
1.1.1.37  root     12244:                                int fd = -1;
                   12245:                                int sio_port = 0;
                   12246:                                int lpt_port = 0;
1.1       root     12247:                                
1.1.1.45  root     12248:                                if(msdos_is_device_path(path)) {
                   12249:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     12250:                                } else {
                   12251:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12252:                                }
                   12253:                                if(fd != -1) {
                   12254:                                        if(attr == -1) {
                   12255:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   12256:                                        }
                   12257:                                        SetFileAttributes(path, attr);
                   12258:                                        REG16(AX) = fd;
                   12259:                                        REG16(CX) = 3;
1.1.1.45  root     12260:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12261:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12262:                                } else {
                   12263:                                        REG16(AX) = errno;
1.1.1.3   root     12264:                                        m_CF = 1;
1.1       root     12265:                                }
                   12266:                        } else {
                   12267:                                REG16(AX) = 0x50;
1.1.1.3   root     12268:                                m_CF = 1;
1.1       root     12269:                        }
                   12270:                } else {
                   12271:                        // file not exists
                   12272:                        if(REG8(DL) & 0x10) {
                   12273:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12274:                                
                   12275:                                if(fd != -1) {
                   12276:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12277:                                        REG16(AX) = fd;
                   12278:                                        REG16(CX) = 2;
                   12279:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12280:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12281:                                } else {
                   12282:                                        REG16(AX) = errno;
1.1.1.3   root     12283:                                        m_CF = 1;
1.1       root     12284:                                }
                   12285:                        } else {
                   12286:                                REG16(AX) = 0x02;
1.1.1.3   root     12287:                                m_CF = 1;
1.1       root     12288:                        }
                   12289:                }
                   12290:        } else {
                   12291:                REG16(AX) = 0x0c;
1.1.1.3   root     12292:                m_CF = 1;
1.1       root     12293:        }
                   12294: }
                   12295: 
1.1.1.43  root     12296: inline void msdos_int_21h_70h()
                   12297: {
                   12298:        switch(REG8(AL)) {
                   12299:        case 0x02:
                   12300:                if(REG16(CX) >= 7) {
                   12301:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   12302:                        msdos_nls_tables_update();
                   12303:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   12304:                        REG16(AX) = system_code_page;
                   12305:                } else {
                   12306:                        REG16(AX) = 0x0c;
                   12307:                        m_CF = 1;
                   12308:                }
                   12309:                break;
                   12310:        default:
                   12311:                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));
                   12312:                REG16(AX) = 0x01;
                   12313:                m_CF = 1;
                   12314:                break;
                   12315:        }
                   12316: }
                   12317: 
1.1       root     12318: inline void msdos_int_21h_710dh()
                   12319: {
                   12320:        // reset drive
                   12321: }
                   12322: 
1.1.1.17  root     12323: inline void msdos_int_21h_7141h(int lfn)
                   12324: {
                   12325:        if(REG16(SI) == 0) {
                   12326:                msdos_int_21h_41h(lfn);
                   12327:                return;
                   12328:        }
                   12329:        if(REG16(SI) != 1) {
                   12330:                REG16(AX) = 5;
                   12331:                m_CF = 1;
                   12332:        }
                   12333:        /* wild card and matching attributes... */
                   12334:        char tmp[MAX_PATH * 2];
                   12335:        // copy search pathname (and quick check overrun)
                   12336:        ZeroMemory(tmp, sizeof(tmp));
                   12337:        tmp[MAX_PATH - 1] = '\0';
                   12338:        tmp[MAX_PATH] = 1;
                   12339:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   12340:        
                   12341:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   12342:                REG16(AX) = 1;
                   12343:                m_CF = 1;
                   12344:                return;
                   12345:        }
                   12346:        for(char *s = tmp; *s; ++s) {
                   12347:                if(*s == '/') {
                   12348:                        *s = '\\';
                   12349:                }
                   12350:        }
                   12351:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   12352:        if(tmp_name) {
                   12353:                ++tmp_name;
                   12354:        } else {
                   12355:                tmp_name = strchr(tmp, ':');
                   12356:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   12357:        }
                   12358:        
                   12359:        WIN32_FIND_DATAA fd;
                   12360:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   12361:        if(fh == INVALID_HANDLE_VALUE) {
                   12362:                REG16(AX) = 2;
                   12363:                m_CF = 1;
                   12364:                return;
                   12365:        }
                   12366:        do {
                   12367:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   12368:                        strcpy(tmp_name, fd.cFileName);
                   12369:                        if(remove(msdos_trimmed_path(tmp, lfn))) {
                   12370:                                REG16(AX) = 5;
                   12371:                                m_CF = 1;
                   12372:                                break;
                   12373:                        }
                   12374:                }
                   12375:        } while(FindNextFileA(fh, &fd));
                   12376:        if(!m_CF) {
                   12377:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   12378:                        m_CF = 1;
                   12379:                        REG16(AX) = 2;
                   12380:                }
                   12381:        }
                   12382:        FindClose(fh);
                   12383: }
                   12384: 
1.1       root     12385: inline void msdos_int_21h_714eh()
                   12386: {
                   12387:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12388:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   12389:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12390:        WIN32_FIND_DATA fd;
                   12391:        
1.1.1.13  root     12392:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12393:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12394:                FindClose(dtainfo->find_handle);
                   12395:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12396:        }
                   12397:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12398:        dtainfo->allowable_mask = REG8(CL);
                   12399:        dtainfo->required_mask = REG8(CH);
                   12400:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12401:        
1.1.1.14  root     12402:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12403:                dtainfo->allowable_mask &= ~8;
1.1       root     12404:        }
1.1.1.14  root     12405:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   12406:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12407:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12408:                                FindClose(dtainfo->find_handle);
                   12409:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12410:                                break;
                   12411:                        }
                   12412:                }
                   12413:        }
1.1.1.13  root     12414:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12415:                find->attrib = fd.dwFileAttributes;
                   12416:                msdos_find_file_conv_local_time(&fd);
                   12417:                if(REG16(SI) == 0) {
                   12418:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12419:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12420:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12421:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12422:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12423:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12424:                } else {
                   12425:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12426:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12427:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12428:                }
                   12429:                find->size_hi = fd.nFileSizeHigh;
                   12430:                find->size_lo = fd.nFileSizeLow;
                   12431:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12432:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12433:                REG16(AX) = dtainfo - dtalist + 1;
                   12434:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12435:                // volume label
                   12436:                find->attrib = 8;
                   12437:                find->size_hi = find->size_lo = 0;
                   12438:                strcpy(find->full_name, process->volume_label);
                   12439:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12440:                dtainfo->allowable_mask &= ~8;
                   12441:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     12442:        } else {
                   12443:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12444:                m_CF = 1;
1.1       root     12445:        }
                   12446: }
                   12447: 
                   12448: inline void msdos_int_21h_714fh()
                   12449: {
                   12450:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12451:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     12452:        WIN32_FIND_DATA fd;
                   12453:        
1.1.1.14  root     12454:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12455:                REG16(AX) = 6;
1.1.1.13  root     12456:                m_CF = 1;
                   12457:                return;
                   12458:        }
1.1.1.14  root     12459:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12460:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12461:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     12462:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12463:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12464:                                        FindClose(dtainfo->find_handle);
                   12465:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12466:                                        break;
                   12467:                                }
                   12468:                        }
                   12469:                } else {
1.1.1.13  root     12470:                        FindClose(dtainfo->find_handle);
                   12471:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12472:                }
                   12473:        }
1.1.1.13  root     12474:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12475:                find->attrib = fd.dwFileAttributes;
                   12476:                msdos_find_file_conv_local_time(&fd);
                   12477:                if(REG16(SI) == 0) {
                   12478:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12479:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12480:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12481:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12482:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12483:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12484:                } else {
                   12485:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12486:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12487:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12488:                }
                   12489:                find->size_hi = fd.nFileSizeHigh;
                   12490:                find->size_lo = fd.nFileSizeLow;
                   12491:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12492:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12493:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12494:                // volume label
                   12495:                find->attrib = 8;
                   12496:                find->size_hi = find->size_lo = 0;
                   12497:                strcpy(find->full_name, process->volume_label);
                   12498:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12499:                dtainfo->allowable_mask &= ~8;
1.1       root     12500:        } else {
                   12501:                REG16(AX) = 0x12;
1.1.1.3   root     12502:                m_CF = 1;
1.1       root     12503:        }
                   12504: }
                   12505: 
                   12506: inline void msdos_int_21h_71a0h()
                   12507: {
                   12508:        DWORD max_component_len, file_sys_flag;
                   12509:        
1.1.1.14  root     12510:        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))) {
                   12511:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   12512:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     12513:                REG16(CX) = (UINT16)max_component_len;          // 255
                   12514:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   12515:        } else {
                   12516:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12517:                m_CF = 1;
1.1       root     12518:        }
                   12519: }
                   12520: 
                   12521: inline void msdos_int_21h_71a1h()
                   12522: {
1.1.1.14  root     12523:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12524:                REG16(AX) = 6;
1.1.1.13  root     12525:                m_CF = 1;
                   12526:                return;
                   12527:        }
1.1.1.14  root     12528:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12529:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12530:                FindClose(dtainfo->find_handle);
                   12531:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12532:        }
                   12533: }
                   12534: 
                   12535: inline void msdos_int_21h_71a6h()
                   12536: {
                   12537:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12538:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   12539:        
1.1.1.3   root     12540:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12541:        struct _stat64 status;
                   12542:        DWORD serial_number = 0;
                   12543:        
1.1.1.20  root     12544:        if(fd < process->max_files && file_handler[fd].valid) {
                   12545:                if(_fstat64(fd, &status) == 0) {
                   12546:                        if(file_handler[fd].path[1] == ':') {
1.1       root     12547:                                // NOTE: we need to consider the network file path "\\host\share\"
                   12548:                                char volume[] = "A:\\";
1.1.1.20  root     12549:                                volume[0] = file_handler[fd].path[1];
1.1       root     12550:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   12551:                        }
1.1.1.20  root     12552:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     12553:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   12554:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   12555:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   12556:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   12557:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   12558:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   12559:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   12560:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   12561:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   12562:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     12563:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     12564:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     12565:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     12566:                } else {
                   12567:                        REG16(AX) = errno;
1.1.1.3   root     12568:                        m_CF = 1;
1.1       root     12569:                }
                   12570:        } else {
                   12571:                REG16(AX) = 0x06;
1.1.1.3   root     12572:                m_CF = 1;
1.1       root     12573:        }
                   12574: }
                   12575: 
                   12576: inline void msdos_int_21h_71a7h()
                   12577: {
                   12578:        switch(REG8(BL)) {
                   12579:        case 0x00:
1.1.1.3   root     12580:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     12581:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12582:                        m_CF = 1;
1.1       root     12583:                }
                   12584:                break;
                   12585:        case 0x01:
                   12586:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     12587:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     12588:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12589:                        m_CF = 1;
1.1       root     12590:                }
                   12591:                break;
                   12592:        default:
1.1.1.22  root     12593:                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     12594:                REG16(AX) = 0x01;
1.1.1.3   root     12595:                m_CF = 1;
1.1       root     12596:                break;
                   12597:        }
                   12598: }
                   12599: 
                   12600: inline void msdos_int_21h_71a8h()
                   12601: {
                   12602:        if(REG8(DH) == 0) {
                   12603:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     12604:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     12605:                memset(fcb, 0x20, sizeof(fcb));
                   12606:                int len = strlen(tmp);
1.1.1.21  root     12607:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     12608:                        if(tmp[i] == '.') {
                   12609:                                pos = 8;
                   12610:                        } else {
                   12611:                                if(msdos_lead_byte_check(tmp[i])) {
                   12612:                                        fcb[pos++] = tmp[i++];
                   12613:                                }
                   12614:                                fcb[pos++] = tmp[i];
                   12615:                        }
                   12616:                }
1.1.1.3   root     12617:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     12618:        } else {
1.1.1.3   root     12619:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     12620:        }
                   12621: }
                   12622: 
1.1.1.22  root     12623: inline void msdos_int_21h_71aah()
                   12624: {
                   12625:        char drv[] = "A:", path[MAX_PATH];
                   12626:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12627:        
                   12628:        if(REG8(BL) == 0) {
                   12629:                drv[0] = 'A' + _getdrive() - 1;
                   12630:        } else {
                   12631:                drv[0] = 'A' + REG8(BL) - 1;
                   12632:        }
                   12633:        switch(REG8(BH)) {
                   12634:        case 0x00:
1.1.1.44  root     12635:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12636:                        REG16(AX) = 0x0f; // invalid drive
                   12637:                        m_CF = 1;
                   12638:                } else if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   12639:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     12640:                        m_CF = 1;
                   12641:                }
                   12642:                break;
                   12643:        case 0x01:
1.1.1.44  root     12644:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12645:                        REG16(AX) = 0x0f; // invalid drive
                   12646:                        m_CF = 1;
                   12647:                } else if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     12648:                        REG16(AX) = 0x0f; // invalid drive
                   12649:                        m_CF = 1;
                   12650:                }
                   12651:                break;
                   12652:        case 0x02:
1.1.1.44  root     12653:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12654:                        REG16(AX) = 0x0f; // invalid drive
                   12655:                        m_CF = 1;
                   12656:                } else if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     12657:                        REG16(AX) = 0x0f; // invalid drive
                   12658:                        m_CF = 1;
                   12659:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   12660:                        REG16(AX) = 0x0f; // invalid drive
                   12661:                        m_CF = 1;
                   12662:                } else {
                   12663:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   12664:                }
                   12665:                break;
                   12666:        default:
                   12667:                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));
                   12668:                REG16(AX) = 0x01;
                   12669:                m_CF = 1;
                   12670:                break;
                   12671:        }
                   12672: }
                   12673: 
1.1.1.14  root     12674: inline void msdos_int_21h_7300h()
                   12675: {
1.1.1.44  root     12676:        REG8(AL) = REG8(CL);
                   12677:        REG8(AH) = 0;
1.1.1.14  root     12678: }
                   12679: 
                   12680: inline void msdos_int_21h_7302h()
                   12681: {
                   12682:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   12683:        UINT16 seg, ofs;
                   12684:        
                   12685:        if(REG16(CX) < 0x3f) {
                   12686:                REG8(AL) = 0x18;
                   12687:                m_CF = 1;
                   12688:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   12689:                REG8(AL) = 0xff;
                   12690:                m_CF = 1;
                   12691:        } else {
                   12692:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   12693:        }
                   12694: }
                   12695: 
1.1       root     12696: inline void msdos_int_21h_7303h()
                   12697: {
1.1.1.3   root     12698:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12699:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     12700:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   12701:        
                   12702:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   12703:                info->size_of_structure = sizeof(ext_space_info_t);
                   12704:                info->structure_version = 0;
                   12705:                info->sectors_per_cluster = sectors_per_cluster;
                   12706:                info->bytes_per_sector = bytes_per_sector;
                   12707:                info->available_clusters_on_drive = free_clusters;
                   12708:                info->total_clusters_on_drive = total_clusters;
                   12709:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   12710:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   12711:                info->available_allocation_units = free_clusters;       // ???
                   12712:                info->total_allocation_units = total_clusters;          // ???
                   12713:        } else {
                   12714:                REG16(AX) = errno;
1.1.1.3   root     12715:                m_CF = 1;
1.1       root     12716:        }
                   12717: }
                   12718: 
1.1.1.30  root     12719: inline void msdos_int_21h_dbh()
                   12720: {
                   12721:        // Novell NetWare - Workstation - Get Number of Local Drives
                   12722:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   12723:        REG8(AL) = dos_info->last_drive;
                   12724: }
                   12725: 
                   12726: inline void msdos_int_21h_dch()
                   12727: {
                   12728:        // Novell NetWare - Connection Services - Get Connection Number
                   12729:        REG8(AL) = 0x00;
                   12730: }
                   12731: 
1.1.1.32  root     12732: inline void msdos_int_24h()
                   12733: {
                   12734:        const char *message = NULL;
                   12735:        int key = 0;
                   12736:        
                   12737:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   12738:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   12739:                        if(active_code_page == 932) {
                   12740:                                message = critical_error_table[i].message_japanese;
                   12741:                        }
                   12742:                        if(message == NULL) {
                   12743:                                message = critical_error_table[i].message_english;
                   12744:                        }
                   12745:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   12746:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   12747:                        
                   12748:                        SREG(ES) = WORK_TOP >> 4;
                   12749:                        i386_load_segment_descriptor(ES);
                   12750:                        REG16(DI) = 0x0000;
                   12751:                        break;
                   12752:                }
                   12753:        }
                   12754:        fprintf(stderr, "\n%s", message);
                   12755:        if(!(REG8(AH) & 0x80)) {
                   12756:                if(REG8(AH) & 0x01) {
                   12757:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   12758:                } else {
                   12759:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   12760:                }
                   12761:        }
                   12762:        fprintf(stderr, "\n");
                   12763:        
1.1.1.33  root     12764:        {
1.1.1.32  root     12765:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     12766:        }
1.1.1.32  root     12767:        if(REG8(AH) & 0x10) {
                   12768:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   12769:        }
                   12770:        if(REG8(AH) & 0x20) {
                   12771:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   12772:        }
                   12773:        if(REG8(AH) & 0x08) {
                   12774:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   12775:        }
                   12776:        fprintf(stderr, "? ");
                   12777:        
                   12778:        while(1) {
                   12779:                while(!_kbhit()) {
                   12780:                        Sleep(10);
                   12781:                }
                   12782:                key = _getch();
                   12783:                
                   12784:                if(key == 'I' || key == 'i') {
                   12785:                        if(REG8(AH) & 0x20) {
                   12786:                                REG8(AL) = 0;
                   12787:                                break;
                   12788:                        }
                   12789:                } else if(key == 'R' || key == 'r') {
                   12790:                        if(REG8(AH) & 0x10) {
                   12791:                                REG8(AL) = 1;
                   12792:                                break;
                   12793:                        }
                   12794:                } else if(key == 'A' || key == 'a') {
                   12795:                        REG8(AL) = 2;
                   12796:                        break;
                   12797:                } else if(key == 'F' || key == 'f') {
                   12798:                        if(REG8(AH) & 0x08) {
                   12799:                                REG8(AL) = 3;
                   12800:                                break;
                   12801:                        }
                   12802:                }
                   12803:        }
                   12804:        fprintf(stderr, "%c\n", key);
                   12805: }
                   12806: 
1.1       root     12807: inline void msdos_int_25h()
                   12808: {
                   12809:        UINT16 seg, ofs;
                   12810:        DWORD dwSize;
                   12811:        
1.1.1.3   root     12812: #if defined(HAS_I386)
                   12813:        I386OP(pushf)();
                   12814: #else
                   12815:        PREFIX86(_pushf());
                   12816: #endif
1.1       root     12817:        
                   12818:        if(!(REG8(AL) < 26)) {
                   12819:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     12820:                m_CF = 1;
1.1       root     12821:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   12822:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12823:                m_CF = 1;
1.1       root     12824:        } else {
                   12825:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   12826:                char dev[64];
                   12827:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   12828:                
                   12829:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   12830:                if(hFile == INVALID_HANDLE_VALUE) {
                   12831:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12832:                        m_CF = 1;
1.1       root     12833:                } else {
1.1.1.19  root     12834:                        UINT32 top_sector  = REG16(DX);
                   12835:                        UINT16 sector_num  = REG16(CX);
                   12836:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   12837:                        
                   12838:                        if(sector_num == 0xffff) {
                   12839:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   12840:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   12841:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   12842:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   12843:                                buffer_addr = (seg << 4) + ofs;
                   12844:                        }
                   12845: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   12846: //                             REG8(AL) = 0x02; // drive not ready
                   12847: //                             m_CF = 1;
                   12848: //                     } else 
                   12849:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     12850:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     12851:                                m_CF = 1;
1.1.1.19  root     12852:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     12853:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     12854:                                m_CF = 1;
1.1       root     12855:                        }
                   12856:                        CloseHandle(hFile);
                   12857:                }
                   12858:        }
                   12859: }
                   12860: 
                   12861: inline void msdos_int_26h()
                   12862: {
1.1.1.42  root     12863:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     12864:        UINT16 seg, ofs;
                   12865:        DWORD dwSize;
                   12866:        
1.1.1.3   root     12867: #if defined(HAS_I386)
                   12868:        I386OP(pushf)();
                   12869: #else
                   12870:        PREFIX86(_pushf());
                   12871: #endif
1.1       root     12872:        
                   12873:        if(!(REG8(AL) < 26)) {
                   12874:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     12875:                m_CF = 1;
1.1       root     12876:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   12877:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12878:                m_CF = 1;
1.1       root     12879:        } else {
                   12880:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   12881:                char dev[64];
                   12882:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   12883:                
                   12884:                if(dpb->media_type == 0xf8) {
                   12885:                        // this drive is not a floppy
1.1.1.6   root     12886: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   12887: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   12888: //                     }
1.1       root     12889:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12890:                        m_CF = 1;
1.1       root     12891:                } else {
                   12892:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   12893:                        if(hFile == INVALID_HANDLE_VALUE) {
                   12894:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12895:                                m_CF = 1;
1.1       root     12896:                        } else {
1.1.1.19  root     12897:                                UINT32 top_sector  = REG16(DX);
                   12898:                                UINT16 sector_num  = REG16(CX);
                   12899:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   12900:                                
                   12901:                                if(sector_num == 0xffff) {
                   12902:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   12903:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   12904:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   12905:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   12906:                                        buffer_addr = (seg << 4) + ofs;
                   12907:                                }
1.1       root     12908:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   12909:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12910:                                        m_CF = 1;
1.1.1.19  root     12911:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     12912:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     12913:                                        m_CF = 1;
1.1.1.19  root     12914:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     12915:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     12916:                                        m_CF = 1;
1.1       root     12917:                                }
                   12918:                                CloseHandle(hFile);
                   12919:                        }
                   12920:                }
                   12921:        }
                   12922: }
                   12923: 
                   12924: inline void msdos_int_27h()
                   12925: {
1.1.1.29  root     12926:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   12927:        try {
                   12928:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   12929:        } catch(...) {
                   12930:                // recover the broken mcb
                   12931:                int mcb_seg = SREG(CS) - 1;
                   12932:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     12933:                
1.1.1.29  root     12934:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     12935:                        mcb->mz = 'M';
                   12936:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   12937:                        
1.1.1.29  root     12938:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     12939:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     12940:                        } else {
1.1.1.39  root     12941:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     12942:                        }
                   12943:                } else {
                   12944:                        mcb->mz = 'Z';
1.1.1.30  root     12945:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     12946:                }
                   12947:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   12948:        }
1.1.1.3   root     12949:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     12950: }
                   12951: 
                   12952: inline void msdos_int_29h()
                   12953: {
1.1.1.14  root     12954: #if 1
                   12955:        // need to check escape sequences
1.1       root     12956:        msdos_putch(REG8(AL));
1.1.1.14  root     12957: #else
                   12958:        DWORD num;
                   12959:        vram_flush();
1.1.1.23  root     12960:        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     12961:        cursor_moved = true;
                   12962: #endif
1.1       root     12963: }
                   12964: 
                   12965: inline void msdos_int_2eh()
                   12966: {
                   12967:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   12968:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12969:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     12970:        char *token = my_strtok(tmp, " ");
                   12971:        strcpy(command, token);
                   12972:        strcpy(opt, token + strlen(token) + 1);
                   12973:        
                   12974:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   12975:        param->env_seg = 0;
                   12976:        param->cmd_line.w.l = 44;
                   12977:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   12978:        param->fcb1.w.l = 24;
                   12979:        param->fcb1.w.h = (WORK_TOP >> 4);
                   12980:        param->fcb2.w.l = 24;
                   12981:        param->fcb2.w.h = (WORK_TOP >> 4);
                   12982:        
                   12983:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   12984:        
                   12985:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   12986:        cmd_line->len = strlen(opt);
                   12987:        strcpy(cmd_line->cmd, opt);
                   12988:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   12989:        
1.1.1.28  root     12990:        try {
                   12991:                if(msdos_process_exec(command, param, 0)) {
                   12992:                        REG16(AX) = 0xffff; // error before processing command
                   12993:                } else {
                   12994:                        // set flag to set retval to ax when the started process is terminated
                   12995:                        process_t *process = msdos_process_info_get(current_psp);
                   12996:                        process->called_by_int2eh = true;
                   12997:                }
                   12998:        } catch(...) {
                   12999:                REG16(AX) = 0xffff; // error before processing command
                   13000:        }
1.1       root     13001: }
                   13002: 
1.1.1.29  root     13003: inline void msdos_int_2fh_05h()
                   13004: {
                   13005:        switch(REG8(AL)) {
                   13006:        case 0x00:
1.1.1.32  root     13007:                REG8(AL) = 0xff;
                   13008:                break;
                   13009:        case 0x01:
                   13010:        case 0x02:
                   13011:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   13012:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   13013:                                const char *message = NULL;
                   13014:                                if(active_code_page == 932) {
                   13015:                                        message = standard_error_table[i].message_japanese;
                   13016:                                }
                   13017:                                if(message == NULL) {
                   13018:                                        message = standard_error_table[i].message_english;
                   13019:                                }
                   13020:                                strcpy((char *)(mem + WORK_TOP), message);
                   13021:                                
                   13022:                                SREG(ES) = WORK_TOP >> 4;
                   13023:                                i386_load_segment_descriptor(ES);
                   13024:                                REG16(DI) = 0x0000;
                   13025:                                REG8(AL) = 0x01;
                   13026:                                break;
                   13027:                        }
                   13028:                }
1.1.1.29  root     13029:                break;
                   13030:        default:
                   13031:                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));
                   13032:                m_CF = 1;
                   13033:        }
                   13034: }
                   13035: 
1.1.1.44  root     13036: inline void msdos_int_2fh_06h()
                   13037: {
                   13038:        switch(REG8(AL)) {
                   13039:        case 0x00:
                   13040:                // ASSIGN is not installed
                   13041:                break;
                   13042:        case 0x01:
                   13043:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   13044:                REG16(AX) = 0x01;
                   13045:                m_CF = 1;
                   13046:                break;
                   13047:        default:
                   13048:                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));
                   13049:                REG16(AX) = 0x01;
                   13050:                m_CF = 1;
                   13051:                break;
                   13052:        }
                   13053: }
                   13054: 
1.1.1.22  root     13055: inline void msdos_int_2fh_11h()
                   13056: {
                   13057:        switch(REG8(AL)) {
                   13058:        case 0x00:
1.1.1.29  root     13059:                if(i386_read_stack() == 0xdada) {
                   13060:                        // MSCDEX is not installed
                   13061: //                     REG8(AL) = 0x00;
                   13062:                } else {
                   13063:                        // Network Redirector is not installed
                   13064: //                     REG8(AL) = 0x00;
                   13065:                }
1.1.1.22  root     13066:                break;
                   13067:        default:
1.1.1.43  root     13068: //             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     13069:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     13070:                m_CF = 1;
                   13071:                break;
                   13072:        }
                   13073: }
                   13074: 
1.1.1.21  root     13075: inline void msdos_int_2fh_12h()
                   13076: {
                   13077:        switch(REG8(AL)) {
1.1.1.22  root     13078:        case 0x00:
1.1.1.29  root     13079:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     13080:                REG8(AL) = 0xff;
                   13081:                break;
1.1.1.29  root     13082: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   13083:        case 0x02:
                   13084:                {
                   13085:                        UINT16 stack = i386_read_stack();
                   13086:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   13087:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   13088:                        i386_load_segment_descriptor(ES);
                   13089:                }
                   13090:                break;
1.1.1.30  root     13091:        case 0x03:
                   13092:                SREG(DS) = (DEVICE_TOP >> 4);
                   13093:                i386_load_segment_descriptor(DS);
                   13094:                break;
1.1.1.29  root     13095:        case 0x04:
                   13096:                {
                   13097:                        UINT16 stack = i386_read_stack();
                   13098:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   13099: #if defined(HAS_I386)
                   13100:                        m_ZF = (REG8(AL) == '\\');
                   13101: #else
                   13102:                        m_ZeroVal = (REG8(AL) != '\\');
                   13103: #endif
                   13104:                }
                   13105:                break;
                   13106:        case 0x05:
                   13107:                msdos_putch(i386_read_stack());
                   13108:                break;
                   13109: //     case 0x06: // DOS 3.0+ internal - Invole Critical Error
                   13110: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   13111: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
                   13112: //     case 0x09: // DOS 3.0+ internal - Flush and FREE Disk Buffer
                   13113: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   13114: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   13115: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   13116:        case 0x0d:
                   13117:                {
                   13118:                        SYSTEMTIME time;
                   13119:                        FILETIME file_time;
                   13120:                        WORD dos_date, dos_time;
                   13121:                        GetLocalTime(&time);
                   13122:                        SystemTimeToFileTime(&time, &file_time);
                   13123:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   13124:                        REG16(AX) = dos_date;
                   13125:                        REG16(DX) = dos_time;
                   13126:                }
                   13127:                break;
                   13128: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   13129: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   13130: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   13131:        case 0x11:
                   13132:                {
                   13133:                        char path[MAX_PATH], *p;
                   13134:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13135:                        my_strupr(path);
                   13136:                        while((p = my_strchr(path, '/')) != NULL) {
                   13137:                                *p = '\\';
                   13138:                        }
                   13139:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   13140:                }
                   13141:                break;
                   13142:        case 0x12:
                   13143:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13144:                break;
                   13145:        case 0x13:
                   13146:                {
                   13147:                        char tmp[2] = {0};
                   13148:                        tmp[0] = i386_read_stack();
                   13149:                        my_strupr(tmp);
                   13150:                        REG8(AL) = tmp[0];
                   13151:                }
                   13152:                break;
                   13153:        case 0x14:
                   13154: #if defined(HAS_I386)
                   13155:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   13156: #else
                   13157:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13158: #endif
                   13159:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13160:                break;
                   13161: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     13162:        case 0x16:
                   13163:                if(REG16(BX) < 20) {
                   13164:                        SREG(ES) = SFT_TOP >> 4;
                   13165:                        i386_load_segment_descriptor(ES);
                   13166:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   13167:                        
                   13168:                        // update system file table
                   13169:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   13170:                        if(file_handler[REG16(BX)].valid) {
                   13171:                                int count = 0;
                   13172:                                for(int i = 0; i < 20; i++) {
                   13173:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   13174:                                                count++;
                   13175:                                        }
                   13176:                                }
                   13177:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   13178:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   13179:                                _lseek(REG16(BX), 0, SEEK_END);
                   13180:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   13181:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   13182:                        } else {
                   13183:                                memset(sft, 0, 0x3b);
                   13184:                        }
                   13185:                } else {
                   13186:                        REG16(AX) = 0x06;
                   13187:                        m_CF = 1;
                   13188:                }
                   13189:                break;
1.1.1.29  root     13190: //     case 0x17: // DOS 3.0+ internal - Get Current Directory Structure for Drive
                   13191: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   13192: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   13193:        case 0x1a:
                   13194:                {
                   13195:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   13196:                        if(path[1] == ':') {
                   13197:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   13198:                                        REG8(AL) = path[0] - 'a' + 1;
                   13199:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   13200:                                        REG8(AL) = path[0] - 'A' + 1;
                   13201:                                } else {
                   13202:                                        REG8(AL) = 0xff; // invalid
                   13203:                                }
                   13204:                                strcpy(full, path);
                   13205:                                strcpy(path, full + 2);
                   13206:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   13207:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   13208:                                        REG8(AL) = full[0] - 'a' + 1;
                   13209:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   13210:                                        REG8(AL) = full[0] - 'A' + 1;
                   13211:                                } else {
                   13212:                                        REG8(AL) = 0xff; // invalid
                   13213:                                }
                   13214:                        } else {
                   13215:                                REG8(AL) = 0x00; // default
                   13216:                        }
                   13217:                }
                   13218:                break;
                   13219:        case 0x1b:
                   13220:                {
                   13221:                        int year = REG16(CX) + 1980;
                   13222:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   13223:                }
                   13224:                break;
                   13225: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   13226: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   13227:        case 0x1e:
                   13228:                {
                   13229:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   13230:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   13231:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   13232: #if defined(HAS_I386)
                   13233:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   13234: #else
                   13235:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   13236: #endif
                   13237:                        } else {
                   13238: #if defined(HAS_I386)
                   13239:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   13240: #else
                   13241:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   13242: #endif
                   13243:                        }
                   13244:                }
                   13245:                break;
                   13246: //     case 0x1f: // DOS 3.0+ internal - Build Current Directory Structure
1.1.1.21  root     13247:        case 0x20:
                   13248:                {
                   13249:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13250:                        
                   13251:                        if(fd < 20) {
                   13252:                                SREG(ES) = current_psp;
                   13253:                                i386_load_segment_descriptor(ES);
                   13254:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   13255:                        } else {
                   13256:                                REG16(AX) = 0x06;
                   13257:                                m_CF = 1;
                   13258:                        }
                   13259:                }
                   13260:                break;
1.1.1.29  root     13261:        case 0x21:
                   13262:                msdos_int_21h_60h(0);
                   13263:                break;
                   13264: //     case 0x22: // DOS 3.0+ internal - Set Extended Error Info
                   13265: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   13266: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   13267:        case 0x25:
                   13268:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13269:                break;
                   13270:        case 0x26:
                   13271:                REG8(AL) = REG8(CL);
                   13272:                msdos_int_21h_3dh();
                   13273:                break;
                   13274:        case 0x27:
                   13275:                msdos_int_21h_3eh();
                   13276:                break;
                   13277:        case 0x28:
                   13278:                REG16(AX) = REG16(BP);
                   13279:                msdos_int_21h_42h();
                   13280:                break;
                   13281:        case 0x29:
                   13282:                msdos_int_21h_3fh();
                   13283:                break;
                   13284: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   13285:        case 0x2b:
                   13286:                REG16(AX) = REG16(BP);
                   13287:                msdos_int_21h_44h();
                   13288:                break;
                   13289:        case 0x2c:
                   13290:                REG16(BX) = DEVICE_TOP >> 4;
                   13291:                REG16(AX) = 22;
                   13292:                break;
                   13293:        case 0x2d:
                   13294:                {
                   13295:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13296:                        REG16(AX) = sda->extended_error_code;
                   13297:                }
                   13298:                break;
                   13299:        case 0x2e:
                   13300:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     13301:                        SREG(ES) = 0x0001;
                   13302:                        i386_load_segment_descriptor(ES);
                   13303:                        REG16(DI) = 0x00;
                   13304:                } else if(REG8(DL) == 0x08) {
                   13305:                        // dummy parameter error message read routine is at fffd:0010
                   13306:                        SREG(ES) = 0xfffd;
1.1.1.22  root     13307:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     13308:                        REG16(DI) = 0x0010;
1.1.1.22  root     13309:                }
                   13310:                break;
1.1.1.29  root     13311:        case 0x2f:
                   13312:                if(REG16(DX) != 0) {
1.1.1.30  root     13313:                        dos_major_version = REG8(DL);
                   13314:                        dos_minor_version = REG8(DH);
1.1.1.29  root     13315:                } else {
                   13316:                        REG8(DL) = 7;
                   13317:                        REG8(DH) = 10;
                   13318:                }
                   13319:                break;
                   13320: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   13321: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     13322:        default:
                   13323:                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));
                   13324:                REG16(AX) = 0x01;
                   13325:                m_CF = 1;
                   13326:                break;
                   13327:        }
                   13328: }
                   13329: 
1.1.1.30  root     13330: inline void msdos_int_2fh_13h()
                   13331: {
                   13332:        static UINT16 prevDS = 0, prevDX = 0;
                   13333:        static UINT16 prevES = 0, prevBX = 0;
                   13334:        UINT16 tmp;
                   13335:        
                   13336:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   13337:        i386_load_segment_descriptor(DS);
                   13338:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   13339:        
                   13340:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   13341:        i386_load_segment_descriptor(ES);
                   13342:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   13343: }
                   13344: 
1.1.1.22  root     13345: inline void msdos_int_2fh_14h()
                   13346: {
                   13347:        switch(REG8(AL)) {
                   13348:        case 0x00:
1.1.1.29  root     13349:                // NLSFUNC.COM is installed
                   13350:                REG8(AL) = 0xff;
1.1.1.25  root     13351:                break;
                   13352:        case 0x01:
                   13353:        case 0x03:
                   13354:                REG8(AL) = 0x00;
                   13355:                active_code_page = REG16(BX);
                   13356:                msdos_nls_tables_update();
                   13357:                break;
                   13358:        case 0x02:
                   13359:                REG8(AL) = get_extended_country_info(REG16(BP));
                   13360:                break;
                   13361:        case 0x04:
1.1.1.42  root     13362:                for(int i = 0;; i++) {
                   13363:                        if(country_table[i].code == REG16(DX)) {
                   13364:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   13365:                                break;
                   13366:                        } else if(country_table[i].code == -1) {
                   13367:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13368:                                break;
                   13369:                        }
                   13370:                }
1.1.1.25  root     13371:                REG8(AL) = 0x00;
1.1.1.22  root     13372:                break;
                   13373:        default:
                   13374:                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));
                   13375:                REG16(AX) = 0x01;
                   13376:                m_CF = 1;
                   13377:                break;
                   13378:        }
                   13379: }
                   13380: 
                   13381: inline void msdos_int_2fh_15h()
                   13382: {
                   13383:        switch(REG8(AL)) {
1.1.1.29  root     13384:        case 0x00: // CD-ROM - Installation Check
                   13385:                if(REG16(BX) == 0x0000) {
1.1.1.43  root     13386: #if 0
                   13387:                        // MSCDEX is installed
                   13388:                        REG16(BX) = 0;
                   13389:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     13390:                                if(msdos_is_cdrom_drive(i)) {
                   13391:                                        if(REG16(BX) == 0) {
                   13392:                                                REG16(CX) = i;
1.1.1.43  root     13393:                                        }
1.1.1.44  root     13394:                                        REG16(BX)++;
1.1.1.43  root     13395:                                }
                   13396:                        }
                   13397: #else
1.1.1.29  root     13398:                        // MSCDEX is not installed
                   13399: //                     REG8(AL) = 0x00;
1.1.1.43  root     13400: #endif
1.1.1.29  root     13401:                } else {
                   13402:                        // GRAPHICS.COM is not installed
                   13403: //                     REG8(AL) = 0x00;
                   13404:                }
1.1.1.22  root     13405:                break;
1.1.1.43  root     13406:        case 0x0b:
1.1.1.44  root     13407:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   13408:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   13409:                REG16(BX) = 0xadad;
1.1.1.43  root     13410:                break;
                   13411:        case 0x0d:
1.1.1.44  root     13412:                for(int i = 0, n = 0; i < 26; i++) {
                   13413:                        if(msdos_is_cdrom_drive(i)) {
                   13414:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     13415:                        }
                   13416:                }
                   13417:                break;
1.1.1.22  root     13418:        case 0xff:
1.1.1.29  root     13419:                if(REG16(BX) == 0x0000) {
                   13420:                        // CORELCDX is not installed
                   13421:                } else {
                   13422:                        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));
                   13423:                        REG16(AX) = 0x01;
                   13424:                        m_CF = 1;
                   13425:                }
1.1.1.22  root     13426:                break;
1.1.1.21  root     13427:        default:
1.1.1.22  root     13428:                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     13429:                REG16(AX) = 0x01;
                   13430:                m_CF = 1;
                   13431:                break;
                   13432:        }
                   13433: }
                   13434: 
1.1       root     13435: inline void msdos_int_2fh_16h()
                   13436: {
                   13437:        switch(REG8(AL)) {
                   13438:        case 0x00:
1.1.1.14  root     13439:                if(no_windows) {
1.1.1.29  root     13440:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   13441: //                     REG8(AL) = 0x00;
1.1.1.14  root     13442:                } else {
1.1.1.30  root     13443:                        REG8(AL) = win_major_version;
                   13444:                        REG8(AH) = win_minor_version;
1.1       root     13445:                }
                   13446:                break;
1.1.1.43  root     13447:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     13448:                // from DOSBox
                   13449:                i386_set_a20_line(1);
                   13450:                break;
1.1.1.43  root     13451:        case 0x06: // Windows Enhanced Mode & 286 DOSX exit Broadcast
                   13452:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   13453:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   13454:                break;
                   13455:        case 0x07:
                   13456:                // Virtual Device Call API
                   13457:                break;
1.1.1.22  root     13458:        case 0x0a:
                   13459:                if(!no_windows) {
                   13460:                        REG16(AX) = 0x0000;
1.1.1.30  root     13461:                        REG8(BH) = win_major_version;
                   13462:                        REG8(BL) = win_minor_version;
1.1.1.22  root     13463:                        REG16(CX) = 0x0003; // enhanced
                   13464:                }
                   13465:                break;
1.1.1.30  root     13466:        case 0x0b:
                   13467:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     13468:        case 0x0e:
                   13469:        case 0x0f:
1.1.1.30  root     13470:        case 0x10:
1.1.1.22  root     13471:        case 0x11:
                   13472:        case 0x12:
                   13473:        case 0x13:
                   13474:        case 0x14:
1.1.1.30  root     13475:        case 0x15:
1.1.1.43  root     13476:        case 0x81:
                   13477:        case 0x82:
1.1.1.44  root     13478:        case 0x84:
1.1.1.33  root     13479:        case 0x86:
1.1.1.22  root     13480:        case 0x87:
1.1.1.30  root     13481:        case 0x89:
1.1.1.33  root     13482:        case 0x8a:
1.1.1.22  root     13483:                // function not supported, do not clear AX
                   13484:                break;
1.1.1.14  root     13485:        case 0x80:
                   13486:                Sleep(10);
1.1.1.35  root     13487:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     13488:                REG8(AL) = 0x00;
1.1.1.14  root     13489:                break;
1.1.1.33  root     13490:        case 0x83:
                   13491:                REG16(BX) = 0x01; // system vm id
                   13492:                break;
1.1.1.22  root     13493:        case 0x8e:
                   13494:                REG16(AX) = 0x00; // failed
                   13495:                break;
1.1.1.20  root     13496:        case 0x8f:
                   13497:                switch(REG8(DH)) {
                   13498:                case 0x00:
                   13499:                case 0x02:
                   13500:                case 0x03:
                   13501:                        REG16(AX) = 0x00;
                   13502:                        break;
                   13503:                case 0x01:
                   13504:                        REG16(AX) = 0x168f;
                   13505:                        break;
                   13506:                }
                   13507:                break;
1.1       root     13508:        default:
1.1.1.22  root     13509:                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));
                   13510:                REG16(AX) = 0x01;
                   13511:                m_CF = 1;
                   13512:                break;
                   13513:        }
                   13514: }
                   13515: 
                   13516: inline void msdos_int_2fh_19h()
                   13517: {
                   13518:        switch(REG8(AL)) {
                   13519:        case 0x00:
1.1.1.29  root     13520:                // SHELLB.COM is not installed
                   13521: //             REG8(AL) = 0x00;
1.1.1.22  root     13522:                break;
                   13523:        case 0x01:
                   13524:        case 0x02:
                   13525:        case 0x03:
                   13526:        case 0x04:
                   13527:                REG16(AX) = 0x01;
                   13528:                m_CF = 1;
                   13529:                break;
1.1.1.29  root     13530:        case 0x80:
                   13531:                // IBM ROM-DOS v4.0 is not installed
                   13532: //             REG8(AL) = 0x00;
                   13533:                break;
1.1.1.22  root     13534:        default:
                   13535:                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     13536:                REG16(AX) = 0x01;
1.1.1.3   root     13537:                m_CF = 1;
1.1       root     13538:                break;
                   13539:        }
                   13540: }
                   13541: 
                   13542: inline void msdos_int_2fh_1ah()
                   13543: {
                   13544:        switch(REG8(AL)) {
                   13545:        case 0x00:
1.1.1.29  root     13546:                // ANSI.SYS is installed
1.1       root     13547:                REG8(AL) = 0xff;
                   13548:                break;
                   13549:        default:
1.1.1.22  root     13550:                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));
                   13551:                REG16(AX) = 0x01;
                   13552:                m_CF = 1;
                   13553:                break;
                   13554:        }
                   13555: }
                   13556: 
1.1.1.30  root     13557: inline void msdos_int_2fh_40h()
1.1.1.22  root     13558: {
                   13559:        switch(REG8(AL)) {
                   13560:        case 0x00:
1.1.1.30  root     13561:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   13562:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     13563:                break;
1.1.1.43  root     13564:        case 0x10:
                   13565:                // OS/2 v2.0+ - Installation Check
                   13566:                REG16(AX) = 0x01;
                   13567:                m_CF = 1;
                   13568:                break;
1.1.1.22  root     13569:        default:
                   13570:                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     13571:                REG16(AX) = 0x01;
1.1.1.3   root     13572:                m_CF = 1;
1.1       root     13573:                break;
                   13574:        }
                   13575: }
                   13576: 
                   13577: inline void msdos_int_2fh_43h()
                   13578: {
                   13579:        switch(REG8(AL)) {
                   13580:        case 0x00:
1.1.1.29  root     13581:                // XMS is installed ?
1.1.1.19  root     13582: #ifdef SUPPORT_XMS
                   13583:                if(support_xms) {
                   13584:                        REG8(AL) = 0x80;
1.1.1.44  root     13585:                }
                   13586: #endif
                   13587:                break;
                   13588:        case 0x08:
                   13589: #ifdef SUPPORT_XMS
                   13590:                if(support_xms) {
                   13591:                        REG8(AL) = 0x43;
                   13592:                        REG8(BL) = 0x01; // IBM PC/AT
                   13593:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   13594:                }
1.1.1.19  root     13595: #endif
                   13596:                break;
                   13597:        case 0x10:
                   13598:                SREG(ES) = XMS_TOP >> 4;
                   13599:                i386_load_segment_descriptor(ES);
1.1.1.26  root     13600:                REG16(BX) = 0x15;
1.1       root     13601:                break;
1.1.1.44  root     13602:        case 0xe0:
                   13603:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   13604:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   13605:                        break;
                   13606:                }
1.1       root     13607:        default:
1.1.1.22  root     13608:                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));
                   13609:                REG16(AX) = 0x01;
                   13610:                m_CF = 1;
                   13611:                break;
                   13612:        }
                   13613: }
                   13614: 
                   13615: inline void msdos_int_2fh_46h()
                   13616: {
                   13617:        switch(REG8(AL)) {
                   13618:        case 0x80:
1.1.1.29  root     13619:                // Windows v3.0 is not installed
                   13620: //             REG8(AL) = 0x00;
1.1.1.22  root     13621:                break;
                   13622:        default:
                   13623:                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));
                   13624:                REG16(AX) = 0x01;
                   13625:                m_CF = 1;
                   13626:                break;
                   13627:        }
                   13628: }
                   13629: 
                   13630: inline void msdos_int_2fh_48h()
                   13631: {
                   13632:        switch(REG8(AL)) {
                   13633:        case 0x00:
1.1.1.29  root     13634:                // DOSKEY is not installed
                   13635: //             REG8(AL) = 0x00;
1.1.1.22  root     13636:                break;
                   13637:        case 0x10:
                   13638:                msdos_int_21h_0ah();
                   13639:                REG16(AX) = 0x00;
                   13640:                break;
                   13641:        default:
                   13642:                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     13643:                REG16(AX) = 0x01;
1.1.1.3   root     13644:                m_CF = 1;
1.1       root     13645:                break;
                   13646:        }
                   13647: }
                   13648: 
                   13649: inline void msdos_int_2fh_4ah()
                   13650: {
                   13651:        switch(REG8(AL)) {
1.1.1.29  root     13652: #ifdef SUPPORT_HMA
                   13653:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   13654:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   13655:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13656:                                // restore first free mcb in high memory area
                   13657:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13658:                        }
                   13659:                        int offset = 0xffff;
                   13660:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   13661:                                REG16(DI) = offset + 0x10;
                   13662:                        } else {
                   13663:                                REG16(DI) = 0xffff;
                   13664:                        }
                   13665:                } else {
                   13666:                        // HMA is already used
                   13667:                        REG16(BX) = 0;
                   13668:                        REG16(DI) = 0xffff;
                   13669:                }
                   13670:                SREG(ES) = 0xffff;
                   13671:                i386_load_segment_descriptor(ES);
                   13672:                break;
                   13673:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   13674:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   13675:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13676:                                // restore first free mcb in high memory area
                   13677:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13678:                        }
                   13679:                        int size = REG16(BX), offset;
                   13680:                        if((size % 16) != 0) {
                   13681:                                size &= ~15;
                   13682:                                size += 16;
                   13683:                        }
                   13684:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   13685:                                REG16(BX) = size;
                   13686:                                REG16(DI) = offset + 0x10;
                   13687:                                is_hma_used_by_int_2fh = true;
                   13688:                        } else {
                   13689:                                REG16(BX) = 0;
                   13690:                                REG16(DI) = 0xffff;
                   13691:                        }
                   13692:                } else {
                   13693:                        // HMA is already used
                   13694:                        REG16(BX) = 0;
                   13695:                        REG16(DI) = 0xffff;
                   13696:                }
                   13697:                SREG(ES) = 0xffff;
                   13698:                i386_load_segment_descriptor(ES);
                   13699:                break;
                   13700:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   13701:                if(REG8(DL) == 0x00) {
                   13702:                        if(!is_hma_used_by_xms) {
                   13703:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13704:                                        // restore first free mcb in high memory area
                   13705:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13706:                                        is_hma_used_by_int_2fh = false;
                   13707:                                }
                   13708:                                int size = REG16(BX), offset;
                   13709:                                if((size % 16) != 0) {
                   13710:                                        size &= ~15;
                   13711:                                        size += 16;
                   13712:                                }
                   13713:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   13714: //                                     REG16(BX) = size;
                   13715:                                        SREG(ES) = 0xffff;
                   13716:                                        i386_load_segment_descriptor(ES);
                   13717:                                        REG16(DI) = offset + 0x10;
                   13718:                                        is_hma_used_by_int_2fh = true;
                   13719:                                } else {
                   13720:                                        REG16(DI) = 0xffff;
                   13721:                                }
                   13722:                        } else {
                   13723:                                REG16(DI) = 0xffff;
                   13724:                        }
                   13725:                } else if(REG8(DL) == 0x01) {
                   13726:                        if(!is_hma_used_by_xms) {
                   13727:                                int size = REG16(BX);
                   13728:                                if((size % 16) != 0) {
                   13729:                                        size &= ~15;
                   13730:                                        size += 16;
                   13731:                                }
                   13732:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   13733:                                        // memory block address is not changed
                   13734:                                } else {
                   13735:                                        REG16(DI) = 0xffff;
                   13736:                                }
                   13737:                        } else {
                   13738:                                REG16(DI) = 0xffff;
                   13739:                        }
                   13740:                } else if(REG8(DL) == 0x02) {
                   13741:                        if(!is_hma_used_by_xms) {
                   13742:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13743:                                        // restore first free mcb in high memory area
                   13744:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13745:                                        is_hma_used_by_int_2fh = false;
                   13746:                                } else {
                   13747:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   13748:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   13749:                                                is_hma_used_by_int_2fh = false;
                   13750:                                        }
                   13751:                                }
                   13752:                        }
                   13753:                } else {
                   13754:                        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));
                   13755:                        REG16(AX) = 0x01;
                   13756:                        m_CF = 1;
                   13757:                }
                   13758:                break;
                   13759:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   13760:                if(!is_hma_used_by_xms) {
                   13761:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13762:                                // restore first free mcb in high memory area
                   13763:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13764:                                is_hma_used_by_int_2fh = false;
                   13765:                        }
                   13766:                        REG16(AX) = 0x0000;
                   13767:                        SREG(ES) = 0xffff;
                   13768:                        i386_load_segment_descriptor(ES);
                   13769:                        REG16(DI) = 0x10;
                   13770:                }
                   13771:                break;
                   13772: #else
1.1       root     13773:        case 0x01:
                   13774:        case 0x02:
1.1.1.29  root     13775:                // HMA is already used
1.1.1.27  root     13776:                REG16(BX) = 0x0000;
1.1.1.3   root     13777:                SREG(ES) = 0xffff;
                   13778:                i386_load_segment_descriptor(ES);
1.1       root     13779:                REG16(DI) = 0xffff;
                   13780:                break;
1.1.1.19  root     13781:        case 0x03:
                   13782:                // unable to allocate
                   13783:                REG16(DI) = 0xffff;
                   13784:                break;
                   13785:        case 0x04:
                   13786:                // function not supported, do not clear AX
                   13787:                break;
1.1.1.29  root     13788: #endif
                   13789:        case 0x10:
1.1.1.42  root     13790:                switch(REG16(BX)) {
                   13791:                case 0x0000:
                   13792:                case 0x0001:
                   13793:                case 0x0002:
                   13794:                case 0x0003:
                   13795:                case 0x0004:
                   13796:                case 0x0005:
                   13797:                case 0x0006:
                   13798:                case 0x0007:
                   13799:                case 0x0008:
                   13800:                case 0x000a:
                   13801:                case 0x1234:
                   13802:                        // SMARTDRV v4.00+ is not installed
                   13803:                        break;
                   13804:                default:
1.1.1.29  root     13805:                        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));
                   13806:                        REG16(AX) = 0x01;
                   13807:                        m_CF = 1;
1.1.1.42  root     13808:                        break;
1.1.1.29  root     13809:                }
                   13810:                break;
                   13811:        case 0x11:
1.1.1.42  root     13812:                switch(REG16(BX)) {
                   13813:                case 0x0000:
                   13814:                case 0x0001:
                   13815:                case 0x0002:
                   13816:                case 0x0003:
                   13817:                case 0x0004:
                   13818:                case 0x0005:
                   13819:                case 0x0006:
                   13820:                case 0x0007:
                   13821:                case 0x0008:
                   13822:                case 0x0009:
                   13823:                case 0x000a:
                   13824:                case 0x000b:
                   13825:                case 0xfffe:
                   13826:                case 0xffff:
1.1.1.29  root     13827:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     13828:                        break;
                   13829:                default:
                   13830:                        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));
                   13831:                        REG16(AX) = 0x01;
                   13832:                        m_CF = 1;
                   13833:                        break;
                   13834:                }
                   13835:                break;
                   13836:        case 0x12:
                   13837:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   13838:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   13839:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   13840:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     13841:                } else {
                   13842:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   13843:                        REG16(AX) = 0x01;
                   13844:                        m_CF = 1;
                   13845:                }
1.1.1.22  root     13846:                break;
1.1.1.42  root     13847:        case 0x13:
                   13848:                // DBLSPACE.BIN is not installed
                   13849:                break;
1.1.1.22  root     13850:        default:
                   13851:                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));
                   13852:                REG16(AX) = 0x01;
                   13853:                m_CF = 1;
                   13854:                break;
                   13855:        }
                   13856: }
                   13857: 
                   13858: inline void msdos_int_2fh_4bh()
                   13859: {
                   13860:        switch(REG8(AL)) {
1.1.1.24  root     13861:        case 0x01:
1.1.1.22  root     13862:        case 0x02:
1.1.1.29  root     13863:                // Task Switcher is not installed
1.1.1.24  root     13864:                break;
                   13865:        case 0x03:
                   13866:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   13867:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     13868:                break;
1.1.1.30  root     13869:        case 0x04:
                   13870:                REG16(BX) = 0x0000; // free switcher id successfully
                   13871:                break;
1.1.1.43  root     13872:        case 0x05:
                   13873:                REG16(BX) = 0x0000; // no instance data chain
                   13874:                SREG(ES) = 0x0000;
                   13875:                i386_load_segment_descriptor(ES);
                   13876:                break;
1.1       root     13877:        default:
1.1.1.22  root     13878:                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     13879:                REG16(AX) = 0x01;
1.1.1.3   root     13880:                m_CF = 1;
1.1       root     13881:                break;
                   13882:        }
                   13883: }
                   13884: 
1.1.1.44  root     13885: inline void msdos_int_2fh_4dh()
                   13886: {
                   13887:        switch(REG8(AL)) {
                   13888:        case 0x00:
                   13889:                // KKCFUNC is not installed ???
                   13890:                break;
                   13891:        default:
                   13892: //             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));
                   13893:                REG16(AX) = 0x01; // invalid function
                   13894:                m_CF = 1;
                   13895:                break;
                   13896:        }
                   13897: }
                   13898: 
1.1       root     13899: inline void msdos_int_2fh_4fh()
                   13900: {
                   13901:        switch(REG8(AL)) {
                   13902:        case 0x00:
1.1.1.29  root     13903:                // BILING is installed
1.1.1.27  root     13904:                REG16(AX) = 0x0000;
                   13905:                REG8(DL) = 0x01;        // major version
                   13906:                REG8(DH) = 0x00;        // minor version
1.1       root     13907:                break;
                   13908:        case 0x01:
1.1.1.27  root     13909:                REG16(AX) = 0x0000;
1.1       root     13910:                REG16(BX) = active_code_page;
                   13911:                break;
                   13912:        default:
1.1.1.22  root     13913:                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));
                   13914:                REG16(AX) = 0x01;
                   13915:                m_CF = 1;
                   13916:                break;
                   13917:        }
                   13918: }
                   13919: 
                   13920: inline void msdos_int_2fh_55h()
                   13921: {
                   13922:        switch(REG8(AL)) {
                   13923:        case 0x00:
                   13924:        case 0x01:
                   13925: //             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));
                   13926:                break;
                   13927:        default:
                   13928:                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     13929:                REG16(AX) = 0x01;
1.1.1.3   root     13930:                m_CF = 1;
1.1       root     13931:                break;
                   13932:        }
                   13933: }
                   13934: 
1.1.1.44  root     13935: inline void msdos_int_2fh_56h()
                   13936: {
                   13937:        switch(REG8(AL)) {
                   13938:        case 0x00:
                   13939:                // INTERLNK is not installed
                   13940:                break;
                   13941:        case 0x01:
                   13942:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   13943: //             if(msdos_is_remote_drive(REG8(BH))) {
                   13944: //                     REG8(AL) = 0x00;
                   13945: //             }
                   13946:                break;
                   13947:        default:
                   13948:                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));
                   13949:                REG16(AX) = 0x01;
                   13950:                m_CF = 1;
                   13951:                break;
                   13952:        }
                   13953: }
                   13954: 
1.1.1.24  root     13955: inline void msdos_int_2fh_adh()
                   13956: {
                   13957:        switch(REG8(AL)) {
                   13958:        case 0x00:
1.1.1.29  root     13959:                // DISPLAY.SYS is installed
1.1.1.24  root     13960:                REG8(AL) = 0xff;
                   13961:                REG16(BX) = 0x100; // ???
                   13962:                break;
                   13963:        case 0x01:
                   13964:                active_code_page = REG16(BX);
                   13965:                msdos_nls_tables_update();
                   13966:                REG16(AX) = 0x01;
                   13967:                break;
                   13968:        case 0x02:
                   13969:                REG16(BX) = active_code_page;
                   13970:                break;
                   13971:        case 0x03:
                   13972:                // FIXME
                   13973:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   13974:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   13975:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   13976:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   13977:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   13978:                break;
                   13979:        case 0x80:
                   13980:                break; // keyb.com is not installed
                   13981:        default:
                   13982:                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));
                   13983:                REG16(AX) = 0x01;
                   13984:                m_CF = 1;
                   13985:                break;
                   13986:        }
                   13987: }
                   13988: 
1.1       root     13989: inline void msdos_int_2fh_aeh()
                   13990: {
                   13991:        switch(REG8(AL)) {
                   13992:        case 0x00:
1.1.1.28  root     13993:                // FIXME: we need to check the given command line
                   13994:                REG8(AL) = 0x00; // the command should be executed as usual
                   13995: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     13996:                break;
                   13997:        case 0x01:
                   13998:                {
                   13999:                        char command[MAX_PATH];
                   14000:                        memset(command, 0, sizeof(command));
1.1.1.3   root     14001:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     14002:                        
                   14003:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   14004:                        param->env_seg = 0;
                   14005:                        param->cmd_line.w.l = 44;
                   14006:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   14007:                        param->fcb1.w.l = 24;
                   14008:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   14009:                        param->fcb2.w.l = 24;
                   14010:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   14011:                        
                   14012:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   14013:                        
                   14014:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     14015:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   14016:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     14017:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   14018:                        
1.1.1.28  root     14019:                        try {
                   14020:                                msdos_process_exec(command, param, 0);
                   14021:                        } catch(...) {
                   14022:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     14023:                        }
                   14024:                }
                   14025:                break;
                   14026:        default:
1.1.1.22  root     14027:                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     14028:                REG16(AX) = 0x01;
1.1.1.3   root     14029:                m_CF = 1;
1.1       root     14030:                break;
                   14031:        }
                   14032: }
                   14033: 
1.1.1.34  root     14034: inline void msdos_int_2fh_b7h()
                   14035: {
                   14036:        switch(REG8(AL)) {
                   14037:        case 0x00:
                   14038:                // APPEND is not installed
                   14039: //             REG8(AL) = 0x00;
                   14040:                break;
1.1.1.44  root     14041:        case 0x06:
                   14042:                REG16(BX) = 0x0000;
                   14043:                break;
1.1.1.34  root     14044:        case 0x07:
1.1.1.43  root     14045:        case 0x11:
1.1.1.34  root     14046:                // COMMAND.COM calls this service without checking APPEND is installed
                   14047:                break;
                   14048:        default:
                   14049:                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));
                   14050:                REG16(AX) = 0x01;
                   14051:                m_CF = 1;
                   14052:                break;
                   14053:        }
                   14054: }
                   14055: 
1.1.1.24  root     14056: inline void msdos_int_33h_0000h()
                   14057: {
                   14058:        REG16(AX) = 0xffff; // hardware/driver installed
                   14059:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14060: }
                   14061: 
                   14062: inline void msdos_int_33h_0001h()
                   14063: {
1.1.1.34  root     14064:        if(mouse.hidden > 0) {
                   14065:                mouse.hidden--;
                   14066:        }
                   14067:        if(mouse.hidden == 0) {
1.1.1.24  root     14068:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   14069:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   14070:                }
                   14071:                pic[1].imr &= ~0x10;    // enable irq12
                   14072:        }
                   14073: }
                   14074: 
                   14075: inline void msdos_int_33h_0002h()
                   14076: {
1.1.1.34  root     14077:        mouse.hidden++;
                   14078: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
                   14079:        pic[1].imr |= 0x10;     // enable irq12
1.1.1.24  root     14080: }
                   14081: 
                   14082: inline void msdos_int_33h_0003h()
                   14083: {
1.1.1.34  root     14084: //     if(mouse.hidden > 0) {
                   14085:                update_console_input();
                   14086: //     }
1.1.1.24  root     14087:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     14088:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   14089:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   14090: }
                   14091: 
                   14092: inline void msdos_int_33h_0004h()
                   14093: {
                   14094:        mouse.position.x = REG16(CX);
                   14095:        mouse.position.x = REG16(DX);
1.1.1.24  root     14096: }
                   14097: 
                   14098: inline void msdos_int_33h_0005h()
                   14099: {
1.1.1.34  root     14100: //     if(mouse.hidden > 0) {
                   14101:                update_console_input();
                   14102: //     }
1.1.1.24  root     14103:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14104:                int idx = REG16(BX);
1.1.1.34  root     14105:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   14106:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   14107:                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     14108:                mouse.buttons[idx].pressed_times = 0;
                   14109:        } else {
                   14110:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14111:        }
                   14112:        REG16(AX) = mouse.get_buttons();
                   14113: }
                   14114: 
                   14115: inline void msdos_int_33h_0006h()
                   14116: {
1.1.1.34  root     14117: //     if(mouse.hidden > 0) {
                   14118:                update_console_input();
                   14119: //     }
1.1.1.24  root     14120:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14121:                int idx = REG16(BX);
1.1.1.34  root     14122:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   14123:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   14124:                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     14125:                mouse.buttons[idx].released_times = 0;
                   14126:        } else {
                   14127:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14128:        }
                   14129:        REG16(AX) = mouse.get_buttons();
                   14130: }
                   14131: 
                   14132: inline void msdos_int_33h_0007h()
                   14133: {
                   14134:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   14135:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   14136: }
                   14137: 
                   14138: inline void msdos_int_33h_0008h()
                   14139: {
                   14140:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   14141:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   14142: }
                   14143: 
                   14144: inline void msdos_int_33h_0009h()
                   14145: {
                   14146:        mouse.hot_spot[0] = REG16(BX);
                   14147:        mouse.hot_spot[1] = REG16(CX);
                   14148: }
                   14149: 
                   14150: inline void msdos_int_33h_000bh()
                   14151: {
1.1.1.34  root     14152: //     if(mouse.hidden > 0) {
                   14153:                update_console_input();
                   14154: //     }
1.1.1.24  root     14155:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14156:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   14157:        mouse.prev_position.x = mouse.position.x;
                   14158:        mouse.prev_position.y = mouse.position.y;
                   14159:        REG16(CX) = dx;
                   14160:        REG16(DX) = dy;
                   14161: }
                   14162: 
                   14163: inline void msdos_int_33h_000ch()
                   14164: {
                   14165:        mouse.call_mask = REG16(CX);
                   14166:        mouse.call_addr.w.l = REG16(DX);
                   14167:        mouse.call_addr.w.h = SREG(ES);
                   14168: }
                   14169: 
                   14170: inline void msdos_int_33h_000fh()
                   14171: {
                   14172:        mouse.mickey.x = REG16(CX);
                   14173:        mouse.mickey.y = REG16(DX);
                   14174: }
                   14175: 
                   14176: inline void msdos_int_33h_0011h()
                   14177: {
                   14178:        REG16(AX) = 0xffff;
                   14179:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14180: }
                   14181: 
                   14182: inline void msdos_int_33h_0014h()
                   14183: {
                   14184:        UINT16 old_mask = mouse.call_mask;
                   14185:        UINT16 old_ofs = mouse.call_addr.w.l;
                   14186:        UINT16 old_seg = mouse.call_addr.w.h;
                   14187:        
                   14188:        mouse.call_mask = REG16(CX);
                   14189:        mouse.call_addr.w.l = REG16(DX);
                   14190:        mouse.call_addr.w.h = SREG(ES);
                   14191:        
                   14192:        REG16(CX) = old_mask;
                   14193:        REG16(DX) = old_ofs;
                   14194:        SREG(ES) = old_seg;
                   14195:        i386_load_segment_descriptor(ES);
                   14196: }
                   14197: 
                   14198: inline void msdos_int_33h_0015h()
                   14199: {
                   14200:        REG16(BX) = sizeof(mouse);
                   14201: }
                   14202: 
                   14203: inline void msdos_int_33h_0016h()
                   14204: {
                   14205:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   14206: }
                   14207: 
                   14208: inline void msdos_int_33h_0017h()
                   14209: {
                   14210:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   14211: }
                   14212: 
1.1.1.43  root     14213: inline void msdos_int_33h_0018h()
                   14214: {
                   14215:        for(int i = 0; i < 8; i++) {
                   14216:                if(REG16(CX) & (1 << i)) {
                   14217:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   14218:                                // event handler already exists
                   14219:                                REG16(AX) = 0xffff;
                   14220:                                break;
                   14221:                        }
                   14222:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   14223:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   14224:                }
                   14225:        }
                   14226: }
                   14227: 
                   14228: inline void msdos_int_33h_0019h()
                   14229: {
                   14230:        UINT16 call_mask = REG16(CX);
                   14231:        
                   14232:        REG16(CX) = 0;
                   14233:        
                   14234:        for(int i = 0; i < 8; i++) {
                   14235:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   14236:                        for(int j = 0; j < 8; j++) {
                   14237:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   14238:                                        REG16(CX) |= (1 << j);
                   14239:                                }
                   14240:                        }
                   14241:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   14242:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   14243:                        break;
                   14244:                }
                   14245:        }
                   14246: }
                   14247: 
1.1.1.24  root     14248: inline void msdos_int_33h_001ah()
                   14249: {
                   14250:        mouse.sensitivity[0] = REG16(BX);
                   14251:        mouse.sensitivity[1] = REG16(CX);
                   14252:        mouse.sensitivity[2] = REG16(DX);
                   14253: }
                   14254: 
                   14255: inline void msdos_int_33h_001bh()
                   14256: {
                   14257:        REG16(BX) = mouse.sensitivity[0];
                   14258:        REG16(CX) = mouse.sensitivity[1];
                   14259:        REG16(DX) = mouse.sensitivity[2];
                   14260: }
                   14261: 
                   14262: inline void msdos_int_33h_001dh()
                   14263: {
                   14264:        mouse.display_page = REG16(BX);
                   14265: }
                   14266: 
                   14267: inline void msdos_int_33h_001eh()
                   14268: {
                   14269:        REG16(BX) = mouse.display_page;
                   14270: }
                   14271: 
1.1.1.34  root     14272: inline void msdos_int_33h_001fh()
                   14273: {
                   14274:        // from DOSBox
                   14275:        REG16(BX) = 0x0000;
                   14276:        SREG(ES) = 0x0000;
                   14277:        i386_load_segment_descriptor(ES);
                   14278:        mouse.enabled = false;
                   14279:        mouse.old_hidden = mouse.hidden;
                   14280:        mouse.hidden = 1;
                   14281: }
                   14282: 
                   14283: inline void msdos_int_33h_0020h()
                   14284: {
                   14285:        // from DOSBox
                   14286:        mouse.enabled = true;
                   14287:        mouse.hidden = mouse.old_hidden;
                   14288: }
                   14289: 
1.1.1.24  root     14290: inline void msdos_int_33h_0021h()
                   14291: {
                   14292:        REG16(AX) = 0xffff;
                   14293:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14294: }
                   14295: 
                   14296: inline void msdos_int_33h_0022h()
                   14297: {
                   14298:        mouse.language = REG16(BX);
                   14299: }
                   14300: 
                   14301: inline void msdos_int_33h_0023h()
                   14302: {
                   14303:        REG16(BX) = mouse.language;
                   14304: }
                   14305: 
                   14306: inline void msdos_int_33h_0024h()
                   14307: {
                   14308:        REG16(BX) = 0x0805; // V8.05
                   14309:        REG16(CX) = 0x0400; // PS/2
                   14310: }
                   14311: 
                   14312: inline void msdos_int_33h_0026h()
                   14313: {
                   14314:        REG16(BX) = 0x0000;
                   14315:        REG16(CX) = mouse.max_position.x;
                   14316:        REG16(DX) = mouse.max_position.y;
                   14317: }
                   14318: 
                   14319: inline void msdos_int_33h_002ah()
                   14320: {
1.1.1.34  root     14321:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     14322:        REG16(BX) = mouse.hot_spot[0];
                   14323:        REG16(CX) = mouse.hot_spot[1];
                   14324:        REG16(DX) = 4; // PS/2
                   14325: }
                   14326: 
                   14327: inline void msdos_int_33h_0031h()
                   14328: {
                   14329:        REG16(AX) = mouse.min_position.x;
                   14330:        REG16(BX) = mouse.min_position.y;
                   14331:        REG16(CX) = mouse.max_position.x;
                   14332:        REG16(DX) = mouse.max_position.y;
                   14333: }
                   14334: 
                   14335: inline void msdos_int_33h_0032h()
                   14336: {
                   14337:        REG16(AX) = 0;
                   14338: //     REG16(AX) |= 0x8000; // 0025h
                   14339:        REG16(AX) |= 0x4000; // 0026h
                   14340: //     REG16(AX) |= 0x2000; // 0027h
                   14341: //     REG16(AX) |= 0x1000; // 0028h
                   14342: //     REG16(AX) |= 0x0800; // 0029h
                   14343:        REG16(AX) |= 0x0400; // 002ah
                   14344: //     REG16(AX) |= 0x0200; // 002bh
                   14345: //     REG16(AX) |= 0x0100; // 002ch
                   14346: //     REG16(AX) |= 0x0080; // 002dh
                   14347: //     REG16(AX) |= 0x0040; // 002eh
                   14348:        REG16(AX) |= 0x0020; // 002fh
                   14349: //     REG16(AX) |= 0x0010; // 0030h
                   14350:        REG16(AX) |= 0x0008; // 0031h
                   14351:        REG16(AX) |= 0x0004; // 0032h
                   14352: //     REG16(AX) |= 0x0002; // 0033h
                   14353: //     REG16(AX) |= 0x0001; // 0034h
                   14354: }
                   14355: 
1.1.1.19  root     14356: inline void msdos_int_67h_40h()
                   14357: {
                   14358:        if(!support_ems) {
                   14359:                REG8(AH) = 0x84;
                   14360:        } else {
                   14361:                REG8(AH) = 0x00;
                   14362:        }
                   14363: }
                   14364: 
                   14365: inline void msdos_int_67h_41h()
                   14366: {
                   14367:        if(!support_ems) {
                   14368:                REG8(AH) = 0x84;
                   14369:        } else {
                   14370:                REG8(AH) = 0x00;
                   14371:                REG16(BX) = EMS_TOP >> 4;
                   14372:        }
                   14373: }
                   14374: 
                   14375: inline void msdos_int_67h_42h()
                   14376: {
                   14377:        if(!support_ems) {
                   14378:                REG8(AH) = 0x84;
                   14379:        } else {
                   14380:                REG8(AH) = 0x00;
                   14381:                REG16(BX) = free_ems_pages;
                   14382:                REG16(DX) = MAX_EMS_PAGES;
                   14383:        }
                   14384: }
                   14385: 
                   14386: inline void msdos_int_67h_43h()
                   14387: {
                   14388:        if(!support_ems) {
                   14389:                REG8(AH) = 0x84;
                   14390:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   14391:                REG8(AH) = 0x87;
                   14392:        } else if(REG16(BX) > free_ems_pages) {
                   14393:                REG8(AH) = 0x88;
                   14394:        } else if(REG16(BX) == 0) {
                   14395:                REG8(AH) = 0x89;
                   14396:        } else {
1.1.1.31  root     14397:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14398:                        if(!ems_handles[i].allocated) {
                   14399:                                ems_allocate_pages(i, REG16(BX));
                   14400:                                REG8(AH) = 0x00;
                   14401:                                REG16(DX) = i;
                   14402:                                return;
                   14403:                        }
                   14404:                }
                   14405:                REG8(AH) = 0x85;
                   14406:        }
                   14407: }
                   14408: 
                   14409: inline void msdos_int_67h_44h()
                   14410: {
                   14411:        if(!support_ems) {
                   14412:                REG8(AH) = 0x84;
1.1.1.31  root     14413:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14414:                REG8(AH) = 0x83;
                   14415:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   14416:                REG8(AH) = 0x8a;
                   14417: //     } else if(!(REG8(AL) < 4)) {
                   14418: //             REG8(AH) = 0x8b;
                   14419:        } else if(REG16(BX) == 0xffff) {
                   14420:                ems_unmap_page(REG8(AL) & 3);
                   14421:                REG8(AH) = 0x00;
                   14422:        } else {
                   14423:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   14424:                REG8(AH) = 0x00;
                   14425:        }
                   14426: }
                   14427: 
                   14428: inline void msdos_int_67h_45h()
                   14429: {
                   14430:        if(!support_ems) {
                   14431:                REG8(AH) = 0x84;
1.1.1.31  root     14432:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14433:                REG8(AH) = 0x83;
                   14434:        } else {
                   14435:                ems_release_pages(REG16(DX));
                   14436:                REG8(AH) = 0x00;
                   14437:        }
                   14438: }
                   14439: 
                   14440: inline void msdos_int_67h_46h()
                   14441: {
                   14442:        if(!support_ems) {
                   14443:                REG8(AH) = 0x84;
                   14444:        } else {
1.1.1.29  root     14445: //             REG16(AX) = 0x0032; // EMS 3.2
                   14446:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     14447:        }
                   14448: }
                   14449: 
                   14450: inline void msdos_int_67h_47h()
                   14451: {
                   14452:        // NOTE: the map data should be stored in the specified ems page, not process data
                   14453:        process_t *process = msdos_process_info_get(current_psp);
                   14454:        
                   14455:        if(!support_ems) {
                   14456:                REG8(AH) = 0x84;
1.1.1.31  root     14457: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14458: //             REG8(AH) = 0x83;
                   14459:        } else if(process->ems_pages_stored) {
                   14460:                REG8(AH) = 0x8d;
                   14461:        } else {
                   14462:                for(int i = 0; i < 4; i++) {
                   14463:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   14464:                        process->ems_pages[i].page   = ems_pages[i].page;
                   14465:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   14466:                }
                   14467:                process->ems_pages_stored = true;
                   14468:                REG8(AH) = 0x00;
                   14469:        }
                   14470: }
                   14471: 
                   14472: inline void msdos_int_67h_48h()
                   14473: {
                   14474:        // NOTE: the map data should be restored from the specified ems page, not process data
                   14475:        process_t *process = msdos_process_info_get(current_psp);
                   14476:        
                   14477:        if(!support_ems) {
                   14478:                REG8(AH) = 0x84;
1.1.1.31  root     14479: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14480: //             REG8(AH) = 0x83;
                   14481:        } else if(!process->ems_pages_stored) {
                   14482:                REG8(AH) = 0x8e;
                   14483:        } else {
                   14484:                for(int i = 0; i < 4; i++) {
                   14485:                        if(process->ems_pages[i].mapped) {
                   14486:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   14487:                        } else {
                   14488:                                ems_unmap_page(i);
                   14489:                        }
                   14490:                }
                   14491:                process->ems_pages_stored = false;
                   14492:                REG8(AH) = 0x00;
                   14493:        }
                   14494: }
                   14495: 
                   14496: inline void msdos_int_67h_4bh()
                   14497: {
                   14498:        if(!support_ems) {
                   14499:                REG8(AH) = 0x84;
                   14500:        } else {
                   14501:                REG8(AH) = 0x00;
                   14502:                REG16(BX) = 0;
1.1.1.31  root     14503:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14504:                        if(ems_handles[i].allocated) {
                   14505:                                REG16(BX)++;
                   14506:                        }
                   14507:                }
                   14508:        }
                   14509: }
                   14510: 
                   14511: inline void msdos_int_67h_4ch()
                   14512: {
                   14513:        if(!support_ems) {
                   14514:                REG8(AH) = 0x84;
1.1.1.31  root     14515:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14516:                REG8(AH) = 0x83;
                   14517:        } else {
                   14518:                REG8(AH) = 0x00;
                   14519:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   14520:        }
                   14521: }
                   14522: 
                   14523: inline void msdos_int_67h_4dh()
                   14524: {
                   14525:        if(!support_ems) {
                   14526:                REG8(AH) = 0x84;
                   14527:        } else {
                   14528:                REG8(AH) = 0x00;
                   14529:                REG16(BX) = 0;
1.1.1.31  root     14530:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14531:                        if(ems_handles[i].allocated) {
                   14532:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   14533:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   14534:                                REG16(BX)++;
                   14535:                        }
                   14536:                }
                   14537:        }
                   14538: }
                   14539: 
1.1.1.20  root     14540: inline void msdos_int_67h_4eh()
                   14541: {
                   14542:        if(!support_ems) {
                   14543:                REG8(AH) = 0x84;
                   14544:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14545:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   14546:                        // save page map
                   14547:                        for(int i = 0; i < 4; i++) {
                   14548:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   14549:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   14550:                        }
                   14551:                }
                   14552:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14553:                        // restore page map
                   14554:                        for(int i = 0; i < 4; i++) {
                   14555:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   14556:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   14557:                                
1.1.1.31  root     14558:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     14559:                                        ems_map_page(i, handle, page);
                   14560:                                } else {
                   14561:                                        ems_unmap_page(i);
                   14562:                                }
                   14563:                        }
                   14564:                }
                   14565:                REG8(AH) = 0x00;
                   14566:        } else if(REG8(AL) == 0x03) {
                   14567:                REG8(AH) = 0x00;
1.1.1.21  root     14568:                REG8(AL) = 4 * 4;
                   14569:        } else {
1.1.1.22  root     14570:                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     14571:                REG8(AH) = 0x8f;
                   14572:        }
                   14573: }
                   14574: 
                   14575: inline void msdos_int_67h_4fh()
                   14576: {
                   14577:        if(!support_ems) {
                   14578:                REG8(AH) = 0x84;
                   14579:        } else if(REG8(AL) == 0x00) {
                   14580:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   14581:                
                   14582:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   14583:                for(int i = 0; i < count; i++) {
                   14584:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   14585:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   14586:                        
                   14587: //                     if(!(physical < 4)) {
                   14588: //                             REG8(AH) = 0x8b;
                   14589: //                             return;
                   14590: //                     }
                   14591:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     14592:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   14593:                        *(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     14594:                }
                   14595:                REG8(AH) = 0x00;
                   14596:        } else if(REG8(AL) == 0x01) {
                   14597:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   14598:                
                   14599:                for(int i = 0; i < count; i++) {
                   14600:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   14601:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   14602:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   14603:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   14604:                        
                   14605: //                     if(!(physical < 4)) {
                   14606: //                             REG8(AH) = 0x8b;
                   14607: //                             return;
                   14608: //                     } else
1.1.1.41  root     14609:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     14610:                                ems_map_page(physical & 3, handle, logical);
                   14611:                        } else {
1.1.1.41  root     14612:                                ems_unmap_page(physical & 3);
1.1.1.21  root     14613:                        }
                   14614:                }
                   14615:                REG8(AH) = 0x00;
                   14616:        } else if(REG8(AL) == 0x02) {
                   14617:                REG8(AH) = 0x00;
                   14618:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     14619:        } else {
1.1.1.22  root     14620:                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     14621:                REG8(AH) = 0x8f;
                   14622:        }
                   14623: }
                   14624: 
                   14625: inline void msdos_int_67h_50h()
                   14626: {
                   14627:        if(!support_ems) {
                   14628:                REG8(AH) = 0x84;
1.1.1.31  root     14629:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     14630:                REG8(AH) = 0x83;
                   14631:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   14632:                for(int i = 0; i < REG16(CX); i++) {
                   14633:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   14634:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   14635:                        
                   14636:                        if(REG8(AL) == 0x01) {
                   14637:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   14638:                        }
                   14639: //                     if(!(physical < 4)) {
                   14640: //                             REG8(AH) = 0x8b;
                   14641: //                             return;
                   14642: //                     } else
                   14643:                        if(logical == 0xffff) {
                   14644:                                ems_unmap_page(physical & 3);
                   14645:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   14646:                                ems_map_page(physical & 3, REG16(DX), logical);
                   14647:                        } else {
                   14648:                                REG8(AH) = 0x8a;
                   14649:                                return;
                   14650:                        }
                   14651:                }
                   14652:                REG8(AH) = 0x00;
                   14653:        } else {
1.1.1.22  root     14654:                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     14655:                REG8(AH) = 0x8f;
                   14656:        }
                   14657: }
                   14658: 
1.1.1.19  root     14659: inline void msdos_int_67h_51h()
                   14660: {
                   14661:        if(!support_ems) {
                   14662:                REG8(AH) = 0x84;
1.1.1.31  root     14663:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14664:                REG8(AH) = 0x83;
                   14665:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   14666:                REG8(AH) = 0x87;
                   14667:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   14668:                REG8(AH) = 0x88;
                   14669:        } else {
                   14670:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   14671:                REG8(AH) = 0x00;
                   14672:        }
                   14673: }
                   14674: 
1.1.1.20  root     14675: inline void msdos_int_67h_52h()
                   14676: {
                   14677:        if(!support_ems) {
                   14678:                REG8(AH) = 0x84;
1.1.1.31  root     14679: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   14680: //             REG8(AH) = 0x83;
1.1.1.20  root     14681:        } else if(REG8(AL) == 0x00) {
                   14682:                REG8(AL) = 0x00; // handle is volatile
                   14683:                REG8(AH) = 0x00;
                   14684:        } else if(REG8(AL) == 0x01) {
                   14685:                if(REG8(BL) == 0x00) {
                   14686:                        REG8(AH) = 0x00;
                   14687:                } else {
                   14688:                        REG8(AH) = 0x90; // undefined attribute type
                   14689:                }
                   14690:        } else if(REG8(AL) == 0x02) {
                   14691:                REG8(AL) = 0x00; // only volatile handles supported
                   14692:                REG8(AH) = 0x00;
                   14693:        } else {
1.1.1.22  root     14694:                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     14695:                REG8(AH) = 0x8f;
                   14696:        }
                   14697: }
                   14698: 
1.1.1.19  root     14699: inline void msdos_int_67h_53h()
                   14700: {
                   14701:        if(!support_ems) {
                   14702:                REG8(AH) = 0x84;
1.1.1.31  root     14703:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14704:                REG8(AH) = 0x83;
                   14705:        } else if(REG8(AL) == 0x00) {
                   14706:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   14707:                REG8(AH) = 0x00;
                   14708:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     14709:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14710:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   14711:                                REG8(AH) = 0xa1;
                   14712:                                return;
                   14713:                        }
                   14714:                }
                   14715:                REG8(AH) = 0x00;
                   14716:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   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: 
                   14723: inline void msdos_int_67h_54h()
                   14724: {
                   14725:        if(!support_ems) {
                   14726:                REG8(AH) = 0x84;
                   14727:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     14728:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14729:                        if(ems_handles[i].allocated) {
                   14730:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   14731:                        } else {
                   14732:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   14733:                        }
                   14734:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   14735:                }
                   14736:                REG8(AH) = 0x00;
                   14737:                REG8(AL) = MAX_EMS_HANDLES;
                   14738:        } else if(REG8(AL) == 0x01) {
                   14739:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     14740:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14741:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   14742:                                REG8(AH) = 0x00;
                   14743:                                REG16(DX) = i;
                   14744:                                break;
                   14745:                        }
                   14746:                }
                   14747:        } else if(REG8(AL) == 0x02) {
                   14748:                REG8(AH) = 0x00;
                   14749:                REG16(BX) = MAX_EMS_HANDLES;
                   14750:        } else {
1.1.1.22  root     14751:                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     14752:                REG8(AH) = 0x8f;
                   14753:        }
                   14754: }
                   14755: 
                   14756: inline void msdos_int_67h_57h_tmp()
                   14757: {
                   14758:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   14759:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   14760:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   14761:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   14762:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   14763:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   14764:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   14765:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   14766:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   14767:        
1.1.1.32  root     14768:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     14769:        UINT32 src_addr, dest_addr;
                   14770:        UINT32 src_addr_max, dest_addr_max;
                   14771:        
                   14772:        if(src_type == 0) {
                   14773:                src_buffer = mem;
                   14774:                src_addr = (src_seg << 4) + src_ofs;
                   14775:                src_addr_max = MAX_MEM;
                   14776:        } else {
1.1.1.31  root     14777:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     14778:                        REG8(AH) = 0x83;
                   14779:                        return;
                   14780:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   14781:                        REG8(AH) = 0x8a;
                   14782:                        return;
                   14783:                }
1.1.1.32  root     14784:                if(ems_handles[src_handle].buffer != NULL) {
                   14785:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   14786:                }
1.1.1.20  root     14787:                src_addr = src_ofs;
1.1.1.32  root     14788:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     14789:        }
                   14790:        if(dest_type == 0) {
                   14791:                dest_buffer = mem;
                   14792:                dest_addr = (dest_seg << 4) + dest_ofs;
                   14793:                dest_addr_max = MAX_MEM;
                   14794:        } else {
1.1.1.31  root     14795:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     14796:                        REG8(AH) = 0x83;
                   14797:                        return;
                   14798:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   14799:                        REG8(AH) = 0x8a;
                   14800:                        return;
                   14801:                }
1.1.1.32  root     14802:                if(ems_handles[dest_handle].buffer != NULL) {
                   14803:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   14804:                }
1.1.1.20  root     14805:                dest_addr = dest_ofs;
1.1.1.32  root     14806:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     14807:        }
1.1.1.32  root     14808:        if(src_buffer != NULL && dest_buffer != NULL) {
                   14809:                for(int i = 0; i < copy_length; i++) {
                   14810:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   14811:                                if(REG8(AL) == 0x00) {
                   14812:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   14813:                                } else if(REG8(AL) == 0x01) {
                   14814:                                        UINT8 tmp = dest_buffer[dest_addr];
                   14815:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   14816:                                        src_buffer[src_addr++] = tmp;
                   14817:                                }
                   14818:                        } else {
                   14819:                                REG8(AH) = 0x93;
                   14820:                                return;
1.1.1.20  root     14821:                        }
                   14822:                }
1.1.1.32  root     14823:                REG8(AH) = 0x00;
                   14824:        } else {
                   14825:                REG8(AH) = 0x80;
1.1.1.20  root     14826:        }
                   14827: }
                   14828: 
                   14829: inline void msdos_int_67h_57h()
                   14830: {
                   14831:        if(!support_ems) {
                   14832:                REG8(AH) = 0x84;
                   14833:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   14834:                struct {
                   14835:                        UINT16 handle;
                   14836:                        UINT16 page;
                   14837:                        bool mapped;
                   14838:                } tmp_pages[4];
                   14839:                
                   14840:                // unmap pages to copy memory data to ems buffer
                   14841:                for(int i = 0; i < 4; i++) {
                   14842:                        tmp_pages[i].handle = ems_pages[i].handle;
                   14843:                        tmp_pages[i].page   = ems_pages[i].page;
                   14844:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   14845:                        ems_unmap_page(i);
                   14846:                }
                   14847:                
                   14848:                // run move/exchange operation
                   14849:                msdos_int_67h_57h_tmp();
                   14850:                
                   14851:                // restore unmapped pages
                   14852:                for(int i = 0; i < 4; i++) {
                   14853:                        if(tmp_pages[i].mapped) {
                   14854:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   14855:                        }
                   14856:                }
                   14857:        } else {
1.1.1.22  root     14858:                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     14859:                REG8(AH) = 0x8f;
                   14860:        }
                   14861: }
                   14862: 
                   14863: inline void msdos_int_67h_58h()
                   14864: {
                   14865:        if(!support_ems) {
                   14866:                REG8(AH) = 0x84;
                   14867:        } else if(REG8(AL) == 0x00) {
                   14868:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     14869:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   14870:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     14871:                }
                   14872:                REG8(AH) = 0x00;
                   14873:                REG16(CX) = 4;
                   14874:        } else if(REG8(AL) == 0x01) {
                   14875:                REG8(AH) = 0x00;
                   14876:                REG16(CX) = 4;
                   14877:        } else {
1.1.1.22  root     14878:                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     14879:                REG8(AH) = 0x8f;
                   14880:        }
                   14881: }
                   14882: 
1.1.1.42  root     14883: inline void msdos_int_67h_59h()
                   14884: {
                   14885:        if(!support_ems) {
                   14886:                REG8(AH) = 0x84;
                   14887:        } else if(REG8(AL) == 0x00) {
                   14888:                REG8(AH) = 0xa4; // access denied by operating system
                   14889:        } else if(REG8(AL) == 0x01) {
                   14890:                REG8(AH) = 0x00;
                   14891:                REG16(BX) = free_ems_pages;
                   14892:                REG16(DX) = MAX_EMS_PAGES;
                   14893:        } else {
                   14894:                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));
                   14895:                REG8(AH) = 0x8f;
                   14896:        }
                   14897: }
                   14898: 
1.1.1.20  root     14899: inline void msdos_int_67h_5ah()
                   14900: {
                   14901:        if(!support_ems) {
1.1.1.19  root     14902:                REG8(AH) = 0x84;
1.1.1.20  root     14903:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   14904:                REG8(AH) = 0x87;
                   14905:        } else if(REG16(BX) > free_ems_pages) {
                   14906:                REG8(AH) = 0x88;
                   14907: //     } else if(REG16(BX) == 0) {
                   14908: //             REG8(AH) = 0x89;
                   14909:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     14910:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     14911:                        if(!ems_handles[i].allocated) {
                   14912:                                ems_allocate_pages(i, REG16(BX));
                   14913:                                REG8(AH) = 0x00;
                   14914:                                REG16(DX) = i;
                   14915:                                return;
                   14916:                        }
                   14917:                }
                   14918:                REG8(AH) = 0x85;
                   14919:        } else {
1.1.1.22  root     14920:                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     14921:                REG8(AH) = 0x8f;
1.1.1.19  root     14922:        }
                   14923: }
                   14924: 
1.1.1.43  root     14925: inline void msdos_int_67h_5dh()
                   14926: {
                   14927:        if(!support_ems) {
                   14928:                REG8(AH) = 0x84;
                   14929:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14930:                REG8(AH) = 0xa4; // operating system denied access
                   14931:        } else {
                   14932:                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));
                   14933:                REG8(AH) = 0x8f;
                   14934:        }
                   14935: }
                   14936: 
1.1.1.30  root     14937: inline void msdos_int_67h_deh()
                   14938: {
                   14939:        REG8(AH) = 0x84;
                   14940: }
                   14941: 
1.1.1.19  root     14942: #ifdef SUPPORT_XMS
                   14943: 
1.1.1.32  root     14944: void msdos_xms_init()
1.1.1.26  root     14945: {
1.1.1.30  root     14946:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   14947:        emb_handle_top->address = EMB_TOP;
                   14948:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     14949:        xms_a20_local_enb_count = 0;
                   14950: }
                   14951: 
1.1.1.32  root     14952: void msdos_xms_finish()
                   14953: {
                   14954:        msdos_xms_release();
                   14955: }
                   14956: 
                   14957: void msdos_xms_release()
1.1.1.30  root     14958: {
                   14959:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   14960:                emb_handle_t *next_handle = emb_handle->next;
                   14961:                free(emb_handle);
                   14962:                emb_handle = next_handle;
                   14963:        }
                   14964: }
                   14965: 
                   14966: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   14967: {
                   14968:        if(handle != 0) {
                   14969:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   14970:                        if(emb_handle->handle == handle) {
                   14971:                                return(emb_handle);
                   14972:                        }
                   14973:                }
                   14974:        }
                   14975:        return(NULL);
                   14976: }
                   14977: 
                   14978: int msdos_xms_get_unused_emb_handle_id()
                   14979: {
                   14980:        for(int handle = 1;; handle++) {
                   14981:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   14982:                        return(handle);
                   14983:                }
                   14984:        }
                   14985:        return(0);
                   14986: }
                   14987: 
                   14988: int msdos_xms_get_unused_emb_handle_count()
                   14989: {
                   14990:        int count = 64; //255;
                   14991:        
                   14992:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   14993:                if(emb_handle->handle != 0) {
                   14994:                        if(--count == 1) {
                   14995:                                break;
                   14996:                        }
                   14997:                }
                   14998:        }
                   14999:        return(count);
                   15000: }
                   15001: 
                   15002: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   15003: {
                   15004:        if(emb_handle->size_kb > size_kb) {
                   15005:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   15006:                
                   15007:                new_handle->address = emb_handle->address + size_kb * 1024;
                   15008:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   15009:                emb_handle->size_kb = size_kb;
                   15010:                
                   15011:                new_handle->prev = emb_handle;
                   15012:                new_handle->next = emb_handle->next;
                   15013:                if(emb_handle->next != NULL) {
                   15014:                        emb_handle->next->prev = new_handle;
                   15015:                }
                   15016:                emb_handle->next = new_handle;
                   15017:        }
                   15018: }
                   15019: 
                   15020: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   15021: {
                   15022:        emb_handle_t *next_handle = emb_handle->next;
                   15023:        
                   15024:        if(next_handle != NULL) {
                   15025:                emb_handle->size_kb += next_handle->size_kb;
                   15026:                
                   15027:                if(next_handle->next != NULL) {
                   15028:                        next_handle->next->prev = emb_handle;
                   15029:                }
                   15030:                emb_handle->next = next_handle->next;
                   15031:                free(next_handle);
                   15032:        }
                   15033: }
                   15034: 
                   15035: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   15036: {
                   15037:        emb_handle_t *target_handle = NULL;
                   15038:        
                   15039:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15040:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   15041:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   15042:                                target_handle = emb_handle;
                   15043:                        }
                   15044:                }
                   15045:        }
                   15046:        if(target_handle != NULL) {
                   15047:                if(target_handle->size_kb > size_kb) {
                   15048:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   15049:                }
                   15050: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   15051:                return(target_handle);
                   15052:        }
                   15053:        return(NULL);
                   15054: }
                   15055: 
                   15056: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   15057: {
                   15058:        emb_handle_t *prev_handle = emb_handle->prev;
                   15059:        emb_handle_t *next_handle = emb_handle->next;
                   15060:        
                   15061:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   15062:                msdos_xms_combine_emb_handles(prev_handle);
                   15063:                emb_handle = prev_handle;
                   15064:        }
                   15065:        if(next_handle != NULL && next_handle->handle == 0) {
                   15066:                msdos_xms_combine_emb_handles(emb_handle);
                   15067:        }
                   15068:        emb_handle->handle = 0;
                   15069: }
                   15070: 
1.1.1.19  root     15071: inline void msdos_call_xms_00h()
                   15072: {
1.1.1.29  root     15073: #if defined(HAS_I386)
                   15074:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.45  root     15075: //     REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
1.1.1.29  root     15076:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   15077: #else
                   15078:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   15079:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   15080: #endif
                   15081: //     REG16(DX) = 0x0000; // HMA does not exist
                   15082:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     15083: }
                   15084: 
                   15085: inline void msdos_call_xms_01h()
                   15086: {
1.1.1.29  root     15087:        if(REG8(AL) == 0x40) {
                   15088:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   15089:                // DX=KB free extended memory returned by last call of function 08h
                   15090:                REG16(AX) = 0x0000;
                   15091:                REG8(BL) = 0x91;
                   15092:                REG16(DX) = xms_dx_after_call_08h;
                   15093:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   15094:                REG16(AX) = 0x0000;
                   15095:                REG8(BL) = 0x81; // Vdisk was detected
                   15096: #ifdef SUPPORT_HMA
                   15097:        } else if(is_hma_used_by_int_2fh) {
                   15098:                REG16(AX) = 0x0000;
                   15099:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   15100:        } else if(is_hma_used_by_xms) {
                   15101:                REG16(AX) = 0x0000;
                   15102:                REG8(BL) = 0x91; // HMA is already in use
                   15103:        } else {
                   15104:                REG16(AX) = 0x0001;
                   15105:                is_hma_used_by_xms = true;
                   15106: #else
                   15107:        } else {
                   15108:                REG16(AX) = 0x0000;
                   15109:                REG8(BL) = 0x91; // HMA is already in use
                   15110: #endif
                   15111:        }
1.1.1.19  root     15112: }
                   15113: 
                   15114: inline void msdos_call_xms_02h()
                   15115: {
1.1.1.29  root     15116:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   15117:                REG16(AX) = 0x0000;
                   15118:                REG8(BL) = 0x81; // Vdisk was detected
                   15119: #ifdef SUPPORT_HMA
                   15120:        } else if(is_hma_used_by_int_2fh) {
                   15121:                REG16(AX) = 0x0000;
                   15122:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   15123:        } else if(!is_hma_used_by_xms) {
                   15124:                REG16(AX) = 0x0000;
                   15125:                REG8(BL) = 0x93; // HMA is not allocated
                   15126:        } else {
                   15127:                REG16(AX) = 0x0001;
                   15128:                is_hma_used_by_xms = false;
                   15129:                // restore first free mcb in high memory area
                   15130:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   15131: #else
                   15132:        } else {
                   15133:                REG16(AX) = 0x0000;
                   15134:                REG8(BL) = 0x91; // HMA is already in use
                   15135: #endif
                   15136:        }
1.1.1.19  root     15137: }
                   15138: 
                   15139: inline void msdos_call_xms_03h()
                   15140: {
                   15141:        i386_set_a20_line(1);
                   15142:        REG16(AX) = 0x0001;
                   15143:        REG8(BL) = 0x00;
                   15144: }
                   15145: 
                   15146: inline void msdos_call_xms_04h()
                   15147: {
1.1.1.21  root     15148:        i386_set_a20_line(0);
                   15149:        REG16(AX) = 0x0001;
                   15150:        REG8(BL) = 0x00;
1.1.1.19  root     15151: }
                   15152: 
                   15153: inline void msdos_call_xms_05h()
                   15154: {
                   15155:        i386_set_a20_line(1);
                   15156:        REG16(AX) = 0x0001;
                   15157:        REG8(BL) = 0x00;
1.1.1.21  root     15158:        xms_a20_local_enb_count++;
1.1.1.19  root     15159: }
                   15160: 
                   15161: void msdos_call_xms_06h()
                   15162: {
1.1.1.21  root     15163:        if(xms_a20_local_enb_count > 0) {
1.1.1.45  root     15164:                if(--xms_a20_local_enb_count == 0) {
                   15165:                        i386_set_a20_line(0);
                   15166:                        REG16(AX) = 0x0001;
                   15167:                        REG8(BL) = 0x00;
                   15168:                } else {
                   15169:                        REG16(AX) = 0x0000;
                   15170:                        REG8(BL) = 0x94;
                   15171:                }
1.1.1.21  root     15172:        } else {
1.1.1.45  root     15173:                i386_set_a20_line(0);
1.1.1.21  root     15174:                REG16(AX) = 0x0001;
                   15175:                REG8(BL) = 0x00;
1.1.1.19  root     15176:        }
                   15177: }
                   15178: 
                   15179: inline void msdos_call_xms_07h()
                   15180: {
                   15181:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   15182:        REG8(BL) = 0x00;
                   15183: }
                   15184: 
                   15185: inline void msdos_call_xms_08h()
                   15186: {
1.1.1.45  root     15187:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     15188:        
1.1.1.30  root     15189:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15190:                if(emb_handle->handle == 0) {
1.1.1.45  root     15191:                        if(eax < emb_handle->size_kb) {
                   15192:                                eax = emb_handle->size_kb;
1.1.1.19  root     15193:                        }
1.1.1.45  root     15194:                        edx += emb_handle->size_kb;
1.1.1.19  root     15195:                }
                   15196:        }
1.1.1.45  root     15197:        if(eax > 65535) {
                   15198:                eax = 65535;
                   15199:        }
                   15200:        if(edx > 65535) {
                   15201:                edx = 65535;
                   15202:        }
                   15203:        if(eax == 0 && edx == 0) {
1.1.1.19  root     15204:                REG8(BL) = 0xa0;
                   15205:        } else {
                   15206:                REG8(BL) = 0x00;
                   15207:        }
1.1.1.45  root     15208: #if defined(HAS_I386)
                   15209:        REG32(EAX) = eax;
                   15210:        REG32(EDX) = edx;
                   15211: #else
                   15212:        REG16(AX) = (UINT16)eax;
                   15213:        REG16(DX) = (UINT16)edx;
                   15214: #endif
1.1.1.29  root     15215:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     15216: }
                   15217: 
1.1.1.30  root     15218: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     15219: {
1.1.1.30  root     15220:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   15221:        
                   15222:        if(emb_handle != NULL) {
                   15223:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   15224:                
                   15225:                REG16(AX) = 0x0001;
                   15226:                REG16(DX) = emb_handle->handle;
                   15227:                REG8(BL) = 0x00;
                   15228:        } else {
                   15229:                REG16(AX) = REG16(DX) = 0x0000;
                   15230:                REG8(BL) = 0xa0;
1.1.1.19  root     15231:        }
1.1.1.30  root     15232: }
                   15233: 
                   15234: inline void msdos_call_xms_09h()
                   15235: {
                   15236:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     15237: }
                   15238: 
                   15239: inline void msdos_call_xms_0ah()
                   15240: {
1.1.1.30  root     15241:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15242:        
                   15243:        if(emb_handle == NULL) {
1.1.1.19  root     15244:                REG16(AX) = 0x0000;
                   15245:                REG8(BL) = 0xa2;
1.1.1.45  root     15246: //     } else if(emb_handle->lock > 0) {
                   15247: //             REG16(AX) = 0x0000;
                   15248: //             REG8(BL) = 0xab;
1.1.1.19  root     15249:        } else {
1.1.1.30  root     15250:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     15251:                
                   15252:                REG16(AX) = 0x0001;
                   15253:                REG8(BL) = 0x00;
                   15254:        }
                   15255: }
                   15256: 
                   15257: inline void msdos_call_xms_0bh()
                   15258: {
                   15259:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15260:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15261:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   15262:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   15263:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15264:        
                   15265:        UINT8 *src_buffer, *dest_buffer;
                   15266:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     15267:        emb_handle_t *emb_handle;
1.1.1.19  root     15268:        
                   15269:        if(src_handle == 0) {
                   15270:                src_buffer = mem;
                   15271:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   15272:                src_addr_max = MAX_MEM;
                   15273:        } else {
1.1.1.30  root     15274:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     15275:                        REG16(AX) = 0x0000;
                   15276:                        REG8(BL) = 0xa3;
                   15277:                        return;
                   15278:                }
1.1.1.30  root     15279:                src_buffer = mem + emb_handle->address;
                   15280:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     15281:        }
                   15282:        if(dest_handle == 0) {
                   15283:                dest_buffer = mem;
                   15284:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   15285:                dest_addr_max = MAX_MEM;
                   15286:        } else {
1.1.1.30  root     15287:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     15288:                        REG16(AX) = 0x0000;
                   15289:                        REG8(BL) = 0xa5;
                   15290:                        return;
                   15291:                }
1.1.1.30  root     15292:                dest_buffer = mem + emb_handle->address;
                   15293:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     15294:        }
                   15295:        for(int i = 0; i < copy_length; i++) {
                   15296:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   15297:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   15298:                } else {
                   15299:                        break;
                   15300:                }
                   15301:        }
                   15302:        REG16(AX) = 0x0001;
                   15303:        REG8(BL) = 0x00;
                   15304: }
                   15305: 
                   15306: inline void msdos_call_xms_0ch()
                   15307: {
1.1.1.30  root     15308:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15309:        
                   15310:        if(emb_handle == NULL) {
1.1.1.19  root     15311:                REG16(AX) = 0x0000;
                   15312:                REG8(BL) = 0xa2;
                   15313:        } else {
1.1.1.45  root     15314:                if(emb_handle->lock < 255) {
                   15315:                        emb_handle->lock++;
                   15316:                }
1.1.1.19  root     15317:                REG16(AX) = 0x0001;
1.1.1.30  root     15318:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   15319:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     15320:        }
                   15321: }
                   15322: 
                   15323: inline void msdos_call_xms_0dh()
                   15324: {
1.1.1.30  root     15325:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15326:        
                   15327:        if(emb_handle == NULL) {
1.1.1.19  root     15328:                REG16(AX) = 0x0000;
                   15329:                REG8(BL) = 0xa2;
1.1.1.30  root     15330:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     15331:                REG16(AX) = 0x0000;
                   15332:                REG8(BL) = 0xaa;
                   15333:        } else {
1.1.1.30  root     15334:                emb_handle->lock--;
1.1.1.19  root     15335:                REG16(AX) = 0x0001;
                   15336:                REG8(BL) = 0x00;
                   15337:        }
                   15338: }
                   15339: 
                   15340: inline void msdos_call_xms_0eh()
                   15341: {
1.1.1.30  root     15342:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15343:        
                   15344:        if(emb_handle == NULL) {
1.1.1.19  root     15345:                REG16(AX) = 0x0000;
                   15346:                REG8(BL) = 0xa2;
                   15347:        } else {
                   15348:                REG16(AX) = 0x0001;
1.1.1.30  root     15349:                REG8(BH) = emb_handle->lock;
                   15350:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   15351:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     15352:        }
                   15353: }
                   15354: 
1.1.1.30  root     15355: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     15356: {
1.1.1.30  root     15357:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15358:        
                   15359:        if(emb_handle == NULL) {
1.1.1.19  root     15360:                REG16(AX) = 0x0000;
                   15361:                REG8(BL) = 0xa2;
1.1.1.30  root     15362:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     15363:                REG16(AX) = 0x0000;
                   15364:                REG8(BL) = 0xab;
                   15365:        } else {
1.1.1.30  root     15366:                if(emb_handle->size_kb < size_kb) {
                   15367:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   15368:                                msdos_xms_combine_emb_handles(emb_handle);
                   15369:                                if(emb_handle->size_kb > size_kb) {
                   15370:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   15371:                                }
                   15372:                        } else {
                   15373:                                int old_handle = emb_handle->handle;
                   15374:                                int old_size_kb = emb_handle->size_kb;
                   15375:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   15376:                                
                   15377:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   15378:                                msdos_xms_free_emb_handle(emb_handle);
                   15379:                                
                   15380:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   15381:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   15382:                                }
                   15383:                                emb_handle->handle = old_handle;
                   15384:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   15385:                                free(buffer);
                   15386:                        }
                   15387:                } else if(emb_handle->size_kb > size_kb) {
                   15388:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   15389:                }
                   15390:                if(emb_handle->size_kb != size_kb) {
                   15391:                        REG16(AX) = 0x0000;
                   15392:                        REG8(BL) = 0xa0;
                   15393:                } else {
                   15394:                        REG16(AX) = 0x0001;
                   15395:                        REG8(BL) = 0x00;
                   15396:                }
1.1.1.19  root     15397:        }
                   15398: }
                   15399: 
1.1.1.30  root     15400: inline void msdos_call_xms_0fh()
                   15401: {
                   15402:        msdos_call_xms_0fh(REG16(BX));
                   15403: }
                   15404: 
1.1.1.19  root     15405: inline void msdos_call_xms_10h()
                   15406: {
                   15407:        int seg;
                   15408:        
                   15409:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   15410:                REG16(AX) = 0x0001;
                   15411:                REG16(BX) = seg;
                   15412:        } else {
                   15413:                REG16(AX) = 0x0000;
                   15414:                REG8(BL) = 0xb0;
                   15415:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   15416:        }
                   15417: }
                   15418: 
                   15419: inline void msdos_call_xms_11h()
                   15420: {
                   15421:        int mcb_seg = REG16(DX) - 1;
                   15422:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   15423:        
                   15424:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   15425:                msdos_mem_free(REG16(DX));
                   15426:                REG16(AX) = 0x0001;
                   15427:                REG8(BL) = 0x00;
                   15428:        } else {
                   15429:                REG16(AX) = 0x0000;
                   15430:                REG8(BL) = 0xb2;
                   15431:        }
                   15432: }
                   15433: 
                   15434: inline void msdos_call_xms_12h()
                   15435: {
                   15436:        int mcb_seg = REG16(DX) - 1;
                   15437:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   15438:        int max_paragraphs;
                   15439:        
                   15440:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   15441:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   15442:                        REG16(AX) = 0x0001;
                   15443:                        REG8(BL) = 0x00;
                   15444:                } else {
                   15445:                        REG16(AX) = 0x0000;
                   15446:                        REG8(BL) = 0xb0;
                   15447:                        REG16(DX) = max_paragraphs;
                   15448:                }
                   15449:        } else {
                   15450:                REG16(AX) = 0x0000;
                   15451:                REG8(BL) = 0xb2;
                   15452:        }
                   15453: }
                   15454: 
1.1.1.29  root     15455: #if defined(HAS_I386)
                   15456: 
                   15457: inline void msdos_call_xms_88h()
                   15458: {
                   15459:        REG32(EAX) = REG32(EDX) = 0x0000;
                   15460:        
1.1.1.30  root     15461:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15462:                if(emb_handle->handle == 0) {
                   15463:                        if(REG32(EAX) < emb_handle->size_kb) {
                   15464:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     15465:                        }
1.1.1.30  root     15466:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     15467:                }
                   15468:        }
                   15469:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   15470:                REG8(BL) = 0xa0;
                   15471:        } else {
                   15472:                REG8(BL) = 0x00;
                   15473:        }
                   15474:        REG32(ECX) = EMB_END - 1;
                   15475: }
                   15476: 
                   15477: inline void msdos_call_xms_89h()
                   15478: {
1.1.1.30  root     15479:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     15480: }
                   15481: 
                   15482: inline void msdos_call_xms_8eh()
                   15483: {
1.1.1.30  root     15484:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15485:        
                   15486:        if(emb_handle == NULL) {
1.1.1.29  root     15487:                REG16(AX) = 0x0000;
                   15488:                REG8(BL) = 0xa2;
                   15489:        } else {
                   15490:                REG16(AX) = 0x0001;
1.1.1.30  root     15491:                REG8(BH) = emb_handle->lock;
                   15492:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   15493:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     15494:        }
                   15495: }
                   15496: 
                   15497: inline void msdos_call_xms_8fh()
                   15498: {
1.1.1.30  root     15499:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     15500: }
                   15501: 
                   15502: #endif
1.1.1.19  root     15503: #endif
                   15504: 
1.1.1.26  root     15505: UINT16 msdos_get_equipment()
                   15506: {
                   15507:        static UINT16 equip = 0;
                   15508:        
                   15509:        if(equip == 0) {
                   15510: #ifdef SUPPORT_FPU
                   15511:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   15512: #endif
                   15513:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   15514:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   15515: //             equip |= (1 << 8);      // 0 if DMA installed
                   15516:                equip |= (2 << 9);      // number of serial ports
                   15517:                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     15518:                
                   15519:                // check only A: and B: if it is floppy drive
                   15520:                int n = 0;
                   15521:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     15522:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   15523:                                n++;
1.1.1.28  root     15524:                        }
                   15525:                }
                   15526:                if(n != 0) {
                   15527:                        equip |= (1 << 0);      // floppy disk(s) installed
                   15528:                        n--;
                   15529:                        equip |= (n << 6);      // number of floppies installed less 1
                   15530:                }
                   15531: //             if(joyGetNumDevs() != 0) {
                   15532: //                     equip |= (1 << 12);     // game port installed
                   15533: //             }
1.1.1.26  root     15534:        }
                   15535:        return(equip);
                   15536: }
                   15537: 
1.1       root     15538: void msdos_syscall(unsigned num)
                   15539: {
1.1.1.22  root     15540: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     15541:        if(num == 0x08 || num == 0x1c) {
                   15542:                // don't log the timer interrupts
1.1.1.45  root     15543: //             fprintf(fp_debug_log, "int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.43  root     15544:        } else if(num == 0x68) {
1.1.1.22  root     15545:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     15546:                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     15547:        } else if(num == 0x69) {
                   15548:                // dummy interrupt for XMS (call far)
1.1.1.33  root     15549:                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.46! root     15550:        } else if(num >= 0x6a && num < 0x6f) {
1.1.1.45  root     15551:                // dummy interrupt
1.1.1.46! root     15552:        } else if(num == 0x6f) {
        !          15553:                // dummy interrupt for call 0005h (call near)
        !          15554:                fprintf(fp_debug_log, "call 0005h (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.22  root     15555:        } else {
1.1.1.33  root     15556:                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     15557:        }
                   15558: #endif
1.1.1.36  root     15559:        // update cursor position
                   15560:        if(cursor_moved) {
                   15561:                pcbios_update_cursor_position();
                   15562:                cursor_moved = false;
                   15563:        }
1.1.1.33  root     15564:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     15565:        
1.1       root     15566:        switch(num) {
                   15567:        case 0x00:
1.1.1.28  root     15568:                try {
                   15569:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   15570:                        error("division by zero\n");
                   15571:                } catch(...) {
                   15572:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   15573:                }
1.1       root     15574:                break;
                   15575:        case 0x04:
1.1.1.28  root     15576:                try {
                   15577:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   15578:                        error("overflow\n");
                   15579:                } catch(...) {
                   15580:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   15581:                }
1.1       root     15582:                break;
                   15583:        case 0x06:
                   15584:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     15585:                if(!ignore_illegal_insn) {
1.1.1.28  root     15586:                        try {
                   15587:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   15588:                                error("illegal instruction\n");
                   15589:                        } catch(...) {
                   15590:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   15591:                        }
1.1.1.14  root     15592:                } else {
                   15593: #if defined(HAS_I386)
1.1.1.39  root     15594:                        m_eip = m_int6h_skip_eip;
                   15595: #elif defined(HAS_I286)
                   15596:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     15597: #else
1.1.1.39  root     15598:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     15599: #endif
                   15600:                }
1.1       root     15601:                break;
1.1.1.33  root     15602:        case 0x09:
                   15603:                // ctrl-break is pressed
                   15604:                if(raise_int_1bh) {
                   15605: #if defined(HAS_I386)
                   15606:                        m_ext = 0; // not an external interrupt
                   15607:                        i386_trap(0x1b, 1, 0);
                   15608:                        m_ext = 1;
                   15609: #else
                   15610:                        PREFIX86(_interrupt)(0x1b);
                   15611: #endif
                   15612:                        raise_int_1bh = false;
                   15613:                }
1.1.1.8   root     15614:        case 0x08:
1.1.1.14  root     15615: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     15616:        case 0x0b:
                   15617:        case 0x0c:
                   15618:        case 0x0d:
                   15619:        case 0x0e:
                   15620:        case 0x0f:
                   15621:                // EOI
                   15622:                pic[0].isr &= ~(1 << (num - 0x08));
                   15623:                pic_update();
                   15624:                break;
1.1       root     15625:        case 0x10:
                   15626:                // PC BIOS - Video
1.1.1.14  root     15627:                if(!restore_console_on_exit) {
1.1.1.15  root     15628:                        change_console_size(scr_width, scr_height);
1.1       root     15629:                }
1.1.1.3   root     15630:                m_CF = 0;
1.1       root     15631:                switch(REG8(AH)) {
1.1.1.16  root     15632:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     15633:                case 0x01: pcbios_int_10h_01h(); break;
                   15634:                case 0x02: pcbios_int_10h_02h(); break;
                   15635:                case 0x03: pcbios_int_10h_03h(); break;
                   15636:                case 0x05: pcbios_int_10h_05h(); break;
                   15637:                case 0x06: pcbios_int_10h_06h(); break;
                   15638:                case 0x07: pcbios_int_10h_07h(); break;
                   15639:                case 0x08: pcbios_int_10h_08h(); break;
                   15640:                case 0x09: pcbios_int_10h_09h(); break;
                   15641:                case 0x0a: pcbios_int_10h_0ah(); break;
                   15642:                case 0x0b: break;
1.1.1.40  root     15643:                case 0x0c: pcbios_int_10h_0ch(); break;
                   15644:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     15645:                case 0x0e: pcbios_int_10h_0eh(); break;
                   15646:                case 0x0f: pcbios_int_10h_0fh(); break;
                   15647:                case 0x10: break;
1.1.1.14  root     15648:                case 0x11: pcbios_int_10h_11h(); break;
                   15649:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     15650:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     15651:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     15652:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     15653:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   15654:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     15655:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     15656:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   15657:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     15658:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     15659:                case 0x6f: break;
1.1.1.22  root     15660:                case 0x80: m_CF = 1; break; // unknown
                   15661:                case 0x81: m_CF = 1; break; // unknown
1.1       root     15662:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     15663:                case 0x83: pcbios_int_10h_83h(); break;
                   15664:                case 0x8b: break;
                   15665:                case 0x8c: m_CF = 1; break; // unknown
                   15666:                case 0x8d: m_CF = 1; break; // unknown
                   15667:                case 0x8e: m_CF = 1; break; // unknown
                   15668:                case 0x90: pcbios_int_10h_90h(); break;
                   15669:                case 0x91: pcbios_int_10h_91h(); break;
                   15670:                case 0x92: break;
                   15671:                case 0x93: break;
                   15672:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     15673:                case 0xfa: break; // ega register interface library is not installed
1.1       root     15674:                case 0xfe: pcbios_int_10h_feh(); break;
                   15675:                case 0xff: pcbios_int_10h_ffh(); break;
                   15676:                default:
1.1.1.22  root     15677:                        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));
                   15678:                        m_CF = 1;
1.1       root     15679:                        break;
                   15680:                }
                   15681:                break;
                   15682:        case 0x11:
                   15683:                // PC BIOS - Get Equipment List
1.1.1.26  root     15684:                REG16(AX) = msdos_get_equipment();
1.1       root     15685:                break;
                   15686:        case 0x12:
                   15687:                // PC BIOS - Get Memory Size
1.1.1.33  root     15688:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     15689:                break;
                   15690:        case 0x13:
1.1.1.42  root     15691:                // PC BIOS - Disk I/O
                   15692:                {
                   15693:                        static UINT8 last = 0x00;
                   15694:                        switch(REG8(AH)) {
                   15695:                        case 0x00: pcbios_int_13h_00h(); break;
                   15696:                        case 0x01: // get last status
                   15697:                                REG8(AH) = last;
                   15698:                                break;
                   15699:                        case 0x02: pcbios_int_13h_02h(); break;
                   15700:                        case 0x03: pcbios_int_13h_03h(); break;
                   15701:                        case 0x04: pcbios_int_13h_04h(); break;
                   15702:                        case 0x08: pcbios_int_13h_08h(); break;
                   15703:                        case 0x0a: pcbios_int_13h_02h(); break;
                   15704:                        case 0x0b: pcbios_int_13h_03h(); break;
                   15705:                        case 0x0d: pcbios_int_13h_00h(); break;
                   15706:                        case 0x10: pcbios_int_13h_10h(); break;
                   15707:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     15708:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     15709:                        case 0x05: // format
                   15710:                        case 0x06:
                   15711:                        case 0x07:
                   15712:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   15713:                                m_CF = 1;
                   15714:                                break;
                   15715:                        case 0x09:
                   15716:                        case 0x0c: // seek
                   15717:                        case 0x11: // recalib
                   15718:                        case 0x14:
                   15719:                        case 0x17:
                   15720:                                REG8(AH) = 0x00; // successful completion
                   15721:                                break;
1.1.1.43  root     15722:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   15723:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   15724:                                REG8(AH) = 0x01; // invalid function
                   15725:                                m_CF = 1;
                   15726:                                break;
1.1.1.42  root     15727:                        default:
                   15728:                                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));
                   15729:                                REG8(AH) = 0x01; // invalid function
                   15730:                                m_CF = 1;
                   15731:                                break;
                   15732:                        }
                   15733:                        last = REG8(AH);
                   15734:                }
1.1       root     15735:                break;
                   15736:        case 0x14:
                   15737:                // PC BIOS - Serial I/O
1.1.1.25  root     15738:                switch(REG8(AH)) {
                   15739:                case 0x00: pcbios_int_14h_00h(); break;
                   15740:                case 0x01: pcbios_int_14h_01h(); break;
                   15741:                case 0x02: pcbios_int_14h_02h(); break;
                   15742:                case 0x03: pcbios_int_14h_03h(); break;
                   15743:                case 0x04: pcbios_int_14h_04h(); break;
                   15744:                case 0x05: pcbios_int_14h_05h(); break;
                   15745:                default:
                   15746:                        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));
                   15747:                        break;
                   15748:                }
1.1       root     15749:                break;
                   15750:        case 0x15:
                   15751:                // PC BIOS
1.1.1.3   root     15752:                m_CF = 0;
1.1       root     15753:                switch(REG8(AH)) {
1.1.1.14  root     15754:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     15755:                case 0x23: pcbios_int_15h_23h(); break;
                   15756:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     15757:                case 0x41: break;
1.1       root     15758:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     15759:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     15760:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     15761:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     15762:                case 0x86: pcbios_int_15h_86h(); break;
                   15763:                case 0x87: pcbios_int_15h_87h(); break;
                   15764:                case 0x88: pcbios_int_15h_88h(); break;
                   15765:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     15766:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     15767:                case 0xc0: // PS/2 ???
                   15768:                case 0xc1:
                   15769:                case 0xc2:
1.1.1.30  root     15770:                case 0xc3: // PS50+ ???
                   15771:                case 0xc4:
1.1.1.22  root     15772:                        REG8(AH) = 0x86;
                   15773:                        m_CF = 1;
                   15774:                        break;
1.1.1.3   root     15775: #if defined(HAS_I386)
1.1       root     15776:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     15777: #endif
1.1       root     15778:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     15779:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     15780:                default:
1.1.1.22  root     15781:                        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));
                   15782:                        REG8(AH) = 0x86;
1.1.1.3   root     15783:                        m_CF = 1;
1.1       root     15784:                        break;
                   15785:                }
                   15786:                break;
                   15787:        case 0x16:
                   15788:                // PC BIOS - Keyboard
1.1.1.3   root     15789:                m_CF = 0;
1.1       root     15790:                switch(REG8(AH)) {
                   15791:                case 0x00: pcbios_int_16h_00h(); break;
                   15792:                case 0x01: pcbios_int_16h_01h(); break;
                   15793:                case 0x02: pcbios_int_16h_02h(); break;
                   15794:                case 0x03: pcbios_int_16h_03h(); break;
                   15795:                case 0x05: pcbios_int_16h_05h(); break;
                   15796:                case 0x10: pcbios_int_16h_00h(); break;
                   15797:                case 0x11: pcbios_int_16h_01h(); break;
                   15798:                case 0x12: pcbios_int_16h_12h(); break;
                   15799:                case 0x13: pcbios_int_16h_13h(); break;
                   15800:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     15801:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     15802:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     15803:                case 0xda: break; // unknown
1.1.1.43  root     15804:                case 0xdb: break; // unknown
1.1.1.22  root     15805:                case 0xff: break; // unknown
1.1       root     15806:                default:
1.1.1.22  root     15807:                        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     15808:                        break;
                   15809:                }
                   15810:                break;
                   15811:        case 0x17:
                   15812:                // PC BIOS - Printer
1.1.1.37  root     15813:                m_CF = 0;
                   15814:                switch(REG8(AH)) {
                   15815:                case 0x00: pcbios_int_17h_00h(); break;
                   15816:                case 0x01: pcbios_int_17h_01h(); break;
                   15817:                case 0x02: pcbios_int_17h_02h(); break;
                   15818:                case 0x03: pcbios_int_17h_03h(); break;
                   15819:                case 0x50: pcbios_int_17h_50h(); break;
                   15820:                case 0x51: pcbios_int_17h_51h(); break;
                   15821:                case 0x52: pcbios_int_17h_52h(); break;
                   15822:                case 0x84: pcbios_int_17h_84h(); break;
                   15823:                case 0x85: pcbios_int_17h_85h(); break;
                   15824:                default:
                   15825:                        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));
                   15826:                        break;
                   15827:                }
1.1       root     15828:                break;
                   15829:        case 0x1a:
                   15830:                // PC BIOS - Timer
1.1.1.3   root     15831:                m_CF = 0;
1.1       root     15832:                switch(REG8(AH)) {
                   15833:                case 0x00: pcbios_int_1ah_00h(); break;
                   15834:                case 0x01: break;
                   15835:                case 0x02: pcbios_int_1ah_02h(); break;
                   15836:                case 0x03: break;
                   15837:                case 0x04: pcbios_int_1ah_04h(); break;
                   15838:                case 0x05: break;
                   15839:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   15840:                case 0x0b: break;
1.1.1.14  root     15841:                case 0x35: break; // Word Perfect Third Party Interface?
                   15842:                case 0x36: break; // Word Perfect Third Party Interface
                   15843:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     15844:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     15845:                case 0xb1: break; // PCI BIOS v2.0c+
                   15846:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     15847:                default:
1.1.1.22  root     15848:                        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     15849:                        break;
                   15850:                }
                   15851:                break;
1.1.1.33  root     15852:        case 0x1b:
                   15853:                mem[0x471] = 0x00;
                   15854:                break;
1.1       root     15855:        case 0x20:
1.1.1.28  root     15856:                try {
                   15857:                        msdos_process_terminate(SREG(CS), retval, 1);
                   15858:                } catch(...) {
                   15859:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   15860:                }
1.1       root     15861:                break;
1.1.1.46! root     15862:        case 0x6f:
        !          15863:                // dummy interrupt for case map routine pointed in the country info
        !          15864: //             if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
        !          15865: //                     REG8(AL) = 0x00;
        !          15866: //                     break;
        !          15867: //             }
1.1       root     15868:        case 0x21:
                   15869:                // MS-DOS System Call
1.1.1.3   root     15870:                m_CF = 0;
1.1.1.28  root     15871:                try {
1.1.1.46! root     15872:                        switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28  root     15873:                        case 0x00: msdos_int_21h_00h(); break;
                   15874:                        case 0x01: msdos_int_21h_01h(); break;
                   15875:                        case 0x02: msdos_int_21h_02h(); break;
                   15876:                        case 0x03: msdos_int_21h_03h(); break;
                   15877:                        case 0x04: msdos_int_21h_04h(); break;
                   15878:                        case 0x05: msdos_int_21h_05h(); break;
                   15879:                        case 0x06: msdos_int_21h_06h(); break;
                   15880:                        case 0x07: msdos_int_21h_07h(); break;
                   15881:                        case 0x08: msdos_int_21h_08h(); break;
                   15882:                        case 0x09: msdos_int_21h_09h(); break;
                   15883:                        case 0x0a: msdos_int_21h_0ah(); break;
                   15884:                        case 0x0b: msdos_int_21h_0bh(); break;
                   15885:                        case 0x0c: msdos_int_21h_0ch(); break;
                   15886:                        case 0x0d: msdos_int_21h_0dh(); break;
                   15887:                        case 0x0e: msdos_int_21h_0eh(); break;
                   15888:                        case 0x0f: msdos_int_21h_0fh(); break;
                   15889:                        case 0x10: msdos_int_21h_10h(); break;
                   15890:                        case 0x11: msdos_int_21h_11h(); break;
                   15891:                        case 0x12: msdos_int_21h_12h(); break;
                   15892:                        case 0x13: msdos_int_21h_13h(); break;
                   15893:                        case 0x14: msdos_int_21h_14h(); break;
                   15894:                        case 0x15: msdos_int_21h_15h(); break;
                   15895:                        case 0x16: msdos_int_21h_16h(); break;
                   15896:                        case 0x17: msdos_int_21h_17h(); break;
                   15897:                        case 0x18: msdos_int_21h_18h(); break;
                   15898:                        case 0x19: msdos_int_21h_19h(); break;
                   15899:                        case 0x1a: msdos_int_21h_1ah(); break;
                   15900:                        case 0x1b: msdos_int_21h_1bh(); break;
                   15901:                        case 0x1c: msdos_int_21h_1ch(); break;
                   15902:                        case 0x1d: msdos_int_21h_1dh(); break;
                   15903:                        case 0x1e: msdos_int_21h_1eh(); break;
                   15904:                        case 0x1f: msdos_int_21h_1fh(); break;
                   15905:                        case 0x20: msdos_int_21h_20h(); break;
                   15906:                        case 0x21: msdos_int_21h_21h(); break;
                   15907:                        case 0x22: msdos_int_21h_22h(); break;
                   15908:                        case 0x23: msdos_int_21h_23h(); break;
                   15909:                        case 0x24: msdos_int_21h_24h(); break;
                   15910:                        case 0x25: msdos_int_21h_25h(); break;
                   15911:                        case 0x26: msdos_int_21h_26h(); break;
                   15912:                        case 0x27: msdos_int_21h_27h(); break;
                   15913:                        case 0x28: msdos_int_21h_28h(); break;
                   15914:                        case 0x29: msdos_int_21h_29h(); break;
                   15915:                        case 0x2a: msdos_int_21h_2ah(); break;
                   15916:                        case 0x2b: msdos_int_21h_2bh(); break;
                   15917:                        case 0x2c: msdos_int_21h_2ch(); break;
                   15918:                        case 0x2d: msdos_int_21h_2dh(); break;
                   15919:                        case 0x2e: msdos_int_21h_2eh(); break;
                   15920:                        case 0x2f: msdos_int_21h_2fh(); break;
                   15921:                        case 0x30: msdos_int_21h_30h(); break;
                   15922:                        case 0x31: msdos_int_21h_31h(); break;
                   15923:                        case 0x32: msdos_int_21h_32h(); break;
                   15924:                        case 0x33: msdos_int_21h_33h(); break;
                   15925:                        case 0x34: msdos_int_21h_34h(); break;
                   15926:                        case 0x35: msdos_int_21h_35h(); break;
                   15927:                        case 0x36: msdos_int_21h_36h(); break;
                   15928:                        case 0x37: msdos_int_21h_37h(); break;
                   15929:                        case 0x38: msdos_int_21h_38h(); break;
                   15930:                        case 0x39: msdos_int_21h_39h(0); break;
                   15931:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   15932:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   15933:                        case 0x3c: msdos_int_21h_3ch(); break;
                   15934:                        case 0x3d: msdos_int_21h_3dh(); break;
                   15935:                        case 0x3e: msdos_int_21h_3eh(); break;
                   15936:                        case 0x3f: msdos_int_21h_3fh(); break;
                   15937:                        case 0x40: msdos_int_21h_40h(); break;
                   15938:                        case 0x41: msdos_int_21h_41h(0); break;
                   15939:                        case 0x42: msdos_int_21h_42h(); break;
                   15940:                        case 0x43: msdos_int_21h_43h(0); break;
                   15941:                        case 0x44: msdos_int_21h_44h(); break;
                   15942:                        case 0x45: msdos_int_21h_45h(); break;
                   15943:                        case 0x46: msdos_int_21h_46h(); break;
                   15944:                        case 0x47: msdos_int_21h_47h(0); break;
                   15945:                        case 0x48: msdos_int_21h_48h(); break;
                   15946:                        case 0x49: msdos_int_21h_49h(); break;
                   15947:                        case 0x4a: msdos_int_21h_4ah(); break;
                   15948:                        case 0x4b: msdos_int_21h_4bh(); break;
                   15949:                        case 0x4c: msdos_int_21h_4ch(); break;
                   15950:                        case 0x4d: msdos_int_21h_4dh(); break;
                   15951:                        case 0x4e: msdos_int_21h_4eh(); break;
                   15952:                        case 0x4f: msdos_int_21h_4fh(); break;
                   15953:                        case 0x50: msdos_int_21h_50h(); break;
                   15954:                        case 0x51: msdos_int_21h_51h(); break;
                   15955:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     15956:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     15957:                        case 0x54: msdos_int_21h_54h(); break;
                   15958:                        case 0x55: msdos_int_21h_55h(); break;
                   15959:                        case 0x56: msdos_int_21h_56h(0); break;
                   15960:                        case 0x57: msdos_int_21h_57h(); break;
                   15961:                        case 0x58: msdos_int_21h_58h(); break;
                   15962:                        case 0x59: msdos_int_21h_59h(); break;
                   15963:                        case 0x5a: msdos_int_21h_5ah(); break;
                   15964:                        case 0x5b: msdos_int_21h_5bh(); break;
                   15965:                        case 0x5c: msdos_int_21h_5ch(); break;
                   15966:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     15967:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     15968:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     15969:                        case 0x60: msdos_int_21h_60h(0); break;
                   15970:                        case 0x61: msdos_int_21h_61h(); break;
                   15971:                        case 0x62: msdos_int_21h_62h(); break;
                   15972:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     15973:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     15974:                        case 0x65: msdos_int_21h_65h(); break;
                   15975:                        case 0x66: msdos_int_21h_66h(); break;
                   15976:                        case 0x67: msdos_int_21h_67h(); break;
                   15977:                        case 0x68: msdos_int_21h_68h(); break;
                   15978:                        case 0x69: msdos_int_21h_69h(); break;
                   15979:                        case 0x6a: msdos_int_21h_6ah(); break;
                   15980:                        case 0x6b: msdos_int_21h_6bh(); break;
                   15981:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.33  root     15982:                        // 0x6d: Find First ROM Program
                   15983:                        // 0x6e: Find Next ROM Program
                   15984:                        // 0x6f: Get/Set ROM Scan Start Address
1.1.1.43  root     15985:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.28  root     15986:                        case 0x71:
1.1.1.33  root     15987:                                // Windows95 - Long Filename Functions
1.1.1.28  root     15988:                                switch(REG8(AL)) {
                   15989:                                case 0x0d: msdos_int_21h_710dh(); break;
                   15990:                                case 0x39: msdos_int_21h_39h(1); break;
                   15991:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   15992:                                case 0x3b: msdos_int_21h_3bh(1); break;
                   15993:                                case 0x41: msdos_int_21h_7141h(1); break;
                   15994:                                case 0x43: msdos_int_21h_43h(1); break;
                   15995:                                case 0x47: msdos_int_21h_47h(1); break;
                   15996:                                case 0x4e: msdos_int_21h_714eh(); break;
                   15997:                                case 0x4f: msdos_int_21h_714fh(); break;
                   15998:                                case 0x56: msdos_int_21h_56h(1); break;
                   15999:                                case 0x60: msdos_int_21h_60h(1); break;
                   16000:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   16001:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   16002:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   16003:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   16004:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   16005:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45  root     16006:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     16007:                                case 0xaa: msdos_int_21h_71aah(); break;
                   16008:                                default:
                   16009:                                        unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", num, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   16010:                                        REG16(AX) = 0x7100;
                   16011:                                        m_CF = 1;
                   16012:                                        break;
                   16013:                                }
                   16014:                                break;
                   16015:                        // 0x72: Windows95 beta - LFN FindClose
                   16016:                        case 0x73:
1.1.1.33  root     16017:                                // Windows95 - FAT32 Functions
1.1.1.28  root     16018:                                switch(REG8(AL)) {
                   16019:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     16020:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     16021:                                case 0x02: msdos_int_21h_7302h(); break;
                   16022:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     16023:                                // 0x04: Set DPB to Use for Formatting
                   16024:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     16025:                                default:
                   16026:                                        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));
                   16027:                                        REG16(AX) = 0x7300;
                   16028:                                        m_CF = 1;
                   16029:                                        break;
                   16030:                                }
1.1       root     16031:                                break;
1.1.1.30  root     16032:                        case 0xdb: msdos_int_21h_dbh(); break;
                   16033:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     16034:                        default:
1.1.1.22  root     16035:                                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     16036:                                REG16(AX) = 0x01;
1.1.1.3   root     16037:                                m_CF = 1;
1.1       root     16038:                                break;
                   16039:                        }
1.1.1.28  root     16040:                } catch(int error) {
                   16041:                        REG16(AX) = error;
                   16042:                        m_CF = 1;
                   16043:                } catch(...) {
                   16044:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     16045:                        m_CF = 1;
1.1       root     16046:                }
1.1.1.3   root     16047:                if(m_CF) {
1.1.1.23  root     16048:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   16049:                        sda->extended_error_code = REG16(AX);
                   16050:                        switch(sda->extended_error_code) {
                   16051:                        case  4: // Too many open files
                   16052:                        case  8: // Insufficient memory
                   16053:                                sda->error_class = 1; // Out of resource
                   16054:                                break;
                   16055:                        case  5: // Access denied
                   16056:                                sda->error_class = 3; // Authorization
                   16057:                                break;
                   16058:                        case  7: // Memory control block destroyed
                   16059:                                sda->error_class = 4; // Internal
                   16060:                                break;
                   16061:                        case  2: // File not found
                   16062:                        case  3: // Path not found
                   16063:                        case 15: // Invaid drive specified
                   16064:                        case 18: // No more files
                   16065:                                sda->error_class = 8; // Not found
                   16066:                                break;
                   16067:                        case 32: // Sharing violation
                   16068:                        case 33: // Lock violation
                   16069:                                sda->error_class = 10; // Locked
                   16070:                                break;
                   16071: //                     case 16: // Removal of current directory attempted
                   16072:                        case 19: // Attempted write on protected disk
                   16073:                        case 21: // Drive not ready
                   16074: //                     case 29: // Write failure
                   16075: //                     case 30: // Read failure
                   16076: //                     case 82: // Cannot create subdirectory
                   16077:                                sda->error_class = 11; // Media
                   16078:                                break;
                   16079:                        case 80: // File already exists
                   16080:                                sda->error_class = 12; // Already exist
                   16081:                                break;
                   16082:                        default:
                   16083:                                sda->error_class = 13; // Unknown
                   16084:                                break;
                   16085:                        }
                   16086:                        sda->suggested_action = 1; // Retry
                   16087:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     16088:                }
1.1.1.33  root     16089:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     16090:                        // raise int 23h
                   16091: #if defined(HAS_I386)
                   16092:                        m_ext = 0; // not an external interrupt
                   16093:                        i386_trap(0x23, 1, 0);
                   16094:                        m_ext = 1;
                   16095: #else
                   16096:                        PREFIX86(_interrupt)(0x23);
                   16097: #endif
                   16098:                }
1.1       root     16099:                break;
                   16100:        case 0x22:
                   16101:                fatalerror("int 22h (terminate address)\n");
                   16102:        case 0x23:
1.1.1.28  root     16103:                try {
                   16104:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   16105:                } catch(...) {
                   16106:                        fatalerror("failed to terminate the current process by int 23h\n");
                   16107:                }
1.1       root     16108:                break;
                   16109:        case 0x24:
1.1.1.32  root     16110: /*
1.1.1.28  root     16111:                try {
                   16112:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16113:                } catch(...) {
                   16114:                        fatalerror("failed to terminate the current process by int 24h\n");
                   16115:                }
1.1.1.32  root     16116: */
                   16117:                msdos_int_24h();
1.1       root     16118:                break;
                   16119:        case 0x25:
                   16120:                msdos_int_25h();
                   16121:                break;
                   16122:        case 0x26:
                   16123:                msdos_int_26h();
                   16124:                break;
                   16125:        case 0x27:
1.1.1.28  root     16126:                try {
                   16127:                        msdos_int_27h();
                   16128:                } catch(...) {
                   16129:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   16130:                }
1.1       root     16131:                break;
                   16132:        case 0x28:
                   16133:                Sleep(10);
1.1.1.35  root     16134:                REQUEST_HARDWRE_UPDATE();
1.1       root     16135:                break;
                   16136:        case 0x29:
                   16137:                msdos_int_29h();
                   16138:                break;
                   16139:        case 0x2e:
                   16140:                msdos_int_2eh();
                   16141:                break;
                   16142:        case 0x2f:
                   16143:                // multiplex interrupt
                   16144:                switch(REG8(AH)) {
1.1.1.22  root     16145:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     16146:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     16147:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     16148:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     16149:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     16150:                case 0x14: msdos_int_2fh_14h(); break;
                   16151:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     16152:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     16153:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     16154:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     16155:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     16156:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     16157:                case 0x46: msdos_int_2fh_46h(); break;
                   16158:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     16159:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     16160:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     16161:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     16162:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     16163:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     16164:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     16165:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     16166:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     16167:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     16168:                default:
1.1.1.30  root     16169:                        switch(REG8(AL)) {
                   16170:                        case 0x00:
                   16171:                                // This is not installed
                   16172: //                             REG8(AL) = 0x00;
                   16173:                                break;
1.1.1.33  root     16174:                        case 0x01:
1.1.1.42  root     16175:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   16176:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   16177:                                        break;
                   16178:                                }
1.1.1.33  root     16179:                                // Banyan VINES v4+ is not installed
                   16180:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   16181:                                        break;
                   16182:                                }
1.1.1.42  root     16183:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   16184:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   16185:                                        break;
                   16186:                                }
1.1.1.30  root     16187:                        default:
1.1.1.42  root     16188:                                // NORTON UTILITIES 5.0+
                   16189:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   16190:                                        break;
                   16191:                                }
1.1.1.30  root     16192:                                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     16193:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     16194:                                m_CF = 1;
                   16195:                                break;
                   16196:                        }
                   16197:                        break;
1.1       root     16198:                }
                   16199:                break;
1.1.1.24  root     16200:        case 0x33:
                   16201:                switch(REG8(AH)) {
                   16202:                case 0x00:
                   16203:                        // Mouse
                   16204:                        switch(REG8(AL)) {
                   16205:                        case 0x00: msdos_int_33h_0000h(); break;
                   16206:                        case 0x01: msdos_int_33h_0001h(); break;
                   16207:                        case 0x02: msdos_int_33h_0002h(); break;
                   16208:                        case 0x03: msdos_int_33h_0003h(); break;
1.1.1.34  root     16209:                        case 0x04: msdos_int_33h_0004h(); break;
1.1.1.24  root     16210:                        case 0x05: msdos_int_33h_0005h(); break;
                   16211:                        case 0x06: msdos_int_33h_0006h(); break;
                   16212:                        case 0x07: msdos_int_33h_0007h(); break;
                   16213:                        case 0x08: msdos_int_33h_0008h(); break;
                   16214:                        case 0x09: msdos_int_33h_0009h(); break;
1.1.1.34  root     16215:                        case 0x0a: break; // Define Text Cursor
1.1.1.24  root     16216:                        case 0x0b: msdos_int_33h_000bh(); break;
                   16217:                        case 0x0c: msdos_int_33h_000ch(); break;
1.1.1.34  root     16218:                        case 0x0d: break; // Light Pen Emulation On
                   16219:                        case 0x0e: break; // Light Pen Emulation Off
1.1.1.24  root     16220:                        case 0x0f: msdos_int_33h_000fh(); break;
1.1.1.34  root     16221:                        case 0x10: break; // Define Screen Region for Updating
1.1.1.24  root     16222:                        case 0x11: msdos_int_33h_0011h(); break;
1.1.1.34  root     16223:                        case 0x12: REG16(AX) = 0xffff; break; // Set Large Graphics Cursor Block
                   16224:                        case 0x13: break; // Define Double-Speed Threshold
1.1.1.24  root     16225:                        case 0x14: msdos_int_33h_0014h(); break;
                   16226:                        case 0x15: msdos_int_33h_0015h(); break;
                   16227:                        case 0x16: msdos_int_33h_0016h(); break;
                   16228:                        case 0x17: msdos_int_33h_0017h(); break;
1.1.1.43  root     16229:                        case 0x18: msdos_int_33h_0018h(); break;
                   16230:                        case 0x19: msdos_int_33h_0019h(); break;
1.1.1.24  root     16231:                        case 0x1a: msdos_int_33h_001ah(); break;
                   16232:                        case 0x1b: msdos_int_33h_001bh(); break;
                   16233:                        case 0x1d: msdos_int_33h_001dh(); break;
                   16234:                        case 0x1e: msdos_int_33h_001eh(); break;
1.1.1.34  root     16235:                        case 0x1f: msdos_int_33h_001fh(); break;
                   16236:                        case 0x20: msdos_int_33h_0020h(); break;
1.1.1.24  root     16237:                        case 0x21: msdos_int_33h_0021h(); break;
                   16238:                        case 0x22: msdos_int_33h_0022h(); break;
                   16239:                        case 0x23: msdos_int_33h_0023h(); break;
                   16240:                        case 0x24: msdos_int_33h_0024h(); break;
                   16241:                        case 0x26: msdos_int_33h_0026h(); break;
                   16242:                        case 0x2a: msdos_int_33h_002ah(); break;
1.1.1.34  root     16243:                        case 0x2f: break; // Mouse Hardware Reset
1.1.1.24  root     16244:                        case 0x31: msdos_int_33h_0031h(); break;
                   16245:                        case 0x32: msdos_int_33h_0032h(); break;
                   16246:                        default:
                   16247:                                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));
                   16248:                                break;
                   16249:                        }
                   16250:                        break;
                   16251:                default:
                   16252:                        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));
                   16253:                        break;
                   16254:                }
                   16255:                break;
1.1.1.19  root     16256:        case 0x68:
                   16257:                // dummy interrupt for EMS (int 67h)
                   16258:                switch(REG8(AH)) {
                   16259:                case 0x40: msdos_int_67h_40h(); break;
                   16260:                case 0x41: msdos_int_67h_41h(); break;
                   16261:                case 0x42: msdos_int_67h_42h(); break;
                   16262:                case 0x43: msdos_int_67h_43h(); break;
                   16263:                case 0x44: msdos_int_67h_44h(); break;
                   16264:                case 0x45: msdos_int_67h_45h(); break;
                   16265:                case 0x46: msdos_int_67h_46h(); break;
                   16266:                case 0x47: msdos_int_67h_47h(); break;
                   16267:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     16268:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   16269:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     16270:                case 0x4b: msdos_int_67h_4bh(); break;
                   16271:                case 0x4c: msdos_int_67h_4ch(); break;
                   16272:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     16273:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     16274:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     16275:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     16276:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     16277:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     16278:                case 0x53: msdos_int_67h_53h(); break;
                   16279:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.20  root     16280:                // 0x55: LIM EMS 4.0 - Alter Page Map And JUMP
                   16281:                // 0x56: LIM EMS 4.0 - Alter Page Map And CALL
                   16282:                case 0x57: msdos_int_67h_57h(); break;
                   16283:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     16284:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     16285:                case 0x5a: msdos_int_67h_5ah(); break;
                   16286:                // 0x5b: LIM EMS 4.0 - Alternate Map Register Set (for DOS Kernel)
1.1.1.43  root     16287:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   16288:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.31  root     16289:                // 0x60: EEMS - Get Physical Window Array
                   16290:                // 0x61: EEMS - Generic Accelerator Card Support
                   16291:                // 0x68: EEMS - Get Address of All Pge Frames om System
                   16292:                // 0x69: EEMS - Map Page into Frame
                   16293:                // 0x6a: EEMS - Page Mapping
                   16294:                // 0xde: VCPI
1.1.1.30  root     16295:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     16296:                default:
1.1.1.22  root     16297:                        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     16298:                        REG8(AH) = 0x84;
                   16299:                        break;
                   16300:                }
                   16301:                break;
                   16302: #ifdef SUPPORT_XMS
                   16303:        case 0x69:
                   16304:                // dummy interrupt for XMS (call far)
1.1.1.28  root     16305:                try {
                   16306:                        switch(REG8(AH)) {
                   16307:                        case 0x00: msdos_call_xms_00h(); break;
                   16308:                        case 0x01: msdos_call_xms_01h(); break;
                   16309:                        case 0x02: msdos_call_xms_02h(); break;
                   16310:                        case 0x03: msdos_call_xms_03h(); break;
                   16311:                        case 0x04: msdos_call_xms_04h(); break;
                   16312:                        case 0x05: msdos_call_xms_05h(); break;
                   16313:                        case 0x06: msdos_call_xms_06h(); break;
                   16314:                        case 0x07: msdos_call_xms_07h(); break;
                   16315:                        case 0x08: msdos_call_xms_08h(); break;
                   16316:                        case 0x09: msdos_call_xms_09h(); break;
                   16317:                        case 0x0a: msdos_call_xms_0ah(); break;
                   16318:                        case 0x0b: msdos_call_xms_0bh(); break;
                   16319:                        case 0x0c: msdos_call_xms_0ch(); break;
                   16320:                        case 0x0d: msdos_call_xms_0dh(); break;
                   16321:                        case 0x0e: msdos_call_xms_0eh(); break;
                   16322:                        case 0x0f: msdos_call_xms_0fh(); break;
                   16323:                        case 0x10: msdos_call_xms_10h(); break;
                   16324:                        case 0x11: msdos_call_xms_11h(); break;
                   16325:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     16326: #if defined(HAS_I386)
                   16327:                        case 0x88: msdos_call_xms_88h(); break;
                   16328:                        case 0x89: msdos_call_xms_89h(); break;
                   16329:                        case 0x8e: msdos_call_xms_8eh(); break;
                   16330:                        case 0x8f: msdos_call_xms_8fh(); break;
                   16331: #endif
1.1.1.28  root     16332:                        default:
                   16333:                                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));
                   16334:                                REG16(AX) = 0x0000;
                   16335:                                REG8(BL) = 0x80; // function not implemented
                   16336:                                break;
                   16337:                        }
                   16338:                } catch(...) {
1.1.1.19  root     16339:                        REG16(AX) = 0x0000;
1.1.1.28  root     16340:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     16341:                }
                   16342:                break;
                   16343: #endif
                   16344:        case 0x6a:
1.1.1.24  root     16345:                // irq12 (mouse)
                   16346:                mouse_push_ax = REG16(AX);
                   16347:                mouse_push_bx = REG16(BX);
                   16348:                mouse_push_cx = REG16(CX);
                   16349:                mouse_push_dx = REG16(DX);
                   16350:                mouse_push_si = REG16(SI);
                   16351:                mouse_push_di = REG16(DI);
                   16352:                
1.1.1.43  root     16353:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     16354:                        REG16(AX) = mouse.status_irq;
                   16355:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     16356:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   16357:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     16358:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   16359:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   16360:                        
                   16361:                        mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   16362:                        mem[0xfffd0 + 0x03] = mouse.call_addr.w.l & 0xff;
                   16363:                        mem[0xfffd0 + 0x04] = mouse.call_addr.w.l >> 8;
                   16364:                        mem[0xfffd0 + 0x05] = mouse.call_addr.w.h & 0xff;
                   16365:                        mem[0xfffd0 + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.43  root     16366:                        break;
1.1.1.24  root     16367:                }
1.1.1.43  root     16368:                for(int i = 0; i < 8; i++) {
                   16369:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   16370:                                REG16(AX) = mouse.status_irq_alt;
                   16371:                                REG16(BX) = mouse.get_buttons();
                   16372:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   16373:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   16374:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   16375:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   16376:                                
                   16377:                                mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   16378:                                mem[0xfffd0 + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   16379:                                mem[0xfffd0 + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   16380:                                mem[0xfffd0 + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   16381:                                mem[0xfffd0 + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
                   16382:                                break;
                   16383:                        }
                   16384:                }
                   16385:                // invalid call addr :-(
                   16386:                mem[0xfffd0 + 0x02] = 0x90;     // nop
                   16387:                mem[0xfffd0 + 0x03] = 0x90;     // nop
                   16388:                mem[0xfffd0 + 0x04] = 0x90;     // nop
                   16389:                mem[0xfffd0 + 0x05] = 0x90;     // nop
                   16390:                mem[0xfffd0 + 0x06] = 0x90;     // nop
1.1.1.24  root     16391:                break;
                   16392:        case 0x6b:
                   16393:                // end of irq12 (mouse)
                   16394:                REG16(AX) = mouse_push_ax;
                   16395:                REG16(BX) = mouse_push_bx;
                   16396:                REG16(CX) = mouse_push_cx;
                   16397:                REG16(DX) = mouse_push_dx;
                   16398:                REG16(SI) = mouse_push_si;
                   16399:                REG16(DI) = mouse_push_di;
                   16400:                
                   16401:                // EOI
                   16402:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   16403:                        pic[0].isr &= ~(1 << 2); // master
                   16404:                }
                   16405:                pic_update();
                   16406:                break;
                   16407:        case 0x6c:
1.1.1.19  root     16408:                // dummy interrupt for case map routine pointed in the country info
                   16409:                if(REG8(AL) >= 0x80) {
                   16410:                        char tmp[2] = {0};
                   16411:                        tmp[0] = REG8(AL);
                   16412:                        my_strupr(tmp);
                   16413:                        REG8(AL) = tmp[0];
                   16414:                }
                   16415:                break;
1.1.1.27  root     16416:        case 0x6d:
                   16417:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   16418:                REG8(AL) = 0x86; // not supported
                   16419:                m_CF = 1;
                   16420:                break;
1.1.1.32  root     16421:        case 0x6e:
                   16422:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   16423:                {
                   16424:                        UINT16 code = REG16(AX);
                   16425:                        if(code & 0xf0) {
                   16426:                                code = (code & 7) | ((code & 0x10) >> 1);
                   16427:                        }
                   16428:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   16429:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   16430:                                        const char *message = NULL;
                   16431:                                        if(active_code_page == 932) {
                   16432:                                                message = param_error_table[i].message_japanese;
                   16433:                                        }
                   16434:                                        if(message == NULL) {
                   16435:                                                message = param_error_table[i].message_english;
                   16436:                                        }
                   16437:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   16438:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   16439:                                        
                   16440:                                        SREG(ES) = WORK_TOP >> 4;
                   16441:                                        i386_load_segment_descriptor(ES);
                   16442:                                        REG16(DI) = 0x0000;
                   16443:                                        break;
                   16444:                                }
                   16445:                        }
                   16446:                }
                   16447:                break;
1.1.1.8   root     16448:        case 0x70:
                   16449:        case 0x71:
                   16450:        case 0x72:
                   16451:        case 0x73:
                   16452:        case 0x74:
                   16453:        case 0x75:
                   16454:        case 0x76:
                   16455:        case 0x77:
                   16456:                // EOI
                   16457:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   16458:                        pic[0].isr &= ~(1 << 2); // master
                   16459:                }
                   16460:                pic_update();
                   16461:                break;
1.1       root     16462:        default:
1.1.1.22  root     16463: //             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     16464:                break;
                   16465:        }
                   16466:        
                   16467:        // update cursor position
                   16468:        if(cursor_moved) {
1.1.1.36  root     16469:                pcbios_update_cursor_position();
1.1       root     16470:                cursor_moved = false;
                   16471:        }
                   16472: }
                   16473: 
                   16474: // init
                   16475: 
                   16476: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   16477: {
                   16478:        // init file handler
                   16479:        memset(file_handler, 0, sizeof(file_handler));
                   16480:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   16481:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   16482:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     16483: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     16484:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     16485: #else
                   16486:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   16487: #endif
                   16488:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     16489:        }
1.1.1.21  root     16490:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45  root     16491: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   16492:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     16493:        }
1.1       root     16494:        _dup2(0, DUP_STDIN);
                   16495:        _dup2(1, DUP_STDOUT);
                   16496:        _dup2(2, DUP_STDERR);
1.1.1.21  root     16497:        _dup2(3, DUP_STDAUX);
                   16498:        _dup2(4, DUP_STDPRN);
1.1       root     16499:        
1.1.1.24  root     16500:        // init mouse
                   16501:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     16502:        mouse.enabled = true;   // from DOSBox
                   16503:        mouse.hidden = 1;       // hidden in default ???
                   16504:        mouse.old_hidden = 1;   // from DOSBox
                   16505:        mouse.max_position.x = 8 * (scr_width  - 1);
                   16506:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     16507:        mouse.mickey.x = 8;
                   16508:        mouse.mickey.y = 16;
                   16509:        
1.1.1.26  root     16510: #ifdef SUPPORT_XMS
                   16511:        // init xms
                   16512:        msdos_xms_init();
                   16513: #endif
                   16514:        
1.1       root     16515:        // init process
                   16516:        memset(process, 0, sizeof(process));
                   16517:        
1.1.1.13  root     16518:        // init dtainfo
                   16519:        msdos_dta_info_init();
                   16520:        
1.1       root     16521:        // init memory
                   16522:        memset(mem, 0, sizeof(mem));
                   16523:        
                   16524:        // bios data area
1.1.1.23  root     16525:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     16526:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   16527:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     16528:        CONSOLE_FONT_INFO cfi;
                   16529:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   16530:        
                   16531:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   16532:        text_vram_top_address = TEXT_VRAM_TOP;
                   16533:        text_vram_end_address = text_vram_top_address + regen;
                   16534:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   16535:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   16536:        
                   16537:        if(regen > 0x4000) {
                   16538:                regen = 0x8000;
                   16539:                vram_pages = 1;
                   16540:        } else if(regen > 0x2000) {
                   16541:                regen = 0x4000;
                   16542:                vram_pages = 2;
                   16543:        } else if(regen > 0x1000) {
                   16544:                regen = 0x2000;
                   16545:                vram_pages = 4;
                   16546:        } else {
                   16547:                regen = 0x1000;
                   16548:                vram_pages = 8;
                   16549:        }
1.1       root     16550:        
1.1.1.25  root     16551:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   16552:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     16553:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   16554:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     16555:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     16556:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   16557:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     16558: #ifdef EXT_BIOS_TOP
1.1.1.25  root     16559:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     16560: #endif
1.1.1.26  root     16561:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     16562:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     16563:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   16564:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     16565:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     16566:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   16567:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     16568:        *(UINT16 *)(mem + 0x44e) = 0;
                   16569:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     16570:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     16571:        *(UINT8  *)(mem + 0x460) = 7;
                   16572:        *(UINT8  *)(mem + 0x461) = 7;
                   16573:        *(UINT8  *)(mem + 0x462) = 0;
                   16574:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     16575:        *(UINT8  *)(mem + 0x465) = 0x09;
                   16576:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     16577:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     16578:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   16579:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     16580:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   16581:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   16582:        *(UINT8  *)(mem + 0x487) = 0x60;
                   16583:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     16584: #ifdef EXT_BIOS_TOP
1.1.1.25  root     16585:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     16586: #endif
1.1.1.14  root     16587:        
                   16588:        // initial screen
                   16589:        SMALL_RECT rect;
                   16590:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   16591:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   16592:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   16593:                for(int x = 0; x < scr_width; x++) {
                   16594:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   16595:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   16596:                }
                   16597:        }
1.1       root     16598:        
1.1.1.19  root     16599:        // init mcb
1.1       root     16600:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     16601:        
                   16602:        // iret table
                   16603:        // note: int 2eh vector should address the routine in command.com,
                   16604:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   16605:        // so move iret table into allocated memory block
                   16606:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33  root     16607:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19  root     16608:        IRET_TOP = seg << 4;
                   16609:        seg += IRET_SIZE >> 4;
1.1.1.25  root     16610:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     16611:        
                   16612:        // dummy xms/ems device
1.1.1.33  root     16613:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     16614:        XMS_TOP = seg << 4;
                   16615:        seg += XMS_SIZE >> 4;
                   16616:        
                   16617:        // environment
1.1.1.33  root     16618:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     16619:        int env_seg = seg;
                   16620:        int ofs = 0;
1.1.1.32  root     16621:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   16622:        char comspec_added = 0;
1.1.1.33  root     16623:        char lastdrive_added = 0;
1.1.1.32  root     16624:        char env_msdos_path[ENV_SIZE] = {0};
                   16625:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     16626:        char prompt_added = 0;
1.1.1.32  root     16627:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     16628:        char tz_added = 0;
1.1.1.45  root     16629:        const char *path, *short_path;
1.1.1.32  root     16630:        
                   16631:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   16632:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16633:                        strcpy(env_append, short_path);
                   16634:                }
                   16635:        }
                   16636:        if((path = getenv("APPEND")) != NULL) {
                   16637:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16638:                        if(env_append[0] != '\0') {
                   16639:                                strcat(env_append, ";");
                   16640:                        }
                   16641:                        strcat(env_append, short_path);
                   16642:                }
                   16643:        }
                   16644:        
                   16645:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   16646:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16647:                        strcpy(comspec_path, short_path);
                   16648:                }
                   16649:        }
                   16650:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   16651:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16652:                        strcpy(comspec_path, short_path);
                   16653:                }
                   16654:        }
1.1       root     16655:        
1.1.1.28  root     16656:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     16657:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16658:                        strcpy(env_msdos_path, short_path);
                   16659:                        strcpy(env_path, short_path);
1.1.1.14  root     16660:                }
                   16661:        }
1.1.1.28  root     16662:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     16663:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16664:                        if(env_path[0] != '\0') {
                   16665:                                strcat(env_path, ";");
                   16666:                        }
                   16667:                        strcat(env_path, short_path);
1.1.1.9   root     16668:                }
                   16669:        }
1.1.1.32  root     16670:        
                   16671:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   16672:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     16673:        }
1.1.1.32  root     16674:        for(int i = 0; i < 4; i++) {
                   16675:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   16676:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   16677:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16678:                                strcpy(env_temp, short_path);
                   16679:                                break;
                   16680:                        }
                   16681:                }
1.1.1.24  root     16682:        }
1.1.1.32  root     16683:        
1.1.1.9   root     16684:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     16685:                // lower to upper
1.1.1.28  root     16686:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     16687:                strcpy(tmp, *p);
                   16688:                for(int i = 0;; i++) {
                   16689:                        if(tmp[i] == '=') {
                   16690:                                tmp[i] = '\0';
                   16691:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     16692:                                my_strupr(name);
1.1       root     16693:                                tmp[i] = '=';
                   16694:                                break;
                   16695:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     16696:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     16697:                        }
                   16698:                }
1.1.1.33  root     16699:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   16700:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   16701:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     16702:                        // ignore non standard environments
                   16703:                } else {
1.1.1.33  root     16704:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     16705:                                if(env_append[0] != '\0') {
                   16706:                                        sprintf(tmp, "APPEND=%s", env_append);
                   16707:                                } else {
                   16708:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   16709:                                }
                   16710:                                append_added = 1;
                   16711:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     16712:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     16713:                                comspec_added = 1;
1.1.1.33  root     16714:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   16715:                                char *env = getenv("MSDOS_LASTDRIVE");
                   16716:                                if(env != NULL) {
                   16717:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   16718:                                }
                   16719:                                lastdrive_added = 1;
                   16720:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     16721:                                if(env_msdos_path[0] != '\0') {
                   16722:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   16723:                                } else {
                   16724:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   16725:                                }
1.1.1.33  root     16726:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     16727:                                if(env_path[0] != '\0') {
                   16728:                                        sprintf(tmp, "PATH=%s", env_path);
                   16729:                                } else {
                   16730:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   16731:                                }
1.1.1.32  root     16732:                                path_added = 1;
1.1.1.33  root     16733:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   16734:                                prompt_added = 1;
1.1.1.28  root     16735:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   16736:                                if(env_temp[0] != '\0') {
                   16737:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   16738:                                } else {
                   16739:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   16740:                                }
1.1.1.32  root     16741:                                temp_added = 1;
1.1.1.33  root     16742:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     16743:                                if(env_temp[0] != '\0') {
                   16744:                                        sprintf(tmp, "TMP=%s", env_temp);
                   16745:                                } else {
                   16746:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     16747:                                }
1.1.1.32  root     16748:                                tmp_added = 1;
1.1.1.33  root     16749:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   16750:                                char *env = getenv("MSDOS_TZ");
                   16751:                                if(env != NULL) {
                   16752:                                        sprintf(tmp, "TZ=%s", env);
                   16753:                                }
                   16754:                                tz_added = 1;
1.1       root     16755:                        }
                   16756:                        int len = strlen(tmp);
1.1.1.14  root     16757:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     16758:                                fatalerror("too many environments\n");
                   16759:                        }
                   16760:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   16761:                        ofs += len + 1;
                   16762:                }
                   16763:        }
1.1.1.32  root     16764:        if(!append_added && env_append[0] != '\0') {
                   16765:                #define SET_ENV(name, value) { \
                   16766:                        char tmp[ENV_SIZE]; \
                   16767:                        sprintf(tmp, "%s=%s", name, value); \
                   16768:                        int len = strlen(tmp); \
                   16769:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   16770:                                fatalerror("too many environments\n"); \
                   16771:                        } \
                   16772:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   16773:                        ofs += len + 1; \
                   16774:                }
                   16775:                SET_ENV("APPEND", env_append);
                   16776:        }
                   16777:        if(!comspec_added) {
                   16778:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   16779:        }
1.1.1.33  root     16780:        if(!lastdrive_added) {
                   16781:                SET_ENV("LASTDRIVE", "Z");
                   16782:        }
1.1.1.32  root     16783:        if(!path_added) {
                   16784:                SET_ENV("PATH", env_path);
                   16785:        }
1.1.1.33  root     16786:        if(!prompt_added) {
                   16787:                SET_ENV("PROMPT", "$P$G");
                   16788:        }
1.1.1.32  root     16789:        if(!temp_added) {
                   16790:                SET_ENV("TEMP", env_temp);
                   16791:        }
                   16792:        if(!tmp_added) {
                   16793:                SET_ENV("TMP", env_temp);
                   16794:        }
1.1.1.33  root     16795:        if(!tz_added) {
                   16796:                TIME_ZONE_INFORMATION tzi;
                   16797:                HKEY hKey, hSubKey;
                   16798:                char tzi_std_name[64];
                   16799:                char tz_std[8] = "GMT";
                   16800:                char tz_dlt[8] = "GST";
                   16801:                char tz_value[32];
                   16802:                
                   16803:                // timezone name from GetTimeZoneInformation may not be english
                   16804:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   16805:                setlocale(LC_CTYPE, "");
                   16806:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   16807:                
                   16808:                // get english timezone name from registry
                   16809:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   16810:                        for(DWORD i = 0; !tz_added; i++) {
                   16811:                                char reg_name[256], sub_key[1024], std_name[256];
                   16812:                                DWORD size;
                   16813:                                FILETIME ftTime;
                   16814:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   16815:                                
                   16816:                                if(result == ERROR_SUCCESS) {
                   16817:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   16818:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   16819:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   16820:                                                        // search english timezone name from table
1.1.1.37  root     16821:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     16822:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   16823:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   16824:                                                                                if(tz_table[j].std != NULL) {
                   16825:                                                                                        strcpy(tz_std, tz_table[j].std);
                   16826:                                                                                }
                   16827:                                                                                if(tz_table[j].dlt != NULL) {
                   16828:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   16829:                                                                                }
                   16830:                                                                                tz_added = 1;
                   16831:                                                                                break;
                   16832:                                                                        }
                   16833:                                                                }
                   16834:                                                        }
                   16835:                                                }
                   16836:                                                RegCloseKey(hSubKey);
                   16837:                                        }
                   16838:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   16839:                                        break;
                   16840:                                }
                   16841:                        }
                   16842:                        RegCloseKey(hKey);
                   16843:                }
                   16844:                if((tzi.Bias % 60) != 0) {
                   16845:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   16846:                } else {
                   16847:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   16848:                }
                   16849:                if(daylight) {
                   16850:                        strcat(tz_value, tz_dlt);
                   16851:                }
                   16852:                SET_ENV("TZ", tz_value);
                   16853:        }
1.1       root     16854:        seg += (ENV_SIZE >> 4);
                   16855:        
                   16856:        // psp
1.1.1.33  root     16857:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     16858:        current_psp = seg;
1.1.1.35  root     16859:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   16860:        psp->parent_psp = current_psp;
1.1       root     16861:        seg += (PSP_SIZE >> 4);
                   16862:        
1.1.1.19  root     16863:        // first free mcb in conventional memory
1.1.1.33  root     16864:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     16865:        first_mcb = seg;
                   16866:        
1.1.1.19  root     16867:        // dummy mcb to link to umb
1.1.1.33  root     16868: #if 0
1.1.1.39  root     16869:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     16870: #else
1.1.1.39  root     16871:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     16872: #endif
1.1.1.19  root     16873:        
                   16874:        // first free mcb in upper memory block
1.1.1.8   root     16875:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     16876:        
1.1.1.29  root     16877: #ifdef SUPPORT_HMA
                   16878:        // first free mcb in high memory area
                   16879:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   16880: #endif
                   16881:        
1.1.1.26  root     16882:        // interrupt vector
                   16883:        for(int i = 0; i < 0x80; i++) {
                   16884:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   16885:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   16886:        }
1.1.1.35  root     16887:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffd:0018 irq0 (system timer)
1.1.1.26  root     16888:        *(UINT16 *)(mem + 4 * 0x08 + 2) = 0xfffd;
                   16889:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   16890:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   16891:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   16892:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
                   16893:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffd:0000 irq12 (mouse)
                   16894:        *(UINT16 *)(mem + 4 * 0x74 + 2) = 0xfffd;
                   16895:        
1.1.1.29  root     16896:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     16897:        static const struct {
                   16898:                UINT16 attributes;
                   16899:                char *dev_name;
                   16900:        } dummy_devices[] = {
                   16901:                {0x8013, "CON     "},
                   16902:                {0x8000, "AUX     "},
                   16903:                {0xa0c0, "PRN     "},
                   16904:                {0x8008, "CLOCK$  "},
                   16905:                {0x8000, "COM1    "},
                   16906:                {0xa0c0, "LPT1    "},
                   16907:                {0xa0c0, "LPT2    "},
                   16908:                {0xa0c0, "LPT3    "},
                   16909:                {0x8000, "COM2    "},
                   16910:                {0x8000, "COM3    "},
                   16911:                {0x8000, "COM4    "},
1.1.1.30  root     16912: //             {0xc000, "CONFIG$ "},
                   16913:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     16914:        };
                   16915:        static const UINT8 dummy_device_routine[] = {
                   16916:                // from NUL device of Windows 98 SE
                   16917:                // or word ptr ES:[BX+03],0100
                   16918:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   16919:                // retf
                   16920:                0xcb,
                   16921:        };
1.1.1.29  root     16922:        device_t *last = NULL;
1.1.1.32  root     16923:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     16924:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     16925:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   16926:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     16927:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     16928:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   16929:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     16930:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     16931:                last = device;
                   16932:        }
                   16933:        if(last != NULL) {
                   16934:                last->next_driver.w.l = 0;
                   16935:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     16936:        }
1.1.1.29  root     16937:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     16938:        
1.1.1.25  root     16939:        // dos info
                   16940:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   16941:        dos_info->magic_word = 1;
                   16942:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   16943:        dos_info->first_dpb.w.l = 0;
                   16944:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   16945:        dos_info->first_sft.w.l = 0;
                   16946:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     16947:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     16948:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     16949:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     16950:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   16951:        dos_info->max_sector_len = 512;
                   16952:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   16953:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   16954:        dos_info->cds.w.l = 0;
                   16955:        dos_info->cds.w.h = CDS_TOP >> 4;
                   16956:        dos_info->fcb_table.w.l = 0;
                   16957:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   16958:        dos_info->last_drive = 'Z' - 'A' + 1;
                   16959:        dos_info->buffers_x = 20;
                   16960:        dos_info->buffers_y = 0;
                   16961:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     16962:        dos_info->nul_device.next_driver.w.l = 22;
                   16963:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     16964:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     16965:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   16966:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     16967:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   16968:        dos_info->disk_buf_heads.w.l = 0;
                   16969:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     16970:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     16971:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   16972:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     16973:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     16974:        
                   16975:        char *env;
                   16976:        if((env = getenv("LASTDRIVE")) != NULL) {
                   16977:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   16978:                        dos_info->last_drive = env[0] - 'A' + 1;
                   16979:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   16980:                        dos_info->last_drive = env[0] - 'a' + 1;
                   16981:                }
                   16982:        }
                   16983:        if((env = getenv("windir")) != NULL) {
                   16984:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   16985:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   16986:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   16987:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   16988:                }
                   16989:        }
                   16990: #if defined(HAS_I386)
                   16991:        dos_info->i386_or_later = 1;
                   16992: #else
                   16993:        dos_info->i386_or_later = 0;
                   16994: #endif
                   16995:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   16996:        
1.1.1.27  root     16997:        // ems (int 67h) and xms
1.1.1.25  root     16998:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   16999:        xms_device->next_driver.w.l = 0xffff;
                   17000:        xms_device->next_driver.w.h = 0xffff;
                   17001:        xms_device->attributes = 0xc000;
1.1.1.29  root     17002:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   17003:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     17004:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   17005:        
1.1.1.26  root     17006:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   17007:        mem[XMS_TOP + 0x13] = 0x68;
                   17008:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     17009: #ifdef SUPPORT_XMS
                   17010:        if(support_xms) {
1.1.1.26  root     17011:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   17012:                mem[XMS_TOP + 0x16] = 0x69;
                   17013:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     17014:        } else
                   17015: #endif
1.1.1.26  root     17016:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     17017:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     17018:        
1.1.1.26  root     17019:        // irq12 routine (mouse)
1.1.1.24  root     17020:        mem[0xfffd0 + 0x00] = 0xcd;     // int 6ah (dummy)
                   17021:        mem[0xfffd0 + 0x01] = 0x6a;
                   17022:        mem[0xfffd0 + 0x02] = 0x9a;     // call far mouse
                   17023:        mem[0xfffd0 + 0x03] = 0xff;
                   17024:        mem[0xfffd0 + 0x04] = 0xff;
                   17025:        mem[0xfffd0 + 0x05] = 0xff;
                   17026:        mem[0xfffd0 + 0x06] = 0xff;
                   17027:        mem[0xfffd0 + 0x07] = 0xcd;     // int 6bh (dummy)
                   17028:        mem[0xfffd0 + 0x08] = 0x6b;
                   17029:        mem[0xfffd0 + 0x09] = 0xcf;     // iret
                   17030:        
1.1.1.27  root     17031:        // case map routine
                   17032:        mem[0xfffd0 + 0x0a] = 0xcd;     // int 6ch (dummy)
                   17033:        mem[0xfffd0 + 0x0b] = 0x6c;
                   17034:        mem[0xfffd0 + 0x0c] = 0xcb;     // retf
                   17035:        
                   17036:        // font read routine
                   17037:        mem[0xfffd0 + 0x0d] = 0xcd;     // int 6dh (dummy)
                   17038:        mem[0xfffd0 + 0x0e] = 0x6d;
                   17039:        mem[0xfffd0 + 0x0f] = 0xcb;     // retf
1.1.1.19  root     17040:        
1.1.1.32  root     17041:        // error message read routine
                   17042:        mem[0xfffd0 + 0x10] = 0xcd;     // int 6eh (dummy)
                   17043:        mem[0xfffd0 + 0x11] = 0x6e;
                   17044:        mem[0xfffd0 + 0x12] = 0xcb;     // retf
                   17045:        
1.1.1.35  root     17046:        // dummy loop to wait BIOS/DOS service is done
                   17047:        mem[0xfffd0 + 0x13] = 0xe6;     // out f7h, al
                   17048:        mem[0xfffd0 + 0x14] = 0xf7;
                   17049:        mem[0xfffd0 + 0x15] = 0x78;     // js/jns -4
                   17050:        mem[0xfffd0 + 0x16] = 0xfc;
                   17051:        mem[0xfffd0 + 0x17] = 0xcb;     // retf
                   17052:        
1.1.1.26  root     17053:        // irq0 routine (system time)
1.1.1.35  root     17054:        mem[0xfffd0 + 0x18] = 0xcd;     // int 1ch
                   17055:        mem[0xfffd0 + 0x19] = 0x1c;
                   17056:        mem[0xfffd0 + 0x1a] = 0xea;     // jmp far (IRET_TOP >> 4):0008
                   17057:        mem[0xfffd0 + 0x1b] = 0x08;
                   17058:        mem[0xfffd0 + 0x1c] = 0x00;
                   17059:        mem[0xfffd0 + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   17060:        mem[0xfffd0 + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
1.1.1.14  root     17061:        
1.1.1.26  root     17062:        // boot routine
1.1       root     17063:        mem[0xffff0] = 0xf4;    // halt
                   17064:        
1.1.1.24  root     17065:        mem[0xffff5] = '0';     // rom date
                   17066:        mem[0xffff6] = '2';
                   17067:        mem[0xffff7] = '/';
                   17068:        mem[0xffff8] = '2';
                   17069:        mem[0xffff9] = '2';
                   17070:        mem[0xffffa] = '/';
                   17071:        mem[0xffffb] = '0';
                   17072:        mem[0xffffc] = '6';
                   17073:        mem[0xffffe] = 0xfc;    // machine id
                   17074:        mem[0xfffff] = 0x00;
                   17075:        
1.1       root     17076:        // param block
                   17077:        // + 0: param block (22bytes)
                   17078:        // +24: fcb1/2 (20bytes)
                   17079:        // +44: command tail (128bytes)
                   17080:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   17081:        param->env_seg = 0;
                   17082:        param->cmd_line.w.l = 44;
                   17083:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   17084:        param->fcb1.w.l = 24;
                   17085:        param->fcb1.w.h = (WORK_TOP >> 4);
                   17086:        param->fcb2.w.l = 24;
                   17087:        param->fcb2.w.h = (WORK_TOP >> 4);
                   17088:        
                   17089:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   17090:        
                   17091:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   17092:        if(argc > 1) {
                   17093:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   17094:                for(int i = 2; i < argc; i++) {
                   17095:                        char tmp[128];
                   17096:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   17097:                        strcpy(cmd_line->cmd, tmp);
                   17098:                }
                   17099:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   17100:        } else {
                   17101:                cmd_line->len = 0;
                   17102:        }
                   17103:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   17104:        
                   17105:        // system file table
1.1.1.21  root     17106:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   17107:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     17108:        
1.1.1.19  root     17109:        // disk buffer header (from DOSBox)
                   17110:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   17111:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   17112:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   17113:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   17114:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   17115:        
1.1       root     17116:        // fcb table
                   17117:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     17118:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     17119:        
1.1.1.41  root     17120:        // drive parameter block
1.1.1.42  root     17121:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     17122:                // may be a floppy drive
1.1.1.44  root     17123:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   17124:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   17125:                cds->drive_attrib = 0x4000;     // physical drive
                   17126:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   17127:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   17128:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   17129:                cds->bs_offset = 2;
                   17130:                
1.1.1.41  root     17131:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   17132:                dpb->drive_num = i;
                   17133:                dpb->unit_num = i;
1.1.1.43  root     17134:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   17135:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     17136:        }
                   17137:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     17138:                msdos_cds_update(i);
1.1.1.42  root     17139:                UINT16 seg, ofs;
                   17140:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     17141:        }
                   17142:        
1.1.1.17  root     17143:        // nls stuff
                   17144:        msdos_nls_tables_init();
1.1       root     17145:        
                   17146:        // execute command
1.1.1.28  root     17147:        try {
                   17148:                if(msdos_process_exec(argv[0], param, 0)) {
                   17149:                        fatalerror("'%s' not found\n", argv[0]);
                   17150:                }
                   17151:        } catch(...) {
                   17152:                // we should not reach here :-(
                   17153:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     17154:        }
                   17155:        retval = 0;
                   17156:        return(0);
                   17157: }
                   17158: 
                   17159: #define remove_std_file(path) { \
                   17160:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   17161:        if(fd != -1) { \
                   17162:                _lseek(fd, 0, SEEK_END); \
                   17163:                int size = _tell(fd); \
                   17164:                _close(fd); \
                   17165:                if(size == 0) { \
                   17166:                        remove(path); \
                   17167:                } \
                   17168:        } \
                   17169: }
                   17170: 
                   17171: void msdos_finish()
                   17172: {
                   17173:        for(int i = 0; i < MAX_FILES; i++) {
                   17174:                if(file_handler[i].valid) {
                   17175:                        _close(i);
                   17176:                }
                   17177:        }
1.1.1.21  root     17178: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     17179:        remove_std_file("stdaux.txt");
1.1.1.21  root     17180: #endif
1.1.1.30  root     17181: #ifdef SUPPORT_XMS
                   17182:        msdos_xms_finish();
                   17183: #endif
1.1       root     17184:        msdos_dbcs_table_finish();
                   17185: }
                   17186: 
                   17187: /* ----------------------------------------------------------------------------
                   17188:        PC/AT hardware emulation
                   17189: ---------------------------------------------------------------------------- */
                   17190: 
                   17191: void hardware_init()
                   17192: {
1.1.1.3   root     17193:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     17194:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     17195:        m_IF = 1;
1.1.1.3   root     17196: #if defined(HAS_I386)
1.1       root     17197:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   17198:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     17199: #endif
                   17200:        i386_set_a20_line(0);
1.1.1.14  root     17201:        
1.1.1.19  root     17202:        ems_init();
1.1.1.25  root     17203:        dma_init();
1.1       root     17204:        pic_init();
1.1.1.25  root     17205:        pio_init();
1.1.1.8   root     17206: #ifdef PIT_ALWAYS_RUNNING
                   17207:        pit_init();
                   17208: #else
1.1       root     17209:        pit_active = 0;
                   17210: #endif
1.1.1.25  root     17211:        sio_init();
1.1.1.8   root     17212:        cmos_init();
                   17213:        kbd_init();
1.1       root     17214: }
                   17215: 
1.1.1.10  root     17216: void hardware_finish()
                   17217: {
                   17218: #if defined(HAS_I386)
                   17219:        vtlb_free(m_vtlb);
                   17220: #endif
1.1.1.19  root     17221:        ems_finish();
1.1.1.37  root     17222:        pio_finish();
1.1.1.25  root     17223:        sio_finish();
1.1.1.10  root     17224: }
                   17225: 
1.1.1.28  root     17226: void hardware_release()
                   17227: {
                   17228:        // release hardware resources when this program will be terminated abnormally
                   17229: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     17230:        if(fp_debug_log != NULL) {
                   17231:                fclose(fp_debug_log);
                   17232:                fp_debug_log = NULL;
1.1.1.28  root     17233:        }
                   17234: #endif
                   17235: #if defined(HAS_I386)
                   17236:        vtlb_free(m_vtlb);
                   17237: #endif
                   17238:        ems_release();
1.1.1.37  root     17239:        pio_release();
1.1.1.28  root     17240:        sio_release();
                   17241: }
                   17242: 
1.1       root     17243: void hardware_run()
                   17244: {
1.1.1.22  root     17245: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     17246:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     17247:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     17248: #endif
1.1.1.3   root     17249:        while(!m_halted) {
                   17250: #if defined(HAS_I386)
1.1       root     17251:                CPU_EXECUTE_CALL(i386);
1.1.1.14  root     17252:                if(m_eip != m_prev_eip) {
1.1.1.35  root     17253:                        idle_ops++;
                   17254:                }
1.1.1.14  root     17255: #else
1.1.1.35  root     17256:                CPU_EXECUTE_CALL(CPU_MODEL);
1.1.1.14  root     17257:                if(m_pc != m_prevpc) {
1.1.1.35  root     17258:                        idle_ops++;
1.1.1.14  root     17259:                }
1.1.1.35  root     17260: #endif
                   17261:                if(++update_ops == UPDATE_OPS) {
1.1       root     17262:                        hardware_update();
1.1.1.35  root     17263:                        update_ops = 0;
1.1       root     17264:                }
                   17265:        }
1.1.1.22  root     17266: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     17267:        if(fp_debug_log != NULL) {
                   17268:                fclose(fp_debug_log);
                   17269:                fp_debug_log = NULL;
1.1.1.28  root     17270:        }
1.1.1.22  root     17271: #endif
1.1       root     17272: }
                   17273: 
                   17274: void hardware_update()
                   17275: {
1.1.1.8   root     17276:        static UINT32 prev_time = 0;
                   17277:        UINT32 cur_time = timeGetTime();
                   17278:        
                   17279:        if(prev_time != cur_time) {
                   17280:                // update pit and raise irq0
                   17281: #ifndef PIT_ALWAYS_RUNNING
                   17282:                if(pit_active)
                   17283: #endif
                   17284:                {
                   17285:                        if(pit_run(0, cur_time)) {
                   17286:                                pic_req(0, 0, 1);
                   17287:                        }
                   17288:                        pit_run(1, cur_time);
                   17289:                        pit_run(2, cur_time);
                   17290:                }
1.1.1.24  root     17291:                
1.1.1.25  root     17292:                // update sio and raise irq4/3
1.1.1.29  root     17293:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     17294:                        sio_update(c);
                   17295:                }
                   17296:                
1.1.1.24  root     17297:                // update keyboard and mouse
1.1.1.14  root     17298:                static UINT32 prev_tick = 0;
                   17299:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     17300:                
1.1.1.14  root     17301:                if(prev_tick != cur_tick) {
                   17302:                        // update keyboard flags
                   17303:                        UINT8 state;
1.1.1.24  root     17304:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   17305:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   17306:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   17307:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   17308:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   17309:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   17310:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   17311:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     17312:                        mem[0x417] = state;
                   17313:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   17314:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   17315:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   17316:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     17317: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   17318: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     17319:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   17320:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   17321:                        mem[0x418] = state;
                   17322:                        
1.1.1.24  root     17323:                        // update console input if needed
1.1.1.34  root     17324:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     17325:                                update_console_input();
                   17326:                        }
                   17327:                        
                   17328:                        // raise irq1 if key is pressed/released
                   17329:                        if(key_changed) {
1.1.1.8   root     17330:                                pic_req(0, 1, 1);
1.1.1.24  root     17331:                                key_changed = false;
                   17332:                        }
                   17333:                        
                   17334:                        // raise irq12 if mouse status is changed
1.1.1.43  root     17335:                        if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
                   17336:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   17337:                                mouse.status_irq_alt = 0; // ???
1.1.1.24  root     17338:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     17339:                                pic_req(1, 4, 1);
                   17340:                        } else {
                   17341:                                for(int i = 0; i < 8; i++) {
                   17342:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17343:                                                mouse.status_irq = 0; // ???
                   17344:                                                mouse.status_irq_alt = 0;
                   17345:                                                for(int j = 0; j < 8; j++) {
                   17346:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   17347:                                                                mouse.status_irq_alt |=  (1 << j);
                   17348:                                                                mouse.status_alt     &= ~(1 << j);
                   17349:                                                        }
                   17350:                                                }
                   17351:                                                pic_req(1, 4, 1);
                   17352:                                                break;
                   17353:                                        }
                   17354:                                }
1.1.1.8   root     17355:                        }
1.1.1.24  root     17356:                        
1.1.1.14  root     17357:                        prev_tick = cur_tick;
1.1.1.8   root     17358:                }
1.1.1.24  root     17359:                
1.1.1.19  root     17360:                // update daily timer counter
                   17361:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     17362:                
1.1.1.8   root     17363:                prev_time = cur_time;
1.1       root     17364:        }
                   17365: }
                   17366: 
1.1.1.19  root     17367: // ems
                   17368: 
                   17369: void ems_init()
                   17370: {
                   17371:        memset(ems_handles, 0, sizeof(ems_handles));
                   17372:        memset(ems_pages, 0, sizeof(ems_pages));
                   17373:        free_ems_pages = MAX_EMS_PAGES;
                   17374: }
                   17375: 
                   17376: void ems_finish()
                   17377: {
1.1.1.28  root     17378:        ems_release();
                   17379: }
                   17380: 
                   17381: void ems_release()
                   17382: {
1.1.1.31  root     17383:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     17384:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     17385:                        free(ems_handles[i].buffer);
                   17386:                        ems_handles[i].buffer = NULL;
                   17387:                }
                   17388:        }
                   17389: }
                   17390: 
                   17391: void ems_allocate_pages(int handle, int pages)
                   17392: {
                   17393:        if(pages > 0) {
                   17394:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   17395:        } else {
                   17396:                ems_handles[handle].buffer = NULL;
                   17397:        }
                   17398:        ems_handles[handle].pages = pages;
                   17399:        ems_handles[handle].allocated = true;
                   17400:        free_ems_pages -= pages;
                   17401: }
                   17402: 
                   17403: void ems_reallocate_pages(int handle, int pages)
                   17404: {
                   17405:        if(ems_handles[handle].allocated) {
                   17406:                if(ems_handles[handle].pages != pages) {
                   17407:                        UINT8 *new_buffer = NULL;
                   17408:                        
                   17409:                        if(pages > 0) {
                   17410:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   17411:                        }
1.1.1.32  root     17412:                        if(ems_handles[handle].buffer != NULL) {
                   17413:                                if(new_buffer != NULL) {
1.1.1.19  root     17414:                                        if(pages > ems_handles[handle].pages) {
                   17415:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   17416:                                        } else {
                   17417:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   17418:                                        }
                   17419:                                }
                   17420:                                free(ems_handles[handle].buffer);
                   17421:                                ems_handles[handle].buffer = NULL;
                   17422:                        }
                   17423:                        free_ems_pages += ems_handles[handle].pages;
                   17424:                        
                   17425:                        ems_handles[handle].buffer = new_buffer;
                   17426:                        ems_handles[handle].pages = pages;
                   17427:                        free_ems_pages -= pages;
                   17428:                }
                   17429:        } else {
                   17430:                ems_allocate_pages(handle, pages);
                   17431:        }
                   17432: }
                   17433: 
                   17434: void ems_release_pages(int handle)
                   17435: {
                   17436:        if(ems_handles[handle].allocated) {
1.1.1.32  root     17437:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     17438:                        free(ems_handles[handle].buffer);
                   17439:                        ems_handles[handle].buffer = NULL;
                   17440:                }
                   17441:                free_ems_pages += ems_handles[handle].pages;
                   17442:                ems_handles[handle].allocated = false;
                   17443:        }
                   17444: }
                   17445: 
                   17446: void ems_map_page(int physical, int handle, int logical)
                   17447: {
                   17448:        if(ems_pages[physical].mapped) {
                   17449:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   17450:                        return;
                   17451:                }
                   17452:                ems_unmap_page(physical);
                   17453:        }
1.1.1.32  root     17454:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     17455:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   17456:        }
                   17457:        ems_pages[physical].handle = handle;
                   17458:        ems_pages[physical].page = logical;
                   17459:        ems_pages[physical].mapped = true;
                   17460: }
                   17461: 
                   17462: void ems_unmap_page(int physical)
                   17463: {
                   17464:        if(ems_pages[physical].mapped) {
                   17465:                int handle = ems_pages[physical].handle;
                   17466:                int logical = ems_pages[physical].page;
                   17467:                
1.1.1.32  root     17468:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     17469:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   17470:                }
                   17471:                ems_pages[physical].mapped = false;
                   17472:        }
                   17473: }
                   17474: 
1.1.1.25  root     17475: // dma
1.1       root     17476: 
1.1.1.25  root     17477: void dma_init()
1.1       root     17478: {
1.1.1.26  root     17479:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     17480:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     17481: //             for(int ch = 0; ch < 4; ch++) {
                   17482: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   17483: //             }
1.1.1.25  root     17484:                dma_reset(c);
                   17485:        }
1.1       root     17486: }
                   17487: 
1.1.1.25  root     17488: void dma_reset(int c)
1.1       root     17489: {
1.1.1.25  root     17490:        dma[c].low_high = false;
                   17491:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   17492:        dma[c].mask = 0xff;
                   17493: }
                   17494: 
                   17495: void dma_write(int c, UINT32 addr, UINT8 data)
                   17496: {
                   17497:        int ch = (addr >> 1) & 3;
                   17498:        UINT8 bit = 1 << (data & 3);
                   17499:        
                   17500:        switch(addr & 0x0f) {
                   17501:        case 0x00: case 0x02: case 0x04: case 0x06:
                   17502:                if(dma[c].low_high) {
                   17503:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     17504:                } else {
1.1.1.25  root     17505:                        dma[c].ch[ch].bareg.b.l = data;
                   17506:                }
                   17507:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   17508:                dma[c].low_high = !dma[c].low_high;
                   17509:                break;
                   17510:        case 0x01: case 0x03: case 0x05: case 0x07:
                   17511:                if(dma[c].low_high) {
                   17512:                        dma[c].ch[ch].bcreg.b.h = data;
                   17513:                } else {
                   17514:                        dma[c].ch[ch].bcreg.b.l = data;
                   17515:                }
                   17516:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   17517:                dma[c].low_high = !dma[c].low_high;
                   17518:                break;
                   17519:        case 0x08:
                   17520:                // command register
                   17521:                dma[c].cmd = data;
                   17522:                break;
                   17523:        case 0x09:
                   17524:                // dma[c].request register
                   17525:                if(data & 4) {
                   17526:                        if(!(dma[c].req & bit)) {
                   17527:                                dma[c].req |= bit;
                   17528: //                             dma_run(c, ch);
                   17529:                        }
                   17530:                } else {
                   17531:                        dma[c].req &= ~bit;
                   17532:                }
                   17533:                break;
                   17534:        case 0x0a:
                   17535:                // single mask register
                   17536:                if(data & 4) {
                   17537:                        dma[c].mask |= bit;
                   17538:                } else {
                   17539:                        dma[c].mask &= ~bit;
                   17540:                }
                   17541:                break;
                   17542:        case 0x0b:
                   17543:                // mode register
                   17544:                dma[c].ch[data & 3].mode = data;
                   17545:                break;
                   17546:        case 0x0c:
                   17547:                dma[c].low_high = false;
                   17548:                break;
                   17549:        case 0x0d:
                   17550:                // clear master
                   17551:                dma_reset(c);
                   17552:                break;
                   17553:        case 0x0e:
                   17554:                // clear mask register
                   17555:                dma[c].mask = 0;
                   17556:                break;
                   17557:        case 0x0f:
                   17558:                // all mask register
                   17559:                dma[c].mask = data & 0x0f;
                   17560:                break;
                   17561:        }
                   17562: }
                   17563: 
                   17564: UINT8 dma_read(int c, UINT32 addr)
                   17565: {
                   17566:        int ch = (addr >> 1) & 3;
                   17567:        UINT8 val = 0xff;
                   17568:        
                   17569:        switch(addr & 0x0f) {
                   17570:        case 0x00: case 0x02: case 0x04: case 0x06:
                   17571:                if(dma[c].low_high) {
                   17572:                        val = dma[c].ch[ch].areg.b.h;
                   17573:                } else {
                   17574:                        val = dma[c].ch[ch].areg.b.l;
                   17575:                }
                   17576:                dma[c].low_high = !dma[c].low_high;
                   17577:                return(val);
                   17578:        case 0x01: case 0x03: case 0x05: case 0x07:
                   17579:                if(dma[c].low_high) {
                   17580:                        val = dma[c].ch[ch].creg.b.h;
                   17581:                } else {
                   17582:                        val = dma[c].ch[ch].creg.b.l;
                   17583:                }
                   17584:                dma[c].low_high = !dma[c].low_high;
                   17585:                return(val);
                   17586:        case 0x08:
                   17587:                // status register
                   17588:                val = (dma[c].req << 4) | dma[c].tc;
                   17589:                dma[c].tc = 0;
                   17590:                return(val);
                   17591:        case 0x0d:
1.1.1.26  root     17592:                // temporary register (intel 82374 does not support)
1.1.1.25  root     17593:                return(dma[c].tmp & 0xff);
1.1.1.26  root     17594:        case 0x0f:
                   17595:                // mask register (intel 82374 does support)
                   17596:                return(dma[c].mask);
1.1.1.25  root     17597:        }
                   17598:        return(0xff);
                   17599: }
                   17600: 
                   17601: void dma_page_write(int c, int ch, UINT8 data)
                   17602: {
                   17603:        dma[c].ch[ch].pagereg = data;
                   17604: }
                   17605: 
                   17606: UINT8 dma_page_read(int c, int ch)
                   17607: {
                   17608:        return(dma[c].ch[ch].pagereg);
                   17609: }
                   17610: 
                   17611: void dma_run(int c, int ch)
                   17612: {
                   17613:        UINT8 bit = 1 << ch;
                   17614:        
                   17615:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   17616:                // execute dma
                   17617:                while(dma[c].req & bit) {
                   17618:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   17619:                                // memory -> memory
                   17620:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   17621:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   17622:                                
                   17623:                                if(c == 0) {
                   17624:                                        dma[c].tmp = read_byte(saddr);
                   17625:                                        write_byte(daddr, dma[c].tmp);
                   17626:                                } else {
                   17627:                                        dma[c].tmp = read_word(saddr << 1);
                   17628:                                        write_word(daddr << 1, dma[c].tmp);
                   17629:                                }
                   17630:                                if(!(dma[c].cmd & 0x02)) {
                   17631:                                        if(dma[c].ch[0].mode & 0x20) {
                   17632:                                                dma[c].ch[0].areg.w--;
                   17633:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   17634:                                                        dma[c].ch[0].pagereg--;
                   17635:                                                }
                   17636:                                        } else {
                   17637:                                                dma[c].ch[0].areg.w++;
                   17638:                                                if(dma[c].ch[0].areg.w == 0) {
                   17639:                                                        dma[c].ch[0].pagereg++;
                   17640:                                                }
                   17641:                                        }
                   17642:                                }
                   17643:                                if(dma[c].ch[1].mode & 0x20) {
                   17644:                                        dma[c].ch[1].areg.w--;
                   17645:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   17646:                                                dma[c].ch[1].pagereg--;
                   17647:                                        }
                   17648:                                } else {
                   17649:                                        dma[c].ch[1].areg.w++;
                   17650:                                        if(dma[c].ch[1].areg.w == 0) {
                   17651:                                                dma[c].ch[1].pagereg++;
                   17652:                                        }
                   17653:                                }
                   17654:                                
                   17655:                                // check dma condition
                   17656:                                if(dma[c].ch[0].creg.w-- == 0) {
                   17657:                                        if(dma[c].ch[0].mode & 0x10) {
                   17658:                                                // self initialize
                   17659:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   17660:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   17661:                                        } else {
                   17662: //                                             dma[c].mask |= bit;
                   17663:                                        }
                   17664:                                }
                   17665:                                if(dma[c].ch[1].creg.w-- == 0) {
                   17666:                                        // terminal count
                   17667:                                        if(dma[c].ch[1].mode & 0x10) {
                   17668:                                                // self initialize
                   17669:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   17670:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   17671:                                        } else {
                   17672:                                                dma[c].mask |= bit;
                   17673:                                        }
                   17674:                                        dma[c].req &= ~bit;
                   17675:                                        dma[c].tc |= bit;
                   17676:                                }
                   17677:                        } else {
                   17678:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   17679:                                
                   17680:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   17681:                                        // verify
                   17682:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   17683:                                        // io -> memory
                   17684:                                        if(c == 0) {
                   17685:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   17686:                                                write_byte(addr, dma[c].tmp);
                   17687:                                        } else {
                   17688:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   17689:                                                write_word(addr << 1, dma[c].tmp);
                   17690:                                        }
                   17691:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   17692:                                        // memory -> io
                   17693:                                        if(c == 0) {
                   17694:                                                dma[c].tmp = read_byte(addr);
                   17695:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   17696:                                        } else {
                   17697:                                                dma[c].tmp = read_word(addr << 1);
                   17698:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   17699:                                        }
                   17700:                                }
                   17701:                                if(dma[c].ch[ch].mode & 0x20) {
                   17702:                                        dma[c].ch[ch].areg.w--;
                   17703:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   17704:                                                dma[c].ch[ch].pagereg--;
                   17705:                                        }
                   17706:                                } else {
                   17707:                                        dma[c].ch[ch].areg.w++;
                   17708:                                        if(dma[c].ch[ch].areg.w == 0) {
                   17709:                                                dma[c].ch[ch].pagereg++;
                   17710:                                        }
                   17711:                                }
                   17712:                                
                   17713:                                // check dma condition
                   17714:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   17715:                                        // terminal count
                   17716:                                        if(dma[c].ch[ch].mode & 0x10) {
                   17717:                                                // self initialize
                   17718:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   17719:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   17720:                                        } else {
                   17721:                                                dma[c].mask |= bit;
                   17722:                                        }
                   17723:                                        dma[c].req &= ~bit;
                   17724:                                        dma[c].tc |= bit;
                   17725:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   17726:                                        // single mode
                   17727:                                        break;
                   17728:                                }
                   17729:                        }
                   17730:                }
                   17731:        }
                   17732: }
                   17733: 
                   17734: // pic
                   17735: 
                   17736: void pic_init()
                   17737: {
                   17738:        memset(pic, 0, sizeof(pic));
                   17739:        pic[0].imr = pic[1].imr = 0xff;
                   17740:        
                   17741:        // from bochs bios
                   17742:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   17743:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   17744:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   17745:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   17746:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   17747:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   17748:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   17749:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   17750:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   17751: }
                   17752: 
                   17753: void pic_write(int c, UINT32 addr, UINT8 data)
                   17754: {
                   17755:        if(addr & 1) {
                   17756:                if(pic[c].icw2_r) {
                   17757:                        // icw2
                   17758:                        pic[c].icw2 = data;
                   17759:                        pic[c].icw2_r = 0;
                   17760:                } else if(pic[c].icw3_r) {
                   17761:                        // icw3
                   17762:                        pic[c].icw3 = data;
                   17763:                        pic[c].icw3_r = 0;
                   17764:                } else if(pic[c].icw4_r) {
                   17765:                        // icw4
                   17766:                        pic[c].icw4 = data;
                   17767:                        pic[c].icw4_r = 0;
                   17768:                } else {
                   17769:                        // ocw1
1.1       root     17770:                        pic[c].imr = data;
                   17771:                }
                   17772:        } else {
                   17773:                if(data & 0x10) {
                   17774:                        // icw1
                   17775:                        pic[c].icw1 = data;
                   17776:                        pic[c].icw2_r = 1;
                   17777:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   17778:                        pic[c].icw4_r = data & 1;
                   17779:                        pic[c].irr = 0;
                   17780:                        pic[c].isr = 0;
                   17781:                        pic[c].imr = 0;
                   17782:                        pic[c].prio = 0;
                   17783:                        if(!(pic[c].icw1 & 1)) {
                   17784:                                pic[c].icw4 = 0;
                   17785:                        }
                   17786:                        pic[c].ocw3 = 0;
                   17787:                } else if(data & 8) {
                   17788:                        // ocw3
                   17789:                        if(!(data & 2)) {
                   17790:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   17791:                        }
                   17792:                        if(!(data & 0x40)) {
                   17793:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   17794:                        }
                   17795:                        pic[c].ocw3 = data;
                   17796:                } else {
                   17797:                        // ocw2
                   17798:                        int level = 0;
                   17799:                        if(data & 0x40) {
                   17800:                                level = data & 7;
                   17801:                        } else {
                   17802:                                if(!pic[c].isr) {
                   17803:                                        return;
                   17804:                                }
                   17805:                                level = pic[c].prio;
                   17806:                                while(!(pic[c].isr & (1 << level))) {
                   17807:                                        level = (level + 1) & 7;
                   17808:                                }
                   17809:                        }
                   17810:                        if(data & 0x80) {
                   17811:                                pic[c].prio = (level + 1) & 7;
                   17812:                        }
                   17813:                        if(data & 0x20) {
                   17814:                                pic[c].isr &= ~(1 << level);
                   17815:                        }
                   17816:                }
                   17817:        }
                   17818:        pic_update();
                   17819: }
                   17820: 
                   17821: UINT8 pic_read(int c, UINT32 addr)
                   17822: {
                   17823:        if(addr & 1) {
                   17824:                return(pic[c].imr);
                   17825:        } else {
                   17826:                // polling mode is not supported...
                   17827:                //if(pic[c].ocw3 & 4) {
                   17828:                //      return ???;
                   17829:                //}
                   17830:                if(pic[c].ocw3 & 1) {
                   17831:                        return(pic[c].isr);
                   17832:                } else {
                   17833:                        return(pic[c].irr);
                   17834:                }
                   17835:        }
                   17836: }
                   17837: 
                   17838: void pic_req(int c, int level, int signal)
                   17839: {
                   17840:        if(signal) {
                   17841:                pic[c].irr |= (1 << level);
                   17842:        } else {
                   17843:                pic[c].irr &= ~(1 << level);
                   17844:        }
                   17845:        pic_update();
                   17846: }
                   17847: 
                   17848: int pic_ack()
                   17849: {
                   17850:        // ack (INTA=L)
                   17851:        pic[pic_req_chip].isr |= pic_req_bit;
                   17852:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   17853:        if(pic_req_chip > 0) {
                   17854:                // update isr and irr of master
                   17855:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   17856:                pic[pic_req_chip - 1].isr |= slave;
                   17857:                pic[pic_req_chip - 1].irr &= ~slave;
                   17858:        }
                   17859:        //if(pic[pic_req_chip].icw4 & 1) {
                   17860:                // 8086 mode
                   17861:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   17862:        //} else {
                   17863:        //      // 8080 mode
                   17864:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   17865:        //      if(pic[pic_req_chip].icw1 & 4) {
                   17866:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   17867:        //      } else {
                   17868:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   17869:        //      }
                   17870:        //      vector = 0xcd | (addr << 8);
                   17871:        //}
                   17872:        if(pic[pic_req_chip].icw4 & 2) {
                   17873:                // auto eoi
                   17874:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   17875:        }
                   17876:        return(vector);
                   17877: }
                   17878: 
                   17879: void pic_update()
                   17880: {
                   17881:        for(int c = 0; c < 2; c++) {
                   17882:                UINT8 irr = pic[c].irr;
                   17883:                if(c + 1 < 2) {
                   17884:                        // this is master
                   17885:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   17886:                                // request from slave
                   17887:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   17888:                        }
                   17889:                }
                   17890:                irr &= (~pic[c].imr);
                   17891:                if(!irr) {
                   17892:                        break;
                   17893:                }
                   17894:                if(!(pic[c].ocw3 & 0x20)) {
                   17895:                        irr |= pic[c].isr;
                   17896:                }
                   17897:                int level = pic[c].prio;
                   17898:                UINT8 bit = 1 << level;
                   17899:                while(!(irr & bit)) {
                   17900:                        level = (level + 1) & 7;
                   17901:                        bit = 1 << level;
                   17902:                }
                   17903:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   17904:                        // check slave
                   17905:                        continue;
                   17906:                }
                   17907:                if(pic[c].isr & bit) {
                   17908:                        break;
                   17909:                }
                   17910:                // interrupt request
                   17911:                pic_req_chip = c;
                   17912:                pic_req_level = level;
                   17913:                pic_req_bit = bit;
1.1.1.3   root     17914:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     17915:                return;
                   17916:        }
1.1.1.3   root     17917:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     17918: }
1.1       root     17919: 
1.1.1.25  root     17920: // pio
                   17921: 
                   17922: void pio_init()
                   17923: {
1.1.1.38  root     17924: //     bool conv_mode = (GetConsoleCP() == 932);
                   17925:        
1.1.1.26  root     17926:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     17927:        
1.1.1.25  root     17928:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     17929:                pio[c].stat = 0xdf;
1.1.1.25  root     17930:                pio[c].ctrl = 0x0c;
1.1.1.38  root     17931: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     17932:        }
                   17933: }
                   17934: 
1.1.1.37  root     17935: void pio_finish()
                   17936: {
                   17937:        pio_release();
                   17938: }
                   17939: 
                   17940: void pio_release()
                   17941: {
                   17942:        for(int c = 0; c < 2; c++) {
                   17943:                if(pio[c].fp != NULL) {
1.1.1.38  root     17944:                        if(pio[c].jis_mode) {
                   17945:                                fputc(0x1c, pio[c].fp);
                   17946:                                fputc(0x2e, pio[c].fp);
                   17947:                        }
1.1.1.37  root     17948:                        fclose(pio[c].fp);
                   17949:                        pio[c].fp = NULL;
                   17950:                }
                   17951:        }
                   17952: }
                   17953: 
1.1.1.25  root     17954: void pio_write(int c, UINT32 addr, UINT8 data)
                   17955: {
                   17956:        switch(addr & 3) {
                   17957:        case 0:
                   17958:                pio[c].data = data;
                   17959:                break;
                   17960:        case 2:
1.1.1.37  root     17961:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   17962:                        // strobe H -> L
                   17963:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   17964:                                // auto feed
                   17965:                                printer_out(c, 0x0d);
                   17966:                                printer_out(c, 0x0a);
                   17967:                        } else {
                   17968:                                printer_out(c, pio[c].data);
                   17969:                        }
                   17970:                        pio[c].stat &= ~0x40; // set ack
                   17971:                }
1.1.1.25  root     17972:                pio[c].ctrl = data;
                   17973:                break;
                   17974:        }
                   17975: }
                   17976: 
                   17977: UINT8 pio_read(int c, UINT32 addr)
                   17978: {
                   17979:        switch(addr & 3) {
                   17980:        case 0:
1.1.1.37  root     17981:                if(pio[c].ctrl & 0x20) {
                   17982:                        // input mode
                   17983:                        return(0xff);
                   17984:                }
1.1.1.25  root     17985:                return(pio[c].data);
                   17986:        case 1:
1.1.1.37  root     17987:                {
                   17988:                        UINT8 stat = pio[c].stat;
                   17989:                        pio[c].stat |= 0x40; // clear ack
                   17990:                        return(stat);
                   17991:                }
1.1.1.25  root     17992:        case 2:
                   17993:                return(pio[c].ctrl);
                   17994:        }
                   17995:        return(0xff);
                   17996: }
                   17997: 
1.1.1.37  root     17998: void printer_out(int c, UINT8 data)
                   17999: {
                   18000:        SYSTEMTIME time;
1.1.1.38  root     18001:        bool jis_mode = false;
1.1.1.37  root     18002:        
                   18003:        GetLocalTime(&time);
                   18004:        
                   18005:        if(pio[c].fp != NULL) {
                   18006:                // if at least 1000ms passed from last written, close the current file
                   18007:                FILETIME ftime1;
                   18008:                FILETIME ftime2;
                   18009:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   18010:                SystemTimeToFileTime(&time, &ftime2);
                   18011:                INT64 *time1 = (INT64 *)&ftime1;
                   18012:                INT64 *time2 = (INT64 *)&ftime2;
                   18013:                INT64 msec = (*time2 - *time1) / 10000;
                   18014:                
                   18015:                if(msec >= 1000) {
1.1.1.38  root     18016:                        if(pio[c].jis_mode) {
                   18017:                                fputc(0x1c, pio[c].fp);
                   18018:                                fputc(0x2e, pio[c].fp);
                   18019:                                jis_mode = true;
                   18020:                        }
1.1.1.37  root     18021:                        fclose(pio[c].fp);
                   18022:                        pio[c].fp = NULL;
                   18023:                }
                   18024:        }
                   18025:        if(pio[c].fp == NULL) {
                   18026:                // create a new file in the temp folder
                   18027:                char file_name[MAX_PATH];
                   18028:                
                   18029:                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);
                   18030:                if(GetTempPath(MAX_PATH, pio[c].path)) {
                   18031:                        strcat(pio[c].path, file_name);
                   18032:                } else {
                   18033:                        strcpy(pio[c].path, file_name);
                   18034:                }
1.1.1.38  root     18035:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     18036:        }
                   18037:        if(pio[c].fp != NULL) {
1.1.1.38  root     18038:                if(jis_mode) {
                   18039:                        fputc(0x1c, pio[c].fp);
                   18040:                        fputc(0x26, pio[c].fp);
                   18041:                }
1.1.1.37  root     18042:                fputc(data, pio[c].fp);
1.1.1.38  root     18043:                
                   18044:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   18045:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   18046:                        UINT8 buffer[4];
                   18047:                        fseek(pio[c].fp, 0, SEEK_SET);
                   18048:                        fread(buffer, 4, 1, pio[c].fp);
                   18049:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   18050:                                fclose(pio[c].fp);
                   18051:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   18052:                        }
                   18053:                }
1.1.1.37  root     18054:                pio[c].time = time;
                   18055:        }
                   18056: }
                   18057: 
1.1       root     18058: // pit
                   18059: 
1.1.1.22  root     18060: #define PIT_FREQ 1193182ULL
1.1       root     18061: #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)
                   18062: 
                   18063: void pit_init()
                   18064: {
1.1.1.8   root     18065:        memset(pit, 0, sizeof(pit));
1.1       root     18066:        for(int ch = 0; ch < 3; ch++) {
                   18067:                pit[ch].count = 0x10000;
                   18068:                pit[ch].ctrl_reg = 0x34;
                   18069:                pit[ch].mode = 3;
                   18070:        }
                   18071:        
                   18072:        // from bochs bios
                   18073:        pit_write(3, 0x34);
                   18074:        pit_write(0, 0x00);
                   18075:        pit_write(0, 0x00);
                   18076: }
                   18077: 
                   18078: void pit_write(int ch, UINT8 val)
                   18079: {
1.1.1.8   root     18080: #ifndef PIT_ALWAYS_RUNNING
1.1       root     18081:        if(!pit_active) {
                   18082:                pit_active = 1;
                   18083:                pit_init();
                   18084:        }
1.1.1.8   root     18085: #endif
1.1       root     18086:        switch(ch) {
                   18087:        case 0:
                   18088:        case 1:
                   18089:        case 2:
                   18090:                // write count register
                   18091:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   18092:                        if(pit[ch].ctrl_reg & 0x10) {
                   18093:                                pit[ch].low_write = 1;
                   18094:                        }
                   18095:                        if(pit[ch].ctrl_reg & 0x20) {
                   18096:                                pit[ch].high_write = 1;
                   18097:                        }
                   18098:                }
                   18099:                if(pit[ch].low_write) {
                   18100:                        pit[ch].count_reg = val;
                   18101:                        pit[ch].low_write = 0;
                   18102:                } else if(pit[ch].high_write) {
                   18103:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   18104:                                pit[ch].count_reg = val << 8;
                   18105:                        } else {
                   18106:                                pit[ch].count_reg |= val << 8;
                   18107:                        }
                   18108:                        pit[ch].high_write = 0;
                   18109:                }
                   18110:                // start count
1.1.1.8   root     18111:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   18112:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   18113:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   18114:                                pit[ch].prev_time = timeGetTime();
                   18115:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     18116:                        }
                   18117:                }
                   18118:                break;
                   18119:        case 3: // ctrl reg
                   18120:                if((val & 0xc0) == 0xc0) {
                   18121:                        // i8254 read-back command
                   18122:                        for(ch = 0; ch < 3; ch++) {
                   18123:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   18124:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   18125:                                        pit[ch].status_latched = 1;
                   18126:                                }
                   18127:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   18128:                                        pit_latch_count(ch);
                   18129:                                }
                   18130:                        }
                   18131:                        break;
                   18132:                }
                   18133:                ch = (val >> 6) & 3;
                   18134:                if(val & 0x30) {
1.1.1.35  root     18135:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     18136:                        pit[ch].mode = modes[(val >> 1) & 7];
                   18137:                        pit[ch].count_latched = 0;
                   18138:                        pit[ch].low_read = pit[ch].high_read = 0;
                   18139:                        pit[ch].low_write = pit[ch].high_write = 0;
                   18140:                        pit[ch].ctrl_reg = val;
                   18141:                        // stop count
1.1.1.8   root     18142:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     18143:                        pit[ch].count_reg = 0;
                   18144:                } else if(!pit[ch].count_latched) {
                   18145:                        pit_latch_count(ch);
                   18146:                }
                   18147:                break;
                   18148:        }
                   18149: }
                   18150: 
                   18151: UINT8 pit_read(int ch)
                   18152: {
1.1.1.8   root     18153: #ifndef PIT_ALWAYS_RUNNING
1.1       root     18154:        if(!pit_active) {
                   18155:                pit_active = 1;
                   18156:                pit_init();
                   18157:        }
1.1.1.8   root     18158: #endif
1.1       root     18159:        switch(ch) {
                   18160:        case 0:
                   18161:        case 1:
                   18162:        case 2:
                   18163:                if(pit[ch].status_latched) {
                   18164:                        pit[ch].status_latched = 0;
                   18165:                        return(pit[ch].status);
                   18166:                }
                   18167:                // if not latched, through current count
                   18168:                if(!pit[ch].count_latched) {
                   18169:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   18170:                                pit_latch_count(ch);
                   18171:                        }
                   18172:                }
                   18173:                // return latched count
                   18174:                if(pit[ch].low_read) {
                   18175:                        pit[ch].low_read = 0;
                   18176:                        if(!pit[ch].high_read) {
                   18177:                                pit[ch].count_latched = 0;
                   18178:                        }
                   18179:                        return(pit[ch].latch & 0xff);
                   18180:                } else if(pit[ch].high_read) {
                   18181:                        pit[ch].high_read = 0;
                   18182:                        pit[ch].count_latched = 0;
                   18183:                        return((pit[ch].latch >> 8) & 0xff);
                   18184:                }
                   18185:        }
                   18186:        return(0xff);
                   18187: }
                   18188: 
1.1.1.8   root     18189: int pit_run(int ch, UINT32 cur_time)
1.1       root     18190: {
1.1.1.8   root     18191:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     18192:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     18193:                pit[ch].prev_time = pit[ch].expired_time;
                   18194:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   18195:                if(cur_time >= pit[ch].expired_time) {
                   18196:                        pit[ch].prev_time = cur_time;
                   18197:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     18198:                }
1.1.1.8   root     18199:                return(1);
1.1       root     18200:        }
1.1.1.8   root     18201:        return(0);
1.1       root     18202: }
                   18203: 
                   18204: void pit_latch_count(int ch)
                   18205: {
1.1.1.8   root     18206:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     18207:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     18208:                pit_run(ch, cur_time);
                   18209:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     18210:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   18211:                
                   18212:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   18213:                        // decrement counter in 1msec period
                   18214:                        if(pit[ch].next_latch == 0) {
                   18215:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   18216:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   18217:                        }
                   18218:                        if(pit[ch].latch > pit[ch].next_latch) {
                   18219:                                pit[ch].latch--;
                   18220:                        }
                   18221:                } else {
                   18222:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   18223:                        pit[ch].next_latch = 0;
                   18224:                }
1.1.1.8   root     18225:        } else {
                   18226:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     18227:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     18228:        }
                   18229:        pit[ch].count_latched = 1;
                   18230:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   18231:                // lower byte
                   18232:                pit[ch].low_read = 1;
                   18233:                pit[ch].high_read = 0;
                   18234:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   18235:                // upper byte
                   18236:                pit[ch].low_read = 0;
                   18237:                pit[ch].high_read = 1;
                   18238:        } else {
                   18239:                // lower -> upper
1.1.1.14  root     18240:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     18241:        }
                   18242: }
                   18243: 
1.1.1.8   root     18244: int pit_get_expired_time(int ch)
1.1       root     18245: {
1.1.1.22  root     18246:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   18247:        UINT64 val = pit[ch].accum >> 10;
                   18248:        pit[ch].accum -= val << 10;
                   18249:        return((val != 0) ? val : 1);
1.1.1.8   root     18250: }
                   18251: 
1.1.1.25  root     18252: // sio
                   18253: 
                   18254: void sio_init()
                   18255: {
1.1.1.26  root     18256:        memset(sio, 0, sizeof(sio));
                   18257:        memset(sio_mt, 0, sizeof(sio_mt));
                   18258:        
1.1.1.29  root     18259:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     18260:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   18261:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   18262:                
                   18263:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   18264:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     18265:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   18266:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     18267:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   18268:                sio[c].irq_identify = 0x01;     // no pending irq
                   18269:                
                   18270:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   18271:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   18272:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   18273:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   18274:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   18275:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   18276:                
1.1.1.26  root     18277:                if(sio_port_number[c] != 0) {
1.1.1.25  root     18278:                        sio[c].channel = c;
                   18279:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   18280:                }
                   18281:        }
                   18282: }
                   18283: 
                   18284: void sio_finish()
                   18285: {
1.1.1.29  root     18286:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     18287:                if(sio_mt[c].hThread != NULL) {
                   18288:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   18289:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     18290:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     18291:                }
                   18292:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   18293:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   18294:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   18295:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   18296:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   18297:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     18298:        }
                   18299:        sio_release();
                   18300: }
                   18301: 
                   18302: void sio_release()
                   18303: {
1.1.1.29  root     18304:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     18305:                // sio_thread() may access the resources :-(
1.1.1.32  root     18306:                bool running = (sio_mt[c].hThread != NULL);
                   18307:                
                   18308:                if(running) {
                   18309:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   18310:                }
                   18311:                if(sio[c].send_buffer != NULL) {
                   18312:                        sio[c].send_buffer->release();
                   18313:                        delete sio[c].send_buffer;
                   18314:                        sio[c].send_buffer = NULL;
                   18315:                }
                   18316:                if(running) {
                   18317:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   18318:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   18319:                }
                   18320:                if(sio[c].recv_buffer != NULL) {
                   18321:                        sio[c].recv_buffer->release();
                   18322:                        delete sio[c].recv_buffer;
                   18323:                        sio[c].recv_buffer = NULL;
                   18324:                }
                   18325:                if(running) {
                   18326:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     18327:                }
1.1.1.25  root     18328:        }
                   18329: }
                   18330: 
                   18331: void sio_write(int c, UINT32 addr, UINT8 data)
                   18332: {
                   18333:        switch(addr & 7) {
                   18334:        case 0:
                   18335:                if(sio[c].selector & 0x80) {
                   18336:                        if(sio[c].divisor.b.l != data) {
                   18337:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   18338:                                sio[c].divisor.b.l = data;
                   18339:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   18340:                        }
                   18341:                } else {
                   18342:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     18343:                        if(sio[c].send_buffer != NULL) {
                   18344:                                sio[c].send_buffer->write(data);
                   18345:                        }
1.1.1.25  root     18346:                        // transmitter holding/shift registers are not empty
                   18347:                        sio[c].line_stat_buf &= ~0x60;
                   18348:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   18349:                        
                   18350:                        if(sio[c].irq_enable & 0x02) {
                   18351:                                sio_update_irq(c);
                   18352:                        }
                   18353:                }
                   18354:                break;
                   18355:        case 1:
                   18356:                if(sio[c].selector & 0x80) {
                   18357:                        if(sio[c].divisor.b.h != data) {
                   18358:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   18359:                                sio[c].divisor.b.h = data;
                   18360:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   18361:                        }
                   18362:                } else {
                   18363:                        if(sio[c].irq_enable != data) {
                   18364:                                sio[c].irq_enable = data;
                   18365:                                sio_update_irq(c);
                   18366:                        }
                   18367:                }
                   18368:                break;
                   18369:        case 3:
                   18370:                {
                   18371:                        UINT8 line_ctrl = data & 0x3f;
                   18372:                        bool set_brk = ((data & 0x40) != 0);
                   18373:                        
                   18374:                        if(sio[c].line_ctrl != line_ctrl) {
                   18375:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   18376:                                sio[c].line_ctrl = line_ctrl;
                   18377:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   18378:                        }
                   18379:                        if(sio[c].set_brk != set_brk) {
                   18380:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   18381:                                sio[c].set_brk = set_brk;
                   18382:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   18383:                        }
                   18384:                }
                   18385:                sio[c].selector = data;
                   18386:                break;
                   18387:        case 4:
                   18388:                {
                   18389:                        bool set_dtr = ((data & 0x01) != 0);
                   18390:                        bool set_rts = ((data & 0x02) != 0);
                   18391:                        
                   18392:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     18393: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     18394:                                sio[c].set_dtr = set_dtr;
                   18395:                                sio[c].set_rts = set_rts;
1.1.1.26  root     18396: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   18397:                                
                   18398:                                bool state_changed = false;
                   18399:                                
                   18400:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   18401:                                if(set_dtr) {
                   18402:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   18403:                                } else {
                   18404:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   18405:                                }
                   18406:                                if(set_rts) {
                   18407:                                        sio[c].modem_stat |= 0x10;      // cts on
                   18408:                                } else {
                   18409:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   18410:                                }
                   18411:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   18412:                                        if(!(sio[c].modem_stat & 0x02)) {
                   18413:                                                if(sio[c].irq_enable & 0x08) {
                   18414:                                                        state_changed = true;
                   18415:                                                }
                   18416:                                                sio[c].modem_stat |= 0x02;
                   18417:                                        }
                   18418:                                }
                   18419:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   18420:                                        if(!(sio[c].modem_stat & 0x01)) {
                   18421:                                                if(sio[c].irq_enable & 0x08) {
                   18422:                                                        state_changed = true;
                   18423:                                                }
                   18424:                                                sio[c].modem_stat |= 0x01;
                   18425:                                        }
                   18426:                                }
                   18427:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   18428:                                
                   18429:                                if(state_changed) {
                   18430:                                        sio_update_irq(c);
                   18431:                                }
1.1.1.25  root     18432:                        }
                   18433:                }
                   18434:                sio[c].modem_ctrl = data;
                   18435:                break;
                   18436:        case 7:
                   18437:                sio[c].scratch = data;
                   18438:                break;
                   18439:        }
                   18440: }
                   18441: 
                   18442: UINT8 sio_read(int c, UINT32 addr)
                   18443: {
                   18444:        switch(addr & 7) {
                   18445:        case 0:
                   18446:                if(sio[c].selector & 0x80) {
                   18447:                        return(sio[c].divisor.b.l);
                   18448:                } else {
                   18449:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     18450:                        UINT8 data = 0;
                   18451:                        if(sio[c].recv_buffer != NULL) {
                   18452:                                data = sio[c].recv_buffer->read();
                   18453:                        }
1.1.1.25  root     18454:                        // data is not ready
                   18455:                        sio[c].line_stat_buf &= ~0x01;
                   18456:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   18457:                        
                   18458:                        if(sio[c].irq_enable & 0x01) {
                   18459:                                sio_update_irq(c);
                   18460:                        }
                   18461:                        return(data);
                   18462:                }
                   18463:        case 1:
                   18464:                if(sio[c].selector & 0x80) {
                   18465:                        return(sio[c].divisor.b.h);
                   18466:                } else {
                   18467:                        return(sio[c].irq_enable);
                   18468:                }
                   18469:        case 2:
                   18470:                return(sio[c].irq_identify);
                   18471:        case 3:
                   18472:                return(sio[c].selector);
                   18473:        case 4:
                   18474:                return(sio[c].modem_ctrl);
                   18475:        case 5:
                   18476:                {
                   18477:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   18478:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   18479:                        sio[c].line_stat_err = 0x00;
                   18480:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   18481:                        
                   18482:                        bool state_changed = false;
                   18483:                        
                   18484:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   18485:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     18486:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     18487:                                        // transmitter holding register will be empty first
                   18488:                                        if(sio[c].irq_enable & 0x02) {
                   18489:                                                state_changed = true;
                   18490:                                        }
                   18491:                                        sio[c].line_stat_buf |= 0x20;
                   18492:                                }
                   18493:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   18494:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   18495:                                // transmitter shift register will be empty later
                   18496:                                sio[c].line_stat_buf |= 0x40;
                   18497:                        }
                   18498:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   18499:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     18500:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     18501:                                        // data is ready
                   18502:                                        if(sio[c].irq_enable & 0x01) {
                   18503:                                                state_changed = true;
                   18504:                                        }
                   18505:                                        sio[c].line_stat_buf |= 0x01;
                   18506:                                }
                   18507:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   18508:                        }
                   18509:                        if(state_changed) {
                   18510:                                sio_update_irq(c);
                   18511:                        }
                   18512:                        return(val);
                   18513:                }
                   18514:        case 6:
                   18515:                {
                   18516:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   18517:                        UINT8 val = sio[c].modem_stat;
                   18518:                        sio[c].modem_stat &= 0xf0;
                   18519:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   18520:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   18521:                        
                   18522:                        if(sio[c].modem_ctrl & 0x10) {
                   18523:                                // loop-back
                   18524:                                val &= 0x0f;
                   18525:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   18526:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   18527:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   18528:                        }
                   18529:                        return(val);
                   18530:                }
                   18531:        case 7:
                   18532:                return(sio[c].scratch);
                   18533:        }
                   18534:        return(0xff);
                   18535: }
                   18536: 
                   18537: void sio_update(int c)
                   18538: {
                   18539:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   18540:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     18541:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     18542:                        // transmitter holding/shift registers will be empty
                   18543:                        sio[c].line_stat_buf |= 0x60;
                   18544:                }
                   18545:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   18546:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   18547:                // transmitter shift register will be empty
                   18548:                sio[c].line_stat_buf |= 0x40;
                   18549:        }
                   18550:        if(!(sio[c].line_stat_buf & 0x01)) {
                   18551:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     18552:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     18553:                        // data is ready
                   18554:                        sio[c].line_stat_buf |= 0x01;
                   18555:                }
                   18556:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   18557:        }
                   18558:        sio_update_irq(c);
                   18559: }
                   18560: 
                   18561: void sio_update_irq(int c)
                   18562: {
                   18563:        int level = -1;
                   18564:        
                   18565:        if(sio[c].irq_enable & 0x08) {
                   18566:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   18567:                if((sio[c].modem_stat & 0x0f) != 0) {
                   18568:                        level = 0;
                   18569:                }
                   18570:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   18571:        }
                   18572:        if(sio[c].irq_enable & 0x02) {
                   18573:                if(sio[c].line_stat_buf & 0x20) {
                   18574:                        level = 1;
                   18575:                }
                   18576:        }
                   18577:        if(sio[c].irq_enable & 0x01) {
                   18578:                if(sio[c].line_stat_buf & 0x01) {
                   18579:                        level = 2;
                   18580:                }
                   18581:        }
                   18582:        if(sio[c].irq_enable & 0x04) {
                   18583:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   18584:                if(sio[c].line_stat_err != 0) {
                   18585:                        level = 3;
                   18586:                }
                   18587:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   18588:        }
1.1.1.29  root     18589:        
                   18590:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     18591:        if(level != -1) {
                   18592:                sio[c].irq_identify = level << 1;
1.1.1.29  root     18593:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     18594:        } else {
                   18595:                sio[c].irq_identify = 1;
1.1.1.29  root     18596:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     18597:        }
                   18598: }
                   18599: 
                   18600: DWORD WINAPI sio_thread(void *lpx)
                   18601: {
                   18602:        volatile sio_t *p = (sio_t *)lpx;
                   18603:        sio_mt_t *q = &sio_mt[p->channel];
                   18604:        
                   18605:        char name[] = "COM1";
1.1.1.26  root     18606:        name[3] = '0' + sio_port_number[p->channel];
                   18607:        HANDLE hComm = NULL;
                   18608:        COMMPROP commProp;
                   18609:        DCB dcb;
                   18610:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   18611:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   18612:        
                   18613:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   18614:                if(GetCommProperties(hComm, &commProp)) {
                   18615:                        dwSettableBaud = commProp.dwSettableBaud;
                   18616:                }
1.1.1.25  root     18617:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     18618: //             EscapeCommFunction(hComm, SETRTS);
                   18619: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     18620:                
                   18621:                while(!m_halted) {
                   18622:                        // setup comm port
                   18623:                        bool comm_state_changed = false;
                   18624:                        
                   18625:                        EnterCriticalSection(&q->csLineCtrl);
                   18626:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   18627:                                p->prev_divisor = p->divisor.w;
                   18628:                                p->prev_line_ctrl = p->line_ctrl;
                   18629:                                comm_state_changed = true;
                   18630:                        }
                   18631:                        LeaveCriticalSection(&q->csLineCtrl);
                   18632:                        
                   18633:                        if(comm_state_changed) {
1.1.1.26  root     18634:                                if(GetCommState(hComm, &dcb)) {
                   18635: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   18636:                                        DWORD baud = 115200 / p->prev_divisor;
                   18637:                                        dcb.BaudRate = 9600; // default
                   18638:                                        
                   18639:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   18640:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   18641:                                        // 134.5bps is not supported ???
                   18642: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   18643:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   18644:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   18645:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   18646:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   18647:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   18648:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   18649:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   18650:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   18651:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   18652: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   18653: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   18654: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   18655:                                        
                   18656:                                        switch(p->prev_line_ctrl & 0x03) {
                   18657:                                        case 0x00: dcb.ByteSize = 5; break;
                   18658:                                        case 0x01: dcb.ByteSize = 6; break;
                   18659:                                        case 0x02: dcb.ByteSize = 7; break;
                   18660:                                        case 0x03: dcb.ByteSize = 8; break;
                   18661:                                        }
                   18662:                                        switch(p->prev_line_ctrl & 0x04) {
                   18663:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   18664:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   18665:                                        }
                   18666:                                        switch(p->prev_line_ctrl & 0x38) {
                   18667:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   18668:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   18669:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   18670:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   18671:                                        default:   dcb.Parity = NOPARITY;    break;
                   18672:                                        }
                   18673:                                        dcb.fBinary = TRUE;
                   18674:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   18675:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   18676:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   18677:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   18678:                                        dcb.fTXContinueOnXoff = TRUE;
                   18679:                                        dcb.fOutX = dcb.fInX = FALSE;
                   18680:                                        dcb.fErrorChar = FALSE;
                   18681:                                        dcb.fNull = FALSE;
                   18682:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   18683:                                        dcb.fAbortOnError = FALSE;
                   18684:                                        
                   18685:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     18686:                                }
                   18687:                                
                   18688:                                // check again to apply all comm state changes
                   18689:                                Sleep(10);
                   18690:                                continue;
                   18691:                        }
                   18692:                        
                   18693:                        // set comm pins
                   18694:                        bool change_brk = false;
1.1.1.26  root     18695: //                     bool change_rts = false;
                   18696: //                     bool change_dtr = false;
1.1.1.25  root     18697:                        
                   18698:                        EnterCriticalSection(&q->csModemCtrl);
                   18699:                        if(p->prev_set_brk != p->set_brk) {
                   18700:                                p->prev_set_brk = p->set_brk;
                   18701:                                change_brk = true;
                   18702:                        }
1.1.1.26  root     18703: //                     if(p->prev_set_rts != p->set_rts) {
                   18704: //                             p->prev_set_rts = p->set_rts;
                   18705: //                             change_rts = true;
                   18706: //                     }
                   18707: //                     if(p->prev_set_dtr != p->set_dtr) {
                   18708: //                             p->prev_set_dtr = p->set_dtr;
                   18709: //                             change_dtr = true;
                   18710: //                     }
1.1.1.25  root     18711:                        LeaveCriticalSection(&q->csModemCtrl);
                   18712:                        
                   18713:                        if(change_brk) {
1.1.1.26  root     18714:                                static UINT32 clear_time = 0;
                   18715:                                if(p->prev_set_brk) {
                   18716:                                        EscapeCommFunction(hComm, SETBREAK);
                   18717:                                        clear_time = timeGetTime() + 200;
                   18718:                                } else {
                   18719:                                        // keep break for at least 200msec
                   18720:                                        UINT32 cur_time = timeGetTime();
                   18721:                                        if(clear_time > cur_time) {
                   18722:                                                Sleep(clear_time - cur_time);
                   18723:                                        }
                   18724:                                        EscapeCommFunction(hComm, CLRBREAK);
                   18725:                                }
1.1.1.25  root     18726:                        }
1.1.1.26  root     18727: //                     if(change_rts) {
                   18728: //                             if(p->prev_set_rts) {
                   18729: //                                     EscapeCommFunction(hComm, SETRTS);
                   18730: //                             } else {
                   18731: //                                     EscapeCommFunction(hComm, CLRRTS);
                   18732: //                             }
                   18733: //                     }
                   18734: //                     if(change_dtr) {
                   18735: //                             if(p->prev_set_dtr) {
                   18736: //                                     EscapeCommFunction(hComm, SETDTR);
                   18737: //                             } else {
                   18738: //                                     EscapeCommFunction(hComm, CLRDTR);
                   18739: //                             }
                   18740: //                     }
1.1.1.25  root     18741:                        
                   18742:                        // get comm pins
                   18743:                        DWORD dwModemStat = 0;
                   18744:                        
                   18745:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   18746:                                EnterCriticalSection(&q->csModemStat);
                   18747:                                if(dwModemStat & MS_RLSD_ON) {
                   18748:                                        p->modem_stat |= 0x80;
                   18749:                                } else {
                   18750:                                        p->modem_stat &= ~0x80;
                   18751:                                }
                   18752:                                if(dwModemStat & MS_RING_ON) {
                   18753:                                        p->modem_stat |= 0x40;
                   18754:                                } else {
                   18755:                                        p->modem_stat &= ~0x40;
                   18756:                                }
1.1.1.26  root     18757: //                             if(dwModemStat & MS_DSR_ON) {
                   18758: //                                     p->modem_stat |= 0x20;
                   18759: //                             } else {
                   18760: //                                     p->modem_stat &= ~0x20;
                   18761: //                             }
                   18762: //                             if(dwModemStat & MS_CTS_ON) {
                   18763: //                                     p->modem_stat |= 0x10;
                   18764: //                             } else {
                   18765: //                                     p->modem_stat &= ~0x10;
                   18766: //                             }
1.1.1.25  root     18767:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   18768:                                        p->modem_stat |= 0x08;
                   18769:                                }
                   18770:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   18771:                                        p->modem_stat |= 0x04;
                   18772:                                }
1.1.1.26  root     18773: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   18774: //                                     p->modem_stat |= 0x02;
                   18775: //                             }
                   18776: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   18777: //                                     p->modem_stat |= 0x01;
                   18778: //                             }
1.1.1.25  root     18779:                                LeaveCriticalSection(&q->csModemStat);
                   18780:                        }
                   18781:                        
                   18782:                        // send data
                   18783:                        DWORD dwSend = 0;
                   18784:                        
                   18785:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     18786:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     18787:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   18788:                        }
                   18789:                        LeaveCriticalSection(&q->csSendData);
                   18790:                        
                   18791:                        if(dwSend != 0) {
                   18792:                                DWORD dwWritten = 0;
                   18793:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   18794:                        }
                   18795:                        
                   18796:                        // get line status and recv data
                   18797:                        DWORD dwLineStat = 0;
                   18798:                        COMSTAT comStat;
                   18799:                        
                   18800:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   18801:                                EnterCriticalSection(&q->csLineStat);
                   18802:                                if(dwLineStat & CE_BREAK) {
                   18803:                                        p->line_stat_err |= 0x10;
                   18804:                                }
                   18805:                                if(dwLineStat & CE_FRAME) {
                   18806:                                        p->line_stat_err |= 0x08;
                   18807:                                }
                   18808:                                if(dwLineStat & CE_RXPARITY) {
                   18809:                                        p->line_stat_err |= 0x04;
                   18810:                                }
                   18811:                                if(dwLineStat & CE_OVERRUN) {
                   18812:                                        p->line_stat_err |= 0x02;
                   18813:                                }
                   18814:                                LeaveCriticalSection(&q->csLineStat);
                   18815:                                
                   18816:                                if(comStat.cbInQue != 0) {
                   18817:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     18818:                                        DWORD dwRecv = 0;
                   18819:                                        if(p->recv_buffer != NULL) {
                   18820:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   18821:                                        }
1.1.1.25  root     18822:                                        LeaveCriticalSection(&q->csRecvData);
                   18823:                                        
                   18824:                                        if(dwRecv != 0) {
                   18825:                                                DWORD dwRead = 0;
                   18826:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   18827:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     18828:                                                        if(p->recv_buffer != NULL) {
                   18829:                                                                for(int i = 0; i < dwRead; i++) {
                   18830:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   18831:                                                                }
1.1.1.25  root     18832:                                                        }
                   18833:                                                        LeaveCriticalSection(&q->csRecvData);
                   18834:                                                }
                   18835:                                        }
                   18836:                                }
                   18837:                        }
                   18838:                        Sleep(10);
                   18839:                }
                   18840:                CloseHandle(hComm);
                   18841:        }
                   18842:        return 0;
                   18843: }
                   18844: 
1.1.1.8   root     18845: // cmos
                   18846: 
                   18847: void cmos_init()
                   18848: {
                   18849:        memset(cmos, 0, sizeof(cmos));
                   18850:        cmos_addr = 0;
1.1       root     18851:        
1.1.1.8   root     18852:        // from DOSBox
                   18853:        cmos_write(0x0a, 0x26);
                   18854:        cmos_write(0x0b, 0x02);
                   18855:        cmos_write(0x0d, 0x80);
1.1       root     18856: }
                   18857: 
1.1.1.8   root     18858: void cmos_write(int addr, UINT8 val)
1.1       root     18859: {
1.1.1.8   root     18860:        cmos[addr & 0x7f] = val;
                   18861: }
                   18862: 
                   18863: #define CMOS_GET_TIME() { \
                   18864:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   18865:        if(prev_sec != cur_sec) { \
                   18866:                GetLocalTime(&time); \
                   18867:                prev_sec = cur_sec; \
                   18868:        } \
1.1       root     18869: }
1.1.1.8   root     18870: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     18871: 
1.1.1.8   root     18872: UINT8 cmos_read(int addr)
1.1       root     18873: {
1.1.1.8   root     18874:        static SYSTEMTIME time;
                   18875:        static UINT32 prev_sec = 0;
1.1       root     18876:        
1.1.1.8   root     18877:        switch(addr & 0x7f) {
                   18878:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   18879:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   18880:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   18881:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   18882:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   18883:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   18884:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   18885: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   18886:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   18887:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   18888:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   18889:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   18890:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   18891:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   18892:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   18893:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     18894:        }
1.1.1.8   root     18895:        return(cmos[addr & 0x7f]);
1.1       root     18896: }
                   18897: 
1.1.1.7   root     18898: // kbd (a20)
                   18899: 
                   18900: void kbd_init()
                   18901: {
1.1.1.8   root     18902:        kbd_data = kbd_command = 0;
1.1.1.7   root     18903:        kbd_status = 0x18;
                   18904: }
                   18905: 
                   18906: UINT8 kbd_read_data()
                   18907: {
1.1.1.8   root     18908:        kbd_status &= ~1;
1.1.1.7   root     18909:        return(kbd_data);
                   18910: }
                   18911: 
                   18912: void kbd_write_data(UINT8 val)
                   18913: {
                   18914:        switch(kbd_command) {
                   18915:        case 0xd1:
                   18916:                i386_set_a20_line((val >> 1) & 1);
                   18917:                break;
                   18918:        }
                   18919:        kbd_command = 0;
1.1.1.8   root     18920:        kbd_status &= ~8;
1.1.1.7   root     18921: }
                   18922: 
                   18923: UINT8 kbd_read_status()
                   18924: {
                   18925:        return(kbd_status);
                   18926: }
                   18927: 
                   18928: void kbd_write_command(UINT8 val)
                   18929: {
                   18930:        switch(val) {
                   18931:        case 0xd0:
                   18932:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     18933:                kbd_status |= 1;
1.1.1.7   root     18934:                break;
                   18935:        case 0xdd:
                   18936:                i386_set_a20_line(0);
                   18937:                break;
                   18938:        case 0xdf:
                   18939:                i386_set_a20_line(1);
                   18940:                break;
1.1.1.26  root     18941:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   18942:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     18943:                if(!(val & 1)) {
1.1.1.8   root     18944:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     18945:                                // reset pic
                   18946:                                pic_init();
                   18947:                                pic[0].irr = pic[1].irr = 0x00;
                   18948:                                pic[0].imr = pic[1].imr = 0xff;
                   18949:                        }
                   18950:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     18951:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   18952:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   18953:                        i386_jmp_far(selector, address);
1.1.1.7   root     18954:                }
                   18955:                i386_set_a20_line((val >> 1) & 1);
                   18956:                break;
                   18957:        }
                   18958:        kbd_command = val;
1.1.1.8   root     18959:        kbd_status |= 8;
1.1.1.7   root     18960: }
                   18961: 
1.1.1.9   root     18962: // vga
                   18963: 
                   18964: UINT8 vga_read_status()
                   18965: {
                   18966:        // 60hz
                   18967:        static const int period[3] = {16, 17, 17};
                   18968:        static int index = 0;
                   18969:        UINT32 time = timeGetTime() % period[index];
                   18970:        
                   18971:        index = (index + 1) % 3;
1.1.1.14  root     18972:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     18973: }
                   18974: 
1.1       root     18975: // i/o bus
                   18976: 
1.1.1.29  root     18977: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   18978: //#define SW1US_PATCH
                   18979: 
1.1.1.25  root     18980: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     18981: #ifdef USE_DEBUGGER
1.1.1.25  root     18982: {
1.1.1.33  root     18983:        if(now_debugging) {
                   18984:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   18985:                        if(in_break_point.table[i].status == 1) {
                   18986:                                if(addr == in_break_point.table[i].addr) {
                   18987:                                        in_break_point.hit = i + 1;
                   18988:                                        now_suspended = true;
                   18989:                                        break;
                   18990:                                }
                   18991:                        }
                   18992:                }
1.1.1.25  root     18993:        }
1.1.1.33  root     18994:        return(debugger_read_io_byte(addr));
1.1.1.25  root     18995: }
1.1.1.33  root     18996: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     18997: #endif
1.1       root     18998: {
1.1.1.33  root     18999:        UINT8 val = 0xff;
                   19000:        
1.1       root     19001:        switch(addr) {
1.1.1.29  root     19002: #ifdef SW1US_PATCH
                   19003:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   19004:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     19005:                val = sio_read(0, addr - 1);
                   19006:                break;
1.1.1.29  root     19007: #else
1.1.1.25  root     19008:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   19009:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     19010:                val = dma_read(0, addr);
                   19011:                break;
1.1.1.29  root     19012: #endif
1.1.1.25  root     19013:        case 0x20: case 0x21:
1.1.1.33  root     19014:                val = pic_read(0, addr);
                   19015:                break;
1.1.1.25  root     19016:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     19017:                val = pit_read(addr & 0x03);
                   19018:                break;
1.1.1.7   root     19019:        case 0x60:
1.1.1.33  root     19020:                val = kbd_read_data();
                   19021:                break;
1.1.1.9   root     19022:        case 0x61:
1.1.1.33  root     19023:                val = system_port;
                   19024:                break;
1.1.1.7   root     19025:        case 0x64:
1.1.1.33  root     19026:                val = kbd_read_status();
                   19027:                break;
1.1       root     19028:        case 0x71:
1.1.1.33  root     19029:                val = cmos_read(cmos_addr);
                   19030:                break;
1.1.1.25  root     19031:        case 0x81:
1.1.1.33  root     19032:                val = dma_page_read(0, 2);
                   19033:                break;
1.1.1.25  root     19034:        case 0x82:
1.1.1.33  root     19035:                val = dma_page_read(0, 3);
                   19036:                break;
1.1.1.25  root     19037:        case 0x83:
1.1.1.33  root     19038:                val = dma_page_read(0, 1);
                   19039:                break;
1.1.1.25  root     19040:        case 0x87:
1.1.1.33  root     19041:                val = dma_page_read(0, 0);
                   19042:                break;
1.1.1.25  root     19043:        case 0x89:
1.1.1.33  root     19044:                val = dma_page_read(1, 2);
                   19045:                break;
1.1.1.25  root     19046:        case 0x8a:
1.1.1.33  root     19047:                val = dma_page_read(1, 3);
                   19048:                break;
1.1.1.25  root     19049:        case 0x8b:
1.1.1.33  root     19050:                val = dma_page_read(1, 1);
                   19051:                break;
1.1.1.25  root     19052:        case 0x8f:
1.1.1.33  root     19053:                val = dma_page_read(1, 0);
                   19054:                break;
1.1       root     19055:        case 0x92:
1.1.1.33  root     19056:                val = (m_a20_mask >> 19) & 2;
                   19057:                break;
1.1.1.25  root     19058:        case 0xa0: case 0xa1:
1.1.1.33  root     19059:                val = pic_read(1, addr);
                   19060:                break;
1.1.1.25  root     19061:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   19062:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     19063:                val = dma_read(1, (addr - 0xc0) >> 1);
                   19064:                break;
1.1.1.37  root     19065:        case 0x278: case 0x279: case 0x27a:
                   19066:                val = pio_read(1, addr);
                   19067:                break;
1.1.1.29  root     19068:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     19069:                val = sio_read(3, addr);
                   19070:                break;
1.1.1.25  root     19071:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     19072:                val = sio_read(1, addr);
                   19073:                break;
1.1.1.25  root     19074:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     19075:                val = pio_read(0, addr);
                   19076:                break;
1.1.1.25  root     19077:        case 0x3ba: case 0x3da:
1.1.1.33  root     19078:                val = vga_read_status();
                   19079:                break;
1.1.1.37  root     19080:        case 0x3bc: case 0x3bd: case 0x3be:
                   19081:                val = pio_read(2, addr);
                   19082:                break;
1.1.1.29  root     19083:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     19084:                val = sio_read(2, addr);
                   19085:                break;
1.1.1.25  root     19086:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     19087:                val = sio_read(0, addr);
                   19088:                break;
1.1       root     19089:        default:
1.1.1.33  root     19090: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     19091:                break;
                   19092:        }
1.1.1.33  root     19093: #ifdef ENABLE_DEBUG_IOPORT
                   19094:        if(fp_debug_log != NULL) {
                   19095:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   19096:        }
                   19097: #endif
                   19098:        return(val);
1.1       root     19099: }
                   19100: 
                   19101: UINT16 read_io_word(offs_t addr)
                   19102: {
                   19103:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   19104: }
                   19105: 
1.1.1.33  root     19106: #ifdef USE_DEBUGGER
                   19107: UINT16 debugger_read_io_word(offs_t addr)
                   19108: {
                   19109:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   19110: }
                   19111: #endif
                   19112: 
1.1       root     19113: UINT32 read_io_dword(offs_t addr)
                   19114: {
                   19115:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   19116: }
                   19117: 
1.1.1.33  root     19118: #ifdef USE_DEBUGGER
                   19119: UINT32 debugger_read_io_dword(offs_t addr)
                   19120: {
                   19121:        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));
                   19122: }
                   19123: #endif
                   19124: 
1.1       root     19125: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     19126: #ifdef USE_DEBUGGER
                   19127: {
                   19128:        if(now_debugging) {
                   19129:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   19130:                        if(out_break_point.table[i].status == 1) {
                   19131:                                if(addr == out_break_point.table[i].addr) {
                   19132:                                        out_break_point.hit = i + 1;
                   19133:                                        now_suspended = true;
                   19134:                                        break;
                   19135:                                }
                   19136:                        }
                   19137:                }
                   19138:        }
                   19139:        debugger_write_io_byte(addr, val);
                   19140: }
                   19141: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   19142: #endif
1.1       root     19143: {
1.1.1.25  root     19144: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     19145:        if(fp_debug_log != NULL) {
1.1.1.43  root     19146: #ifdef USE_SERVICE_THREAD
                   19147:                if(addr != 0xf7)
                   19148: #endif
1.1.1.33  root     19149:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     19150:        }
                   19151: #endif
1.1       root     19152:        switch(addr) {
1.1.1.29  root     19153: #ifdef SW1US_PATCH
                   19154:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   19155:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   19156:                sio_write(0, addr - 1, val);
                   19157:                break;
                   19158: #else
1.1.1.25  root     19159:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   19160:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   19161:                dma_write(0, addr, val);
                   19162:                break;
1.1.1.29  root     19163: #endif
1.1.1.25  root     19164:        case 0x20: case 0x21:
1.1       root     19165:                pic_write(0, addr, val);
                   19166:                break;
1.1.1.25  root     19167:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     19168:                pit_write(addr & 0x03, val);
                   19169:                break;
1.1.1.7   root     19170:        case 0x60:
                   19171:                kbd_write_data(val);
                   19172:                break;
1.1.1.9   root     19173:        case 0x61:
                   19174:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   19175:                        // beep on
                   19176: //                     MessageBeep(-1);
                   19177:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   19178:                        // beep off
                   19179:                }
                   19180:                system_port = val;
                   19181:                break;
1.1       root     19182:        case 0x64:
1.1.1.7   root     19183:                kbd_write_command(val);
1.1       root     19184:                break;
                   19185:        case 0x70:
                   19186:                cmos_addr = val;
                   19187:                break;
                   19188:        case 0x71:
1.1.1.8   root     19189:                cmos_write(cmos_addr, val);
1.1       root     19190:                break;
1.1.1.25  root     19191:        case 0x81:
                   19192:                dma_page_write(0, 2, val);
                   19193:        case 0x82:
                   19194:                dma_page_write(0, 3, val);
                   19195:        case 0x83:
                   19196:                dma_page_write(0, 1, val);
                   19197:        case 0x87:
                   19198:                dma_page_write(0, 0, val);
                   19199:        case 0x89:
                   19200:                dma_page_write(1, 2, val);
                   19201:        case 0x8a:
                   19202:                dma_page_write(1, 3, val);
                   19203:        case 0x8b:
                   19204:                dma_page_write(1, 1, val);
                   19205:        case 0x8f:
                   19206:                dma_page_write(1, 0, val);
1.1       root     19207:        case 0x92:
1.1.1.7   root     19208:                i386_set_a20_line((val >> 1) & 1);
1.1       root     19209:                break;
1.1.1.25  root     19210:        case 0xa0: case 0xa1:
1.1       root     19211:                pic_write(1, addr, val);
                   19212:                break;
1.1.1.25  root     19213:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   19214:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     19215:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     19216:                break;
1.1.1.35  root     19217: #ifdef USE_SERVICE_THREAD
                   19218:        case 0xf7:
                   19219:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     19220:                if(in_service && cursor_moved) {
                   19221:                        // update cursor position before service is done
                   19222:                        pcbios_update_cursor_position();
                   19223:                        cursor_moved = false;
                   19224:                }
1.1.1.35  root     19225:                finish_service_loop();
                   19226:                break;
                   19227: #endif
1.1.1.37  root     19228:        case 0x278: case 0x279: case 0x27a:
                   19229:                pio_write(1, addr, val);
                   19230:                break;
1.1.1.29  root     19231:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   19232:                sio_write(3, addr, val);
                   19233:                break;
1.1.1.25  root     19234:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   19235:                sio_write(1, addr, val);
                   19236:                break;
                   19237:        case 0x378: case 0x379: case 0x37a:
                   19238:                pio_write(0, addr, val);
                   19239:                break;
1.1.1.37  root     19240:        case 0x3bc: case 0x3bd: case 0x3be:
                   19241:                pio_write(2, addr, val);
                   19242:                break;
1.1.1.29  root     19243:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   19244:                sio_write(2, addr, val);
                   19245:                break;
1.1.1.25  root     19246:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   19247:                sio_write(0, addr, val);
                   19248:                break;
1.1       root     19249:        default:
1.1.1.33  root     19250: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     19251:                break;
                   19252:        }
                   19253: }
                   19254: 
                   19255: void write_io_word(offs_t addr, UINT16 val)
                   19256: {
                   19257:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   19258:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   19259: }
                   19260: 
1.1.1.33  root     19261: #ifdef USE_DEBUGGER
                   19262: void debugger_write_io_word(offs_t addr, UINT16 val)
                   19263: {
                   19264:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   19265:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   19266: }
                   19267: #endif
                   19268: 
1.1       root     19269: void write_io_dword(offs_t addr, UINT32 val)
                   19270: {
                   19271:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   19272:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   19273:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   19274:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   19275: }
1.1.1.33  root     19276: 
                   19277: #ifdef USE_DEBUGGER
                   19278: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   19279: {
                   19280:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   19281:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   19282:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   19283:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   19284: }
                   19285: #endif

unix.superglobalmegacorp.com

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